Moving from LAMP to LEMP

Recently I have had a request from an assignment to migrate the current web servers from the traditional LAMP server stack to the newer LEMP stack. The brief from the end customer was to utilise the nginx web server rather than staying with the traditional apache server.

I thought I'd share my thoughts here.....

For the purpose of this exercise I am using CentOS 7 and Ubuntu 14.04 systems.

Note: I won't be sharing any configuration needed for Nginx, MySQL or MariaDB.

Note: MariaDB is needed for CentOS 7 as MySQL is no longer supported. (but that's another story).

What is LAMP and LEMP?

LAMP according to wikipedia

    LAMP is an archetypal model of web service solution stacks, named as an acronym of the names of its original four open-source components: the Linux operating system, the Apache HTTP Server, the MySQL relational database management system (RDBMS), and the PHP programming language.

LEMP

    Is very similar, however it makes use of an nginx web server rather than Apache. Likewise, as MySQL is no longer supported on some releases of Linux MariaDB is now being deployed in it's place.

System Setup

Before we get started we need to ensure the system is up-to-date and running at the latest version. On the command-line we run:

  • For CentOS 7, we run:
    # yum update
  • For Ubuntu 14.04, we run:
    # apt-get update

Install Nginx

The quickest and easiest way to install Nginx is from the respective OS repositories

  • For Ubuntu we firstly need to stop Apache (if running):
    # sudo service apache2 stop
    Next we install the nginx service:
    # apt-get install nginx
  • Under CentOS the quickest and easiest way to install Nginx is from the EPEL repository:
    # sudo yum install epel-release
    # yum update
    # yum install nginx

Configure Nginx

  • Under CentOS after installation Nginx needs to be enabled and started in systemd. You can do this with the systemctl command:
    # systemctl enable nginx.service
    # systemctl start nginx.service
  • Under Ubuntu, Nginx is configured to run out-of-the-box, so there is no need to start the service.

Test Nginx service

To confirm that the Nginx server is running we can type your server's IP address into a new browser window using the url http://localhost:80 (replace localhost with the hostname or IP address of your server).

default nginx message

Install and Configure PHP Processing

Nginx does not have built in PHP processing so we need to implement PHP in order to allow Nginx to properly handle and parse PHP code.

Deploying PHP with FastCGI under CentOS

We need to deploy PHP-Fast-CGI under Centos. You can install this via YUM from the EPEL repository that was previously installed:

# yum install php-cli php spawn-fcgi

Once PHP-Fast0CGI is installed, you will need to create a script to start and control the php-cgi process. Create a file /usr/bin/php-fastcgi using your favourite text editor and paste the following lines into the file:

#!/bin/sh
if [ `grep -c "nginx" /etc/passwd` = "1" ]; then 
    FASTCGI_USER=nginx
elif [ `grep -c "www-data" /etc/passwd` = "1" ]; then 
    FASTCGI_USER=www-data
elif [ `grep -c "http" /etc/passwd` = "1" ]; then 
    FASTCGI_USER=http
else 
# Set the FASTCGI_USER variable below to the user that 
# you want to run the php-fastcgi processes as

FASTCGI_USER=
fi

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u $FASTCGI_USER -f /usr/bin/php-cgi

Once the file has been created, you need to make the script executable:

# chmod +x /usr/bin/php-fastcgi

You can then run the script manually, or setup a systemd service

Deplying PHP under Ubuntu

Firstly, download a package named php5-fpm

# apt-get install php5-fpm

Using your favourite text editor edit the file /etc/php5/fpm/php.ini. This is a large configuration file with lots of comments (;)

Locate the line

;cgi.fix_pathinfo-1

and remove the semi-colon at the begining of the line and change the value to 0, the end reult will look like:

cgi.fix_pathinfo=0

Restart php5-fpm to ensure the changes take effect:

# service php5-fpm restart