TLDR; guide for deploying Laravel 5.6 to Debian 8
This is a real simple, bare minimum guide to quickly boot up a private VPS with Linux Debian 8 and configure it to start serving up your Laravel webapp. It’s not a full guide on how you should configure your server, there is a lot more to be done past this article, but this should serve you well in getting your webapp online.
Working locally with Laravel Valet/Homestead is super convenient and totally serves (😏) the purpose they were built for. However once our apps are built and ready to be released to the public, we need to get them off our machines and onto a server. First, lets look at hosting
Hosting
I am using ZappieHost as they local to me (in New Zealand), and provide a super reliable and stable service. Plus the cost is crazy good ¯\_(ツ)_/¯
First, start with a fresh install of Debian 8 (minimal). Minimal is good as it doesn’t have the bloatware of the other distros, providing us just the bare-bones we need to start building our server up.
Once installed, boot and ssh in.
Lets start with some basics to get the system rolling
apt-get update
apt-get upgrade
apt-get install sudo
apt-get install git
Later in the process we use Perl to enable php7.2-fpm and it will throw errors that we do not have a locale/language set. Lets sort this out now using the below (en_NZ for me, whatever in the list for you). Thomas covers this in greater detail here, but the short of it is;
export LANGUAGE=en_NZ.UTF-8
export LANG=en_NZ.UTF-8
export LC_ALL=en_NZ.UTF-8
locale-gen en_NZ.UTF-8
dpkg-reconfigure locales
Now lets get the good stuff rolling, the reason we are booting this VPS up in the first place.
Install PHP
Thanks to Chris Shaw, this process is very simple (more details and info on his blog here);
You can update your system with packages from this PPA by adding ppa:ondrej/php to your system’s Software Sources.
sudo apt-get install apt-transport-https lsb-release ca-certificatessudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpgecho "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.listsudo apt-get update
Now we have the PPA loaded, time to Install PHP and PHP-fpm
sudo apt-get install php7.2 && sudo apt-get install php7.2-fpm#install additional packages and extensions
sudo apt-get install php7.2-cli php7.2-common php7.2-curl php7.2-gd php7.2-json php7.2-mbstring php7.2-mysql php7.2-opcache php7.2-readline php7.2-xml#install php-zip
sudo apt-get install php7.2-zip
To verify PHP has installed, call
php -v
Install MySQL
Assuming you want to use MySQL for your application, Rahul has a great guide for installing MySQL on Debian, below are the key steps I’ve used to set it up.
First we need to download the config package(MySQL docs explains this process here too)
# Get the package and install
wget http://repo.mysql.com/mysql-apt-config_0.8.9-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.9-1_all.deb
You will be greeted with the following. If you want MySQL 5.7 installed, choose 4. In my case I want 5.6 so I choose 1.
1. MySQL Server & Cluster (Currently selected: mysql-5.7)
2. MySQL Tools & Connectors (Currently selected: Enabled)
3. MySQL Preview Packages (Currently selected: Disabled)
4. Ok
Which MySQL product do you wish to configure?
Next choose what you want to install, for me I choose 1
1. mysql-5.6
2. mysql-5.7
3. mysql-8.0 preview
4. mysql-cluster-7.5
5. mysql-cluster-7.6
6. None
Which server version do you wish to receive?
Finally you’ll be back to the first display, but with your chosen install preference. If thats ok, choose 4.
1. MySQL Server & Cluster (Currently selected: mysql-5.6)
2. MySQL Tools & Connectors (Currently selected: Enabled)
3. MySQL Preview Packages (Currently selected: Disabled)
4. Ok
Which MySQL product do you wish to configure?
Now we are ready to update and install, usual commands being
# Updates package list and installs mysql server
sudo apt update
sudo apt install mysql-server
Once installed, MySQL will ask for a set of passwords. Enter your passwords and boom, we done with MySQL for now.
Install Nginx
Nginx is super easy to install, simply call sudo apt-get install nginx
to install. Now for me, I get the Apache landing page, if you cd /var/www/html
into the folder you might see that Apache has sneaked its index.html
file in there
Simply rm index.html && cp index.nginx-debian.html index.html
to remove and create a new index.html
from the Nginx file then go to http://your_ip_address
and you should see the nginx html file
Lets verify that nginx is bound to port 80 using netstat -plntu
If you see that Apache is on port 80, remove/stop Apache.
#Remove apache as we dont want you..
sudo apt remove apache2.*
Install Composer
The Composer install guide is super easy to follow, more info found here, however the tldr is
#get and install composer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
Since composer was installed into the folder you were in, lets move it to the root bin dir using
mv composer.phar /usr/local/bin/composer
You can now call composer from anywhere in your system 🙌
Clone Repo
Now we want to get our Laravel app onto our server, so create a new folder in the web root dir, mkdir /var/www/laravel_app_name && cd /var/www/laravel_app_name
Now we are in the newly created folder, lets clone the repo in using
git clone {repo} .
Make sure to use the .
after the repo URL as it will pull the project into that folder, rather than create a new folder inside and pull into there
Install Laravel dependancies
Once installed, we need to pull our dependancies in using composer install
. This will take a bit of time, grab a coffee and wait until its finished.
When its done we need to update some permissions and group ownership on our folders, so lets do that now.
chown -R www-data:root /var/www/laravel_app_name
chmod 755 /var/www/laravel_app_name/storage
Create Database
We need to create a new MySQL database before we can boot up our App (again assuming you are using a database). Simple steps for creating this are
mysql -uroot -p
#enter your passwordcreate database db_name_here;exit;
All going well, you should have created your database.
Create .env file
Next, we need to add our .env
file and details, so lets create that now and ill let you paste your details in there.
sudo vi .env
Generate Laravel Auth Key
Now we need to generate our key, the Laravel Docs state the below..
If the application key is not set, your user sessions and other encrypted data will not be secure!
Simply call php artisan key:generate
and artisan will create this key and update your .env file for you
Migrate Database migrations
Migrate your database migrations as normal
php artisan migrate
Setup Nginx
We need to tell Nginx to serve up our webapp rather than be default index file, so first we need to create a config in the /etc/nginx/sites-available
folder..
sudo vi /etc/nginx/sites-available/lara_app_name# now paste in these basic settings, change the bold items
server { listen 80;
listen [::]:80 ipv6only=on; # Log files for Debugging
access_log /var/log/nginx/laravel-access.log;
error_log /var/log/nginx/laravel-error.log; # Webroot Directory for Laravel project
root /var/www/laravel_dir/public;
index index.php index.html index.htm; # Your Server Name
server_name test.server;
location / {
try_files $uri $uri/ /index.php?$query_string;
} # PHP-FPM Configuration Nginx
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}}
Now create a symlink from sites-avaiable -> sites-enabled
ln -s /etc/nginx/sites-available/lara_app_name /etc/nginx/sites-enabled/
After this we want to test to make sure we don’t have any errors, restart and enable Nginx
nginx -t
# should show all oksystemctl restart nginx
systemctl enable nginx
Now if you navigate to http://your_ip_address
you should see your Laravel webapp up and running.
Thats the steps ive taken many times to get my Laravel apps booted and online. NOTE we are using http and NOT https, so in the next article I will post a quick guide to using LetsEncrypt with Laravel 5.6 to issue our certs and get our sites running over https.