Skip to content

Instantly share code, notes, and snippets.

@masdimdev
Forked from santoshachari/Laravel PHP7 LEMP AWS.md
Last active April 3, 2017 11:49
Show Gist options
  • Save masdimdev/fe56206a2eb3b8c5ae7f5bc6d2628fd6 to your computer and use it in GitHub Desktop.
Save masdimdev/fe56206a2eb3b8c5ae7f5bc6d2628fd6 to your computer and use it in GitHub Desktop.
Laravel 5.x on Ubuntu 14.x, PHP 7.x, Nginx 1.9.x

#Steps to install latest Laravel, LEMP on AWS Ubuntu 14.4 version. This tutorial is the improvised revision 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 software-properties-common python-software-properties
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y zip unzip php7.0 php7.0-fpm php7.0-mysql php7.0-zip php7.0-gd nginx git

Important packages for Laravel

sudo apt-get install -y mcrypt php7.0-mcrypt php7.0-mbstring php7.0-xml 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/html

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 service php7.0-fpm restart

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

Manual setup additional hosts

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

Semi-automating setup additional hosts

#!/bin/bash

if [ "$1" = "" ]
then
    echo "Usage: $0 <your domain without www>"
    exit
fi
domain=$1
root="/var/www/$domain/public"
block="/etc/nginx/sites-available/$domain"

# Create the Document Root directory
sudo mkdir -p $root
sudo cp /usr/share/nginx/html/index.html $root

# Assign ownership to your regular user account
sudo chown -R $USER:$USER $root

# Create the Nginx server block file:
sudo tee $block > /dev/null <<EOF 
server {
        listen 80;
        listen [::]:80;

        root $root;
        index index.php index.html index.htm;

        server_name $domain www.$domain;

        location / {
                try_files \$uri \$uri/ =404;
        }
        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;
        }
}


EOF

# Link to make it available
sudo ln -s $block /etc/nginx/sites-enabled/

# Test configuration and reload if successful
sudo nginx -t && sudo service nginx reload

You can put that in a file (example: domain.sh) and then make it executable with chmod:

chmod +x domain.sh

You should then be able to generate new server blocks by typing:

./domain.sh <your_domain_here>

Type this if error /bin/bash^M: bad interpreter: No such file or directory

sed -i -e 's/\r$//' domain.sh

then execute domain.sh again.

Thanks jellingwood

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