Quick Reference to Setup LEMP stack (Linux, Nginx, MySQL, PHP) for Ubuntu 16.04

Quick reference of commands to setup LEMP stack

Contains list of commands for quick setup of the LEMP stack in ubuntu@16.0

 

Step 1 – Login to your server through terminal —

sudo ssh -i {your pem file path} ubuntu@{your public IP}

For more reference  — https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04

Step 2 – Setup NGNIX

sudo apt-get update
sudo apt-get install nginx

Start NGNIX

sudo systemctl start nginx

Check NGNIX status

sudo systemctl status nginx

Type one of the addresses that you receive in your web browser. It should take you to Nginx’s default landing page:

http://server_domain_or_IP

Nginx default page

Step 3 – Install MYSQL

sudo apt-get install mysql-server mysql-client

Continue and add password for the root user

Check Mysql status

sudo systemctl status mysql

Start or Restart Mysql

sudo /etc/init.d/mysql start
sudo start mysql
sudo /etc/init.d/mysql restart

Step 4 – Install PHP

sudo apt-get install php-fpm php-mysql

Configure php by

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

Search for ;cgi.fix_pathinfo=1 and uncomment it by replacing

cgi.fix_pathinfo=0

Now Check PHP status

sudo systemctl status php7.0-fpm

Now restart it

sudo systemctl restart php7.0-fpm

Step 4 – Install PHPMyAdmin

sudo apt-get install phpmyadmin

Select apache2 when prompted and configure database by choosing dbconfig-common yes
Continue and add password for the root user

Now restart Nginx

sudo service nginx restart

To increase post_max_upload_size in phpMyAdmin

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

then change value of post_max_size to

post_max_size = 25M

And upload_max_size to

upload_max_filesize = 24M

Then restart php by

sudo systemctl restart php7.0-fpm

Step 5 – Configure Nginx to use the PHP Processor

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

replace it by — and update it for yourserver — host name

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    #if you are using laravel php clone your git repo to /var/www and add the path below
    root /var/www/git_repo_name/public/;
    index index.php index.html index.htm;

    server_name name_of_your_web_host_name;




     location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ .php$ {
        try_files $uri /index.php =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;
    }
    location /phpmyadmin {
               root /usr/share/;
 index index.php index.html index.htm;
                location ~ ^/phpmyadmin/(.+.php)$ {
                       try_files $uri =404;
                       root /usr/share/;
                        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include /etc/nginx/fastcgi_params;
               }
               location ~* ^/phpmyadmin/(.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                       root /usr/share/;
               }
        }

}

Test your configuration file for syntax errors by typing:

sudo nginx -t

Reload Ngnix

sudo systemctl reload nginx

Now the setup for LEMP is complete

Step 6 – Configure PHP Laravel composer
Clone your git repo to /var/www/ path

sudo git clone your_git_repo_path

Now, Change you path to cloned repo

sudo cd /var/www/your_repo_name

Install PHP Composer

sudo apt-get install curl php-curl php-mcrypt php-mbstring php-gettext
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Install gZip for helping composer install

sudo apt-get install zip unzip

Now, Run Composer install

sudo composer install

Now, Create .env file and copy and paste it from you local host repo

sudo cp .env.example .env

Change your storage path permission following clearing cache

sudo php artisan cache:clear
sudo chmod -R gu+w storage
sudo chmod -R guo+w storage
sudo chmod -R 777 storage

Now, Restart Ngnix by

sudo systemctl restart nginx

If you want to host the database in different instansce

Step 7 – Configure PHPMyAdmin Setup in new instanse

Log into your new instance and repeat step 4
Now,

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

and search for  bind-address and replace it with

bind-address            = 0.0.0.0

And add rule to allow All-Traffic in your inbound of database instance
restart MySql

sudo /etc/init.d/mysql start

In Server
Also in Server instance add rule to allow MYSQL/Aurora in inbound
Now,

sudo nano /var/www/app_repo/.env

And edit

DB_CONNECTION=mysql
DB_HOST=database instanse
DB_PORT=3306
DB_DATABASE=databasename
DB_USERNAME=username
DB_PASSWORD=password

Now restart NGNIX and MYSql in server by running following commands

sudo systemctl reload nginx
sudo /etc/init.d/mysql restart

If you are using php laravel run following commands

composer dumpautoload
php config:cache
php artisan optimize

enjoy your day!

Leave a Reply

Your email address will not be published. Required fields are marked *