Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save misdreavus79/b423ea874f839cbadef02cf65044c700 to your computer and use it in GitHub Desktop.

Select an option

Save misdreavus79/b423ea874f839cbadef02cf65044c700 to your computer and use it in GitHub Desktop.
Laravel 5.x on Ubuntu 17.x, PHP 7.x, Nginx 1.9.x

Steps to install latest Laravel, LEMP on Google Cloud Ubuntu 17.04 version.

Fork of https://gist.github.com/santoshachari/87bf77baeb45a65eb83b553aceb135a3

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.1 php7.1-fpm php7.1-mysql php7.1-zip php7.1-gd
sudo apt-get install -y nginx git

Important packages for Laravel

sudo apt-get install -y php7.1-mbstring php7.1-xml php7.1-dom mcrypt php7.1-mcrypt

Some Optional ones

sudo apt-get install -y php7.1-curl php7.1-json

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

sudo apt-cache search php7.1-*

Modify the PHP Configuration

Run the following command to go to php.ini

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

And uncomment and fix cgi.fix_pathinfo to

cgi.fix_pathinfo=0

Restart PHP 7.1 FPM

sudo service php7.1-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

Alternatively, clone an existing repo into the var/www/html folder:

cd /var/www/html
sudo rm index.nginx-debian.html
sudo git clone https://github.com/your-username/your-repo.git

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;

        root /var/www/laravel/public; #if cloning your repo, it should be "/var/www/html/<your-project-folder-name>/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.1-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. Google has a GUI configuration for HTTP, but if that doesn't work, you may have to allow HTTP on nginx yourself. To do so, run:

sudo ufw allow 'Nginx HTTP'
sudo ufw enable
sudo ufw status

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.

New Project

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-get intsall php-7.1-<package name>

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

Existing Project

If you cloned an existing repo, you'll run:

sudo chown -R :www-data /var/www/html
sudo chmod -R 775 /var/www/html/storage/
sudo cp .env.example .env
sudo chmod 775 .env
sudo chmod -R 775 bootstrap/cache
sudo composer install

Change the environment settings in the .env file appropriately, then run:

sudo php artisan key:generate
sudo php artisan optimize

If this throws any errors (likely due to file permissions), change the permissions to each file or folder accordingly, then run the commands again.

Feel free to change the permissions from 775 to something more restrictive once the installation process is complete.

Finish

Check your site by http://server_domain_or_IP and it should show you "Laravel" homepage (or your site's domain if you cloned from a repo).

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 its 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.

Need SSL?

This guide should get you on the right track: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

@harysz
Copy link

harysz commented Jul 22, 2018

sudo nano /etc/php/7.1/fpm/php.ini after i run this command it tells me that file doesnt exist i do everything step by step

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