LEMP stackInstead of LAMP, LEMP (Linux, NGINX, MySQL, and PHP) is a light-weight server stack. The nginx can increase the ability of the server to scale in response to demand. A complete LEMP requires as low as 96mb RAM. So this config is ideal for a budget VPS. This HowTo will introduce the setup of this efficient http server on Ubuntu VPS by SSH commands.

Note: This tutorial shows how you can install Nginx on a fresh minimal installation of Ubuntu server with PHP5 support (through PHP-Fastcgi) and MySQL support. No domain will be specified in this tutorial. Further steps, you can learn from this tutorial: how to install wordpress on LEMP.

Prepare System for Deployment

Before beginning with the installation of LEMP web application stack, issue the following commands to ensure that your system’s package database is up to date and that all installed software is running at the latest version:

apt-get update
apt-get upgrade

Then, you are recommended to create unique folder for all your websites files. In this example, I uses “wwwroot” as my websites root. Create folder on Linux with this command:

mkdir /wwwroot

Install & Configure Nginx Virtual Hosting

Nginx is available as a package for Ubuntu project packages.  Issue the following command to install nginx from the Ubuntu repository:

apt-get install nginx

To Start nginx:

/etc/init.d/nginx start

Then you can type in your IP address into browser. You should see the “Welcome to nginx! ” page.

Now we can go to configure Nginx by issuing the following commands:

vi /etc/nginx/sites-available/default

Add the following

server {
        listen   80; ## listen for ipv4
        listen   [::]:80 default ipv6only=on; ## listen for ipv6
        # Make site accessible from http://localhost/
        server_name  localhost;
        root   /wwwroot;
        index  index.php index.html index.htm;
        }
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
        location /doc {
                root   /usr/share;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }
        location /images {
                root   /usr/share;
                autoindex on;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param  SCRIPT_FILENAME  /wwwroot$fastcgi_script_name;
                include fastcgi_params;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny  all;
        }
}

As I didn’t specify any domain for this LEMP server, you should keep “server_name  localhost; “. This configure file has included the fastcgi setting.

Then you should restart Nginx to enable the configuration:

/etc/init.d/nginx restart

Deploy PHP with FastCGI

PHP5 can only work with nginx through PHP FastCGI or with PHP-FPM. I uses the PHP-FastCGI in this tutorial.

First we will go with PHP-FastCGI package:

apt-get install php5-cli php5-cgi

Once installed the FastCGI, we need to create a file php-fastcgi in init.d. Issue following command:

vi /etc/init.d/php-fastcgi

Add the following contents to the file:

#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=5
PHP_FCGI_MAX_REQUESTS=500
PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0
start() {
      echo -n "Starting PHP FastCGI: "
      start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}
stop() {
      echo -n "Stopping PHP FastCGI: "
      killall -q -w -u $USER $PHP_CGI
      RETVAL=$?
      echo "$PHP_CGI_NAME."
}

case "$1" in
    start)
      start
  ;;
    stop)
      stop
  ;;
    restart)
      stop
      start
  ;;
    *)
      echo "Usage: php-fastcgi {start|stop|restart}"
      exit 1
  ;;
esac
exit $RETVAL

Note: The PHP_FCGI_CHILDREN and PHP_FCGI_MAX_REQUESTS variables has to be configured depending on the server memory and CPU. PHP_FCGI_CHILDREN stands for how many fastCGI tasks can run. More tasks will help handle tasks quicker but consume more CPU and RAM.

Then makes php-fastcgi executable and start the FastCGI service when the server start:

chmod +x /etc/init.d/php-fastcgi
service php-fastcgi start
update-rc.d php-fastcgi defaults

Congratulations! You can now deploy PHP scripts with with your LEMP stack.  You can use the following command to make a test file:

vi /wwwroot/test.php

Add the following content to this file:

<?php phpinfo(); ?>

Then you can type in your address e.g. http://yourIP/test.php to see the result.

Install MySQL and phpMyAdmin

MySQL database engine may be the leading open source relational database engine. WordPress and many other popular website system uses MySQL database solution. Issue the following command to install the MySQL server packages:

apt-get install mysql-server mysql-client php5-mysql

You will be prompted to set a password for the MySQL root user. Choose a strong password and keep it in a safe place.

In order to manage your MySQL database more convenient, you can install the phpmyadmin. This is a tool written in PHP intended to handle the administration of MySQL over the Web. Issue the following command to get phpmyadmin package:

apt-get install phpmyadmin

You will be prompted to select from Apache or Httpd, skip the selecting by press “Enter” on you keyboard as you are running the Nginx.  Move the control dashboard to your website root folder:

ln -s /usr/share/phpmyadmin/ /wwwroot

Then you can access your phpMyAdmin dashboard by over the Web by typing http://yourIP/phpmyadmin/ into browser.

Now complete LEMP stack has been installed to your VPS. Some simple VPS optimization will help you get a more efficient server. You can now add website to your VPS.

Leave a Reply

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

/