How-to Install PHP 7.0, NGINX 1.9.x & Laravel 5.x

UPDATE: There is a new version of this article.

Will Bowman
asked.io
3 min readOct 1, 2017

--

Step 1: Get a Server!

UPDATE: Directions updated for Ubuntu 16.04 from DigitalOcean.

This example was originally written for Ubuntu 14.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 & NGINX 1.9.x Repositories

Run the following commands, as root, to install the PHP 7 and Nginx 1.9.x repositories to your sources.list, add the key, 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

echo 'deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx' >> /etc/apt/sources.list
echo 'deb-src http://nginx.org/packages/mainline/ubuntu/ trusty nginx' >> /etc/apt/sources.list
wget -q -O- http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
apt-get update
apt-get -y install nginx php7.0 php7.0-mysql php7.0-fpm php7.0-mbstring php7.0-xml php7.0-curl

After everything is installed you’ll need to configure both PHP 7 and Nginx 1.9.x to work together and to work with Laravel 5.2, it’s not to hard.

Step 3: Configure NGINX

The default config for Nginx isn’t that great, so let’s go ahead and move it then create a new one:

Please note: The config is written with a heredoc, if you copy/paste just the config be sure to remove the escaped $ (the \$).

mv /etc/nginx/conf.d/default.conf /etc/nginx/default.conf-bkup
cat <<EOF > /etc/nginx/conf.d/default.conf
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.0-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

Adjust settings like root and server_name for your desired project.

Now that you have a working config let’s update PHP 7 to work with Nginx 1.9.x.

Step 4: Configure PHP 7

For FPM we need to fix_pathinfo.

echo ‘cgi.fix_pathinfo=0’ >> /etc/php/7.0/fpm/php.ini

Restart Services

mkdir -p /var/www/html
/etc/init.d/nginx restart
/etc/init.d/php7.0-fpm restart

That’s it for your basic web-server, Laravel 5.2 ready — now do some restarting and let’s install Laravel!

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 5: Installing Composer & Laravel

cd ~
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
cd /var/www/html
rm -f index.nginx-debian.html
composer create-project laravel/laravel .

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.9.x, MySQL and Laravel 5.2.

Spend some time to optimize your nginx and fpm configs, 1gb ram tends to crap out at around 30 concurrents with this default setup. Don’t forget to secure your setup as well!

--

--