Skip to content

Instantly share code, notes, and snippets.

@naveenyagati
Last active September 1, 2020 03:59
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save naveenyagati/5deec8fc40a2faaff20c629362dddf39 to your computer and use it in GitHub Desktop.
Save naveenyagati/5deec8fc40a2faaff20c629362dddf39 to your computer and use it in GitHub Desktop.
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: http://do.co/le-nginx

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 https://getcomposer.org/installer | 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
Copy link

ghost commented May 13, 2017

sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/phpudo apt-get install zip unzip
Gets an error

Error: need a single repository as argument

@naveenyagati
Copy link
Author

@dmgdwd I have modified it,thanks for pointing it out

sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/phpudo
sudo apt-get install zip unzip

@flashery
Copy link

flashery commented Jun 8, 2017

No repository for this?
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/phpudo

Cannot add PPA: 'ppa:~ondrej/ubuntu/phpudo'.
The user named '~ondrej' has no PPA named 'ubuntu/phpudo'

@souinhua
Copy link

This didn't work for me. I executed all the commands here but nothing happened :(. Still loading the shark page.

@naveenyagati
Copy link
Author

naveenyagati commented Jun 30, 2017

@souinha you probably missed this step

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

@naveenyagati
Copy link
Author

@flashery I have modified it please have a look

@souinhua
Copy link

You're such an AWESOME guy! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment