Setup LEMP on Ubuntu

< Back to the Title Topic

Setup LEMP on Ubuntu

LEMP is an acronym for Linux, Nginx (Engine x), MySQL, and PHP. It is needed to run web servers due to it containing various software required for other web content management software, such as WordPress and also Joomla.

LEMP is similar to LAMP, where LAMP uses Apache instead of Nginx as open-source web servers. Apache is usually chosen due to its widespread support, while Nginx is chosen due to its effectiveness under load. There’s an article on setting up LAMP on CentOS 6 here if you prefer to use Apache instead of Nginx.

One big difference between Apache and Nginx is the actual way that they handle connections and traffic. This provides perhaps the most significant difference in the way that they respond to different traffic conditions.

Step 1: Update System

First, update your system by typing:

sudo apt-get update

If you get an error regarding the locked directory, first find out the process ID by typing: 

ps -A | grep apt-get

Then, kill the process by typing:

sudo kill -9 <processID>

Run the update once again, it should work now.

If your update process gets stuck at [Waiting for Header], access your source list by typing:

sudo nano /etc/apt/sources.list

Then, copy the following lines on top of the file:

deb mirror:// precise main restricted universe multiverse
deb mirror:// precise-updates main restricted universe multiverse deb mirror:// precise-backports main restricted universe multiverse deb mirror:// precise-security main restricted universe multiverse

Save and exit, then retry the update.

Note: If the update doesn’t work (such as giving errors), although not recommended, you may skip this step and go to Step 2.

Step 2: Install Nginx

Type the following to install the Nginx web server:

sudo apt-get install nginx

Now type the following to check if Nginx is running:

service nginx status

If it’s not, type this to force it to start:

sudo service nginx start

Check the service status again, if it’s running, check it by typing your server’s IP address inside your browser’s URL. If you don’t know which IP to type, use the following command:

ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s/\/.*$//’

The command will give you the public IP of your server to access your web server’s welcome page, which should look something like this.

Note: Consider flushing your IPtables if you think it is preventing you from viewing the welcome page, using:

sudo iptables -F

Step 3: Install MySQL

Install MySQL by typing:

sudo apt-get install mysql-server

You will be prompted to enter a password for accessing the root account of MySQL, do it and repeat it to confirm your password. Wait for the installation to finish.

MySQL is now installed, but we still need to configure it before it becomes functional. Type in the following:

sudo mysql_install_db

sudo mysql_secure_installtion

The first line of command tells the system to generate a directory structure for storing our data. The second line allows us to modify some default settings. Type in the root password you have set just now, then skip the change password option if you are fine with your current root password and answer “Y” to the remaining questions.

Congratulations, MySQL is successfully installed.

Step 4: Install PHP

Type in the following command to install the necessary PHP module. PHP is used for connecting Nginx with MySQL, and “fpm” stands for “fastCGI process manager”.

sudo apt-get install php5-fpm php5-mysql

Now we need to configure the php-fpm’s php.ini file. Open it by typing:

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

Search for the phrase “cgi.fix_pathinfo”. Then uncomment it and change its value from 1 to 0.

After that, restart php-fpm service.

sudo service php5-fpm restart

Then we need to create an info.php file for checking PHP status on your server. Create info.php using the following line (path may differ based on your server):

sudo vim /usr/share/nginx/html/info.php

Then add the following lines inside the file:




Save and exit the file.

Step 5: Configure Virtual Host

We now need to configure Nginx’s configuration file so that it will utilize PHP for processing dynamic content. Type in the command below:

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

Change the contents based on the numbers beside the red boxes:

  1. Add index.php under the index line.
  2. Set server_name to your server’s IP or hostname.
  3. Uncomment the section for error_page
  4. Uncomment the “location ~ \.php” section, since we are using php5-fpm, ONLY uncomment the line “fastcgi_pass unix:/var/run/php5-fpm.sock”, DO NOT uncomment “fastcgi_pass;”. According to the picture below,

After that, restart nginx service to apply the changes.

sudo service nginx restart

Now, type “http://your-ip-address/info.php” (without quote marks) into browser URL to view your PHP status page.

Previous Install LEMP on CentOS 7
Next How to install .NET Framework 3.5 using Server Manager