How-to Install PHP 7.2.x, NGINX 1.10.x & Laravel 5.6
Step 1: Get a Server!
This example is written for Ubuntu 16.04.3 provided by DigitalOcean and ServerHub.
Provision a new droplet, vps, server, whatever you’re using and once you have the login details, login to SSH!
When you’re at the root terminal copy/paste the commands below to get everything configured.
Step 2: Add the PHP 7 Repositories
Run the following commands, as root, to install the PHP repositories to your sources.list, update apt-get and install.
add-apt-repository ppa:ondrej/php
If you’re missing add-apt-repository, like some plain systems are, install it and then add-apt-repository ppa:ondrej/php
apt-get install software-properties-common
apt-get install python-software-properties
then
apt-get update
apt-get -y install unzip zip nginx php7.2 php7.2-mysql php7.2-fpm php7.2-mbstring php7.2-xml php7.2-curl
After everything is installed you’ll need to configure both PHP 7.2 and Nginx to work together and to work with Laravel, it’s not to hard.
Step 3: Configure NGINX
Here we will remove the default config link and create a new config with support for PHP and Laravel.
Please note: The config is written with a heredoc, if you copy/paste just the config be sure to remove the escaped $ (the \$).
rm -f /etc/nginx/sites-enabled/default
cat <<EOF > /etc/nginx/sites-available/laravel
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;root /var/www/html/public;
index index.php index.html index.htm;server_name localhost;
charset utf-8;gzip on;
gzip_vary on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/xml+rss;location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}location ~ \.php\$ {
try_files \$uri /index.php =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;
}location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|svg|woff|woff2|ttf)\$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}location ~* \.(?:css|js)\$ {
expires 7d;
access_log off;
add_header Cache-Control "public";
}location ~ /\.ht {
deny all;
}}
EOF
ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/laravel
Adjust settings like root and server_name for your desired project.
Now restart nginx.
/etc/init.d/nginx restart
Optional: Install MySQL
I wasn’t going to cover this but its so simple. I tend to stick my databases on remote servers and don’t often do this on my web-servers, but you’re more then welcome to.
apt-get -y install mysql-client mysql-server
mysql_secure_installation
Step 4: Installing Composer & Laravel
apt-get -y install composer
cd /var/www/html
rm -f index.nginx-debian.html
composer create-project laravel/laravel .
mv .env.example .env
php artisan key:generate
Then set permissions
chown -R www-data:www-data /var/www/html
chmod -R 775 /var/www/html/storage
That’s it! Laravel is ready to go in /var/www/html — check your web-servers ip/hostname in a browser and if you’re like me you’ll see:
Conclusion
That’s all it takes to get a new server setup and running with PHP 7, Nginx 1.10.x, MySQL and Laravel 5.6.
Spend some time to optimize your nginx and fpm configs. Don’t forget to secure your setup as well!