Skip to content

Instantly share code, notes, and snippets.

@mosufy
Last active August 14, 2023 17:03
Show Gist options
  • Save mosufy/ee5094a791097b1006c6 to your computer and use it in GitHub Desktop.
Save mosufy/ee5094a791097b1006c6 to your computer and use it in GitHub Desktop.
LEMP Stack Installation Guide. Linux (Ubuntu), Nginx, MySQL, PHP

LEMP Stack Installation Guide

LEMP is the new LAMP. If you were an Apache user like most of us, simply follow this guide to easily deploy and learn develop to develop on Nginx.

Pre-requisites

Be sure that you are already running Linux be it Ubuntu, CentOS or RHEL. However, this guide will demonstrate installation guide for Ubuntu Server 14.04 LTS.

Step 1. Install Nginx

Nginx is a reverse proxy server. It is known to be a much more efficient and optimized web server when compared to Apache. However, there are their pros and cons to both. Google them for comparisons.

$ sudo apt-get update
$ sudo apt-get install nginx

Go to the ip address or domain name that you have pointed to and you should see a 'Welcome to nginx!'

Step 2. Install MySQL

$ sudo apt-get install mysql-server
$ sudo mysql_install_db
$ sudo mysql_secure_installation

Answer Yes to most of the questions

Step 3. Install PHP

Nginx requires a special form of PHP called php-fpm, which stands for fastCGI process manager. We will later tell nginx to pass PHP requests to this software for processing.

$ sudo apt-get install php5-fpm php5-mysql

Configure PHP Processor

Now that PHP-fpm is installed, we need to secure it.

$ sudo nano /etc/php5/fpm/php.ini

Uncomment cgi.fix_pathinfo and set it to "0". This is a security fix to disable PHP from finding the closes file if no match is found.

cgi.fix_pathinfo=0

Restart PHP Processor

$ sudo service php5-fpm restart

Step 4. Configure Nginx to use PHP Processor

Similar to Apache's Virtual Hosts, Nginx uses Server Blocks to determine the files to be processed.

Open the default Nginx server block configuration and update to the following setup. Take note that the below is specifically created to be used for my mosufy/php-mvc package on GitHub https://github.com/mosufy/php-mvc.

Open the default server block and update the following

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

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

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

  server_name domain_name_or_IP;

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

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }
  
  # deny access to .htaccess files to prevent conflict
  location ~ /\.ht {
    deny all;
  }
}
...

Save and restart Restart nginx to effect the changes.

$ sudo service nginx restart

Step 5. Test Configuration

CONGRATULATIONS! At this stage, your LEMP stack is successfully installed. To confirm everything is in order, create a simple PHP file.

$ sudo nano /usr/share/nginx/html/info.php

...
<?php
  phpinfo();
?>

Go to your website URL using your IP or domain name that is pointed to the server.

http://server_domain_name_or_IP/info.php

You should be able to see PHP Configuration information. Be sure to remove the file after your review!

$ sudo rm /usr/share/nginx/html/info.php

Bonus Step: Setting multiple hosts

Now that you have your main website up, setting up multiple virtual hosts is also possible.

Copy the default server block to create a second server block (virtual host)

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/domain_name
$ sudo nano /etc/nginx/sites-available/domain_name

...
server {
  listen 80;
  listen [::]:80;

  root /var/www/domain_name/public;
  index index.php index.html index.htm;

  server_name domain_name;
  access_log /var/log/nginx/domain_name-access.log;
  error_log /var/log/nginx/domain_name-error.log;

  location / {
    try_files $uri $uri/ @mvcrewrite;
  }
  
  location @mvcrewrite {
    rewrite ^(.+)$ /index.php?url=$1 last;
  }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }
  
  # deny access to .htaccess files to prevent conflict
  location ~ /\.ht {
    deny all;
  }
}

Sites must be activated at /etc/nginx/sites-enabled. To do this, simply create a symbolic link to sites-activated that you have just created earlier.

$ sudo ln -s /etc/nginx/sites-available/domain_name /etc/nginx/sites-enabled/domain_name
$ sudo service nginx restart

And that's it! Simply go to domain_name and nginx's server block will automatically direct your domain name to the relevant folders!

Bonus Step: Install Memcached (optional)

Memcached allows server side caching for faster data retrieval.

$ sudo apt-get install memcached
$ sudo apt-get install php5-memcached
$ sudo service php5-fpm restart
$ sudo service nginx restart

Bonus Step: Install phpMyAdmin (optional)

phpMyAdmin provides a popular an easy to use open-source MySQL management GUI.

  1. Install phpMyAdmin

     $ sudo apt-get update
     $ sudo apt-get install phpmyadmin
    

When prompted for server type, hit tab and choose neither one. When prompoted for database configuration, select dbconfig-common 2. Create symbolic link and restart nginx

    $ sudo ln -s /usr/share/phpmyadmin /usr/share/nginx/www/phpmyadmin
  1. Enable mcrypt on PHP module

     $ sudo php5enmod mcrypt
     $ sudo service php5-fpm restart
     $ sudo service nginx restart
    
  2. You should be able to access phpmyadmin via domain_name_or_ip/phpmyadmin

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