5/11/2017 - 6:47 PM

Configuration for Installing Laravel with LEMP on Ubuntu 16.04 on Digital Ocean

Configuration for Installing Laravel with LEMP on Ubuntu 16.04 on Digital Ocean

Steps to install latest Laravel with LEMP on Digital Ocean Ubuntu 16.4

This tutorial is made based upon my experiences.In DigitalOcean Firewall in Configured by default so no changes are needed.Let's Encrypt has been pre-installed for us in Digita Ocean, please see:

Install PHP 7 on Ubuntu

sudo apt-get update
sudo apt-get install -y language-pack-en-base
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
sudo apt-get install zip unzip
sudo apt-get install -y php7.0 php7.0-fpm php7.0-mysql php7.0-zip php7.0-gd
sudo apt-get install nginx git

Installing php dependencies for Laravel

mcrypt and mbstring are must for Laravel installation

sudo apt-get install mcrypt php7.0-mcrypt
sudo apt-get install -y php7.0-mbstring php7.0-xml --force-yes

Modifying the PHP Configuration

Command to edit the php.ini file

sudo nano /etc/php/7.0/fpm/php.ini

Uncomment and change to cgi.fix_pathinfo=0 Restart PHP 7.0 FPM

sudo service php7.0-fpm restart

Our PHP is now completely configured and we can move on.

Configure Nginx and the Web Root

I am going to setup using a blank latest version of Laravel. This would installed in /var/www/laravel folder. Run the following command to create the project folder:

sudo mkdir -p /var/www/laravel

It would be better to Backup the original Nginx's configuration file before modifying it,so that it would be easy to revert to previous version if anything goes wrong

sudo mkdir ~/Backup
sudo cp /etc/nginx/sites-available/default ~/Backup/default
sudo cp /etc/nginx/sites-available/digitalocean ~/Backup/digitalocean
Note:If you go for **ONE CLICK APP INSTALL** you have to edit the file named "digitalocean" in "/etc/nginx/site-available" directory for changes to take effect. If not the default LEMP landing page would be shown there

Command to edit the main file,in digital ocean ONE CLICK APP INSTALL

sudo nano /etc/nginx/sites-available/digitalocean

Command to edit the main file,in digital ocean normal installation.

sudo nano /etc/nginx/sites-available/default

Now replace the contents of the file with the following code

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/laravel/public;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name <Your Domain name / Public IP Address>;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;

We have mentioned root directory to point at root /var/www/laravel/public because in Laravel Public folder is the root directory and laravel is the project name in root /var/www/laravel/public

Now restart Nginx for changes to take effect

sudo service nginx restart

Create Swap File (Optional)

If you have a 512 ram droplet you should have a swap so that it can be used when the server is out of memory.

sudo fallocate -l 1G /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Installing Composer and Laravel

Run the following command for installing composer

cd ~
curl -sS | php
sudo mv composer.phar /usr/local/bin/composer

Run the following command for installing Laravel

sudo composer create-project laravel/laravel /var/www/laravel

If any errors show up run the following command

sudo service php7.0-fpm restart

Setting Permissions for Laravel Folders

Run the following command for setting permissions

sudo chown -R :www-data /var/www/laravel
sudo chmod -R 775 /var/www/laravel/storage
sudo chmod -R 775 /var/www/laravel/bootstrap/cache