How To Install Linux, Nginx, MySQL, PHP (LEMP) stack on Ubuntu

The acronym LEMP refers to first letters of the four components of a solution stack, composed entirely of free and open-source software, suitable for building high-availability heavy-duty dynamic web sites, and capable of serving tens of thousands of requests simultaneously. The LAMP acronym stands for: Linux, Nginx (Engine X), MySQL and PHP.

We are assuming that we already have the Ubuntu operating system installed. The first thing to start with is Nginx installation. Nginx is an open source reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols, as well as a load balancer, HTTP cache, and a web server . The nginx project started with a strong focus on high concurrency, high performance and low memory usage.

First, we need to make sure that our repository information is up to date and then install Nginx

# sudo apt-get update
# sudo apt-get install nginx
# sudo service nginx start

That is all regarding nginx installation. It is running and listening for incoming connections on port 80 now. The next step is to install MySQL. MySQL is very popular, widely used open source relation database management system. To install MySQL, we execute the following command:

# sudo apt-get install mysql-server mysql php5-mysql

It will install the MySQL server and the required libraries in order to establish communication with PHP. During the installation, we will get prompt to enter new MySQL root password. We need to make sure that we use secure password. Once the installation is finished, we need to install MySQL system tables:

# sudo mysql_install_db

The next step is to make sure MySQL installation is secure. This can be done manually by editing the default configuration file. If you feel that you have enough knowledge you can skip the next step and set the configuration manually, otherwise execute the following command:

# sudo /usr/bin/mysql_secure_installation

We will be prompt for the MySQL root password. During the process, we will be asked a lot of question. Basically, every “yes” answer, means increased security, so if we want to make our installation more secure, we should answer “yes” to all questions. After that, the new configuration will be applied, MySQL server process will be restarted and we it will be ready to use and listening for incoming connection on port 3306.

The last step is to install PHP. PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. To install it, execute:

# sudo apt-get install php5-fpm

The next step is to configure php and nginx details so they can communicate each other. First, we need to open /etc/php5/fpm/php.ini file using our favorite editor and change the line: cgi.fix_pathinfo=1 to cgi.fix_pathinfo=0. If we do not change this value to 0, we might have potential security risk. If this is set to 0, the interpreter will only process the exact file path, which is much safer solution.

By default, the php5-fpm daemon will listen on port 9000, but we would rather establish socket level communication, which is faster and safer. To do that, we should open /etc/php5/fpm/pool.d/www.conf in our favorite editor and change 127.0.0.1:9000 to /var/run/php5-fpm.sock. In order to apply the changes, we need to restart php5-fpm:

# sudo service php5-fpm restart

Now we need to tell Nginx how to “talk” to php5-fpm on the socket that we defined earlier. To do that, we open the default virtual host file,/etc/nginx/sites-available/default and make sure that we have the following rule:

# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
  try_files $uri =404;
  fastcgi_pass unix:/var/run/php5-fpm.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

This concludes the LEMP installation. Now we can create test file and put it in our web root directory. We should create file with the following content:

<?php
phpinfo();

Once done with this step, simply request to restart Nginx in order to apply the configuration that we’ve defined above.

# sudo service nginx restart

If we point our web browser to our web server and that file, we should get basic report of our PHP installation and about modules and libraries included.

Try this tutorial on an SSD cloud server for $5.00/month only!

Built for Developers: 512MB RAM, 20GB SSD Disk and 2TB Transfer. 30 second setup.
  • Chirag

    Why not automate this using #EasyEnginennRef: https://github.com/rtCamp/easyenginenncurl -sL rt.cx/ee | sudo bash # install easyenginenee system install # install nginx, php, mysql, postfix nee site create example.com –wp # create example.com and install wordpress on it

    • mnaser

      EasyEngine is a pretty cool stack, it does make for a super easy way to get started.nnWe’ll probably cover it in an upcoming tutorial! ud83dude0a