Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel 5.x on Ubuntu 16.x, PHP 7.x, Nginx 1.9.x

#Steps to install latest Laravel, LEMP on AWS Ubuntu 16.4 version. This tutorial is the improvised verision of this tutorial on Digitalocean based on my experience.

Install PHP 7 on Ubuntu

Run the following commands in sequence.

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 update
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

Important packages for Laravel

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

Some Optional ones

sudo apt-get install php7.0-curl php7.0-json

NOTE: You can use the following command to list available PHP 7.0 packages:

sudo apt-cache search php7-*

Modify the PHP Configuration

Run the following command to go to php.ini

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

And uncomment and fix cgi.fix_pathinfo to

cgi.fix_pathinfo=0

Restart PHP 7.0 FPM

sudo service php7.0-fpm restart

Configure Nginx for Laravel

I am going to setup using a blank latest version of Laravel. This would installed in /var/www/laravel folder.

Run the following in terminal:

sudo mkdir -p /var/www/laravel

Next, we are going to modify the nginx's default configuration file: /etc/nginx/sites-available/default. But before that, just make a backup of the file:

sudo mkdir ~/Backups
sudo cp /etc/nginx/sites-available/default ~/Backups/default

Use the following command to edit the file

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

Next modify default from this:

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

        root /usr/share/nginx/html;
        index index.html index.htm;

        server_name localhost;

        location / {
                try_files $uri $uri/ =404;
        }
}

to this:

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;
        }
}

Notice the difference:

  • try_files $uri $uri/ =404; has been changed to try_files $uri $uri/ /index.php?$query_string;
  • and location ~ \.php$ { ... } block has been added.

Restart nginx.

sudo service nginx restart

If all was configured well, you'd see no error.

SWAP file (Optional)

Swap files would help in cases where your server might run out of memory.

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

Install Composer and Laravel

Use the following commands to install composer and make it available across all folders.

cd ~
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Test by composer command to see if it was installed correctly.

Install laravel by using the following command.

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

In case it throws out errors due to missing packages, find the package name using

sudo apt-cache search php7-*

and then remove and create the folder before running the composer create-project again.

sudo rm -rf /var/www/laravel
sudo mkdir /var/www/laravel

Setting the permissions right for the Laravel folders.

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

Finish

Check your site by http://server_domain_or_IP and it should show you "Laravel" homepage.

Other commands of use.

Installed a package that you don't need? Use the following commands to remove the package.

sudo apt-get purge <package name> 
sudo apt-get autoremove --purge

Use following command to remove a folder and it's contents

sudo rm -rfv <folder_name>

Test PHP version

php -v

How to Setup additional hosts?

This is covered in this gist. Both gists were written in continuation.

@scheMeZa

This comment has been minimized.

Copy link

commented Sep 16, 2016

Thank you for this. It helped a lot. What's the database settings I should use?

@santoshachari

This comment has been minimized.

Copy link
Owner Author

commented Oct 23, 2016

Hey, Sorry for the late reply. You can setup MySQL or other database that you can connect with. It shouldn't affect this setup.

@santigarcor

This comment has been minimized.

Copy link

commented Oct 26, 2016

@santoshachari Thank you, it helped me a lot 😄

@ienugr

This comment has been minimized.

Copy link

commented Oct 26, 2016

I have to allow Nginx HTTP first.

sudo ufw allow 'Nginx HTTP'
sudo ufw enable
sudo ufw status
Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)
@tublitzed

This comment has been minimized.

Copy link

commented Oct 30, 2016

This has been massively helpful, thank you!

@tylerapplebaum

This comment has been minimized.

Copy link

commented Nov 30, 2016

Thanks so much! This helped me get going. Used this to get nginx going with LetsEncrypt too: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

@manchoblogs

This comment has been minimized.

Copy link

commented Jan 25, 2017

Truly a Great Tutorial, Believe me - only this tutorial made successful for me without any errors... A Hearty Thanks for @santoshachari

@manchoblogs

This comment has been minimized.

Copy link

commented Jan 25, 2017

Well, If i want to install any other laravel coded project script like bbb.com as a additional domain where already aaa.com exits in the server => then how to ??

Should follow this steps again from : modifying

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

with creating new copy with new domain name ???

@vikas5914

This comment has been minimized.

Copy link

commented Feb 23, 2017

@manchoblogs

you can create a conf like this : /etc/nginx/sites-available/bbb.com

in that file server_name bbb.com www.bbb.com; and listen 80;

After that you need to create Symbolic Link to like this

sudo ln -s /etc/nginx/sites-available/bbb.com /etc/nginx/sites-enabled/bbb.com

Nginx read conf files from sites-enabled folder.

@leandrodaf

This comment has been minimized.

Copy link

commented Mar 12, 2017

Very good, useful!

@thanapongp

This comment has been minimized.

Copy link

commented Mar 21, 2017

Thanks. Works just fine with 16.04 as well.

@santoshachari

This comment has been minimized.

Copy link
Owner Author

commented Mar 22, 2017

@manchoblogs So sorry for replying late. I didn't get any notifications.

As mentioned in the gist, just follow this link for add additional hosts.

@daoduymanhcs

This comment has been minimized.

Copy link

commented Mar 31, 2017

how can I install /phpmyadmin after following this awesome post.

@santoshachari

This comment has been minimized.

Copy link
Owner Author

commented Apr 1, 2017

@daoduymanhcs may be this link would help.

@Baharemo

This comment has been minimized.

Copy link

commented Apr 5, 2017

Hi..Thank for great tutorial. I'm looking for recourse to install mariaDB on ubuntu aws after following this tutorial.
Do you have any resource? thank you

@santoshachari

This comment has been minimized.

Copy link
Owner Author

commented Apr 5, 2017

@Baharemo You would have to do the following:
Install Maria DD as on this link
apt-get install mariadb-server

PHP Mysql connector should work with Maria DB

@olestourko

This comment has been minimized.

Copy link

commented Apr 20, 2017

This guide has been very useful, thanks a bunch!

@notflip

This comment has been minimized.

Copy link

commented May 4, 2017

Can we just swap php70 for 71 in this guide? Thanks! Should it be updated to 7.1?

@RyanRosario

This comment has been minimized.

Copy link

commented May 9, 2017

When I try to install Laravel using sudo, I get an error:

Do not run Composer as root/super user! See https://getcomposer.org/root for details

If I remove the sudo I get another error:

ubuntu@ip-10-0-0-118:~$ composer create-project laravel/laravel /var/www/laravel
Cannot create cache directory /home/ubuntu/.composer/cache/repo/https---packagist.org/, or directory is not writable. Proceeding without cache
Installing laravel/laravel (v5.4.21)
Cannot create cache directory /home/ubuntu/.composer/cache/files/, or directory is not writable. Proceeding without cache


  [InvalidArgumentException]
  Project directory /var/www/laravel/ is not empty.

Has something changed?

EDIT: I chmodded /var/www/laravel/ to 777, ran the command, and then changed the permissions using the commands in this guide and it worked. I am not sure what the cache error is about though.

@HosMercury

This comment has been minimized.

Copy link

commented May 10, 2017

The only working thing for that difficult thing .. thank you man , you are awesome

@santoshachari

This comment has been minimized.

Copy link
Owner Author

commented May 11, 2017

@RyanRosario, Run composer on sudo. I wouldn't bother.

@HosMercury

This comment has been minimized.

Copy link

commented May 13, 2017

I have a problem some assets give me 404
the url linking to them is good .

Permissions also ok .

I don't know where is the problem ?

@melv1n

This comment has been minimized.

Copy link

commented Jun 9, 2017

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

I get the message that the command is deprecated. Probably needs to be updated with correct force/allow suffix.

@santoshachari

This comment has been minimized.

Copy link
Owner Author

commented Jun 19, 2017

@lifesound Is your issue fixed?

@melv1n is your solution working?

@vichetuc

This comment has been minimized.

Copy link

commented Jun 23, 2017

I follow all step, but it doesn't work

@miladr0

This comment has been minimized.

Copy link

commented Sep 1, 2017

if you want install phpmyadmin along with laravel, add the following lines in the server section:

location /phpmyadmin {
root /usr/share/;
index index.php;
try_files $uri $uri/ =404;

location ~ ^/phpmyadmin/(doc|sql|setup)/ {
deny all;
}

location ~ /phpmyadmin/(.+.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;
}
}

@muthurajans2020

This comment has been minimized.

Copy link

commented Sep 23, 2017

Thank you so much. This Blog helps me to complete one of my official task. Thanks a lot Mr.santoshachari

@emoran

This comment has been minimized.

Copy link

commented Oct 3, 2017

Good stuff! all working great at first!

@Younesi

This comment has been minimized.

Copy link

commented Oct 7, 2017

What about installing mysql ?

@doncadavona

This comment has been minimized.

Copy link

commented Nov 7, 2017

Awesome!

@vrajroham

This comment has been minimized.

Copy link

commented Nov 22, 2017

@santoshachari Thanks for your easy and understandable steps. I was able to setup my server running in less than 15 mins. One more things same steps works for php7.1 too. I used php7.1 with all this steps.
Thanks

@tomaytotomato

This comment has been minimized.

Copy link

commented Jan 8, 2018

Also don't forget

sudo apt-get install php7.0-dev

@oscarnevarezleal

This comment has been minimized.

Copy link

commented May 28, 2018

I couldn´t install php7.1 without the following

sudo apt-get install -y python-software-properties
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update -y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.