How to install LEMP stack (Linux, Nginx, MySQL, PHP-FPM) on CentOS6

A LEMP (Linux, Nginx, MySQL, PHP+Fast-CGI Process Manager) stack is the second most popular after LAMP way set up the web server for hosting web content. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx with PHP support through Fast-CGI Process Manager.

We would like to inform you that we do not provide further support for non-cPanel configurations, so further management should be provided solely at your end.

All commands should be performed under root access on a 'blank' operation system only. You can install clean OS using VPS Management console or IMPI for dedicated servers. Also you can submit a ticket to Hosting – VPS and Dedicated servers department so that our technicians re-install the required OS (in this case it is CentOS6.X) at their end. Below you can see VPS Management panel example:

1. System update and password change
2. Nginx and PHP installation
3. Starting and Configuring Nginx
4. Install MySQL server
5. Add your website or setup server blocks (Nginx virtual hosts)

1. System update and password change

Update the operation system to the latest version using yum update command.

Yum is a CentOS package installer, each time we use it, we see this window. You just need to confirm a download typing y:

If you see Complete! at the end running yum update, everything is okay.

You may want to change the default root password provided in the hosting welcome email. Make sure the password is secure enough and you keep it in safe place. Hostname should be some subdomain you do not plan to use for the website. By default it is set as server1.yourdomain.tld, but you can change it anytime.

Password change is done using passwd command.
Hostname can be checked using hostname command:

In this example we are planning to build website and our hostname is

Reboot the server:

reboot -h now

After the server is rebooted we can check upgraded kernel and CentOS version

uname -r
cat /etc/redhat-release

2. Nginx and PHP installation

Nginx is not included in CentOS6 repository by default, so we install Webtatic Yum repository which is a CentOS/RHEL repository containing updated web-related packages and Extra Packages for Enterprise Linux (or EPEL) first. EPEL for Nginx and Webstatic for newer PHP versions support.

rpm -Uvh
rpm -Uvh

And install Nginx using:

yum install nginx

You will need to install the CentOS RPM signing key that is used to sign packages, just type y:

After it is completed, you should see this message:

Then we will install PHP with MySQL and PHP-FPM support:

At the moment default CentOS 6 repositories include php 5.3.3 version. However, this php version already considered to be outdated, up to date version is 5.6. We suggest checking scripts you are going to install to consider what php version you need.

Thus, running this command we will install PHP 5.3.3, in case you need another one, check other options below:

yum install php php-mysql php-fpm

Successful installation looks like on the screenshot below. After an installation we checked php version installed using:

php -v

Installation of another PHP version

No need to worry in case you need another PHP version. We included Webstatic repository before which contains 5.4-7.0 PHP versions.

Checking with the command yum search php, we can find that new PHP packages like php54w, php55w, php56w even new php70w are also included. Do not forget to install phpXXw-fpm module.

For example, in order  to install PHP5.4 instead of default 5.3 on CentOS6 we run:

yum install php54w php54w-mysql php54w-fpm

The same for other PHP versions which will be relevant both both CentOS versions. 5.5-7.0 versions include bundled Zend Opcache module so it will be reasonable to install it too:

PHP v5.5: yum install php55w php55w-opcache php55w-mysql php55w-fpm

PHP v5.6: yum install php56w php56w-opcache php56w-mysql php56w-fpm

PHP v7.0: yum install php70w php70w-opcache php70w-mysql php70w-fpm

Packages are located outside default CentOS repositories, during the installation you’ll need to import a security key:

The example of successful installation. We installed PHP5.6 here and then checked php version using php -v:

Additonal PHP modules installation

If you find that some required modules missing, you can list available modules using

yum search php

In case you have other than 5.3 PHP version it will be reasonable to exclude modules for other version by changing php to phpXX depends on what version you use. For example, for PHP5.6 you need yum search php56w command.

Yum will show all modules starting from php:

To install a module you need enter:

yum install<php-module>

where is full name of module to install:

To install several modules at once you need to separate the name of each module with a space, like:

yum install php-cli.x86_64 php-common.x86_64 php-dba.x86_64

3. Starting and Configuring Nginx

First we need some modifications to connect configure Nginx with FastCGI.

Open /etc/php.ini PHP main configuration file and find cgi.fix_pathinfo. We can use nano text editor to modify the file:

nano /etc/php.ini

Change cgi.fix_pathinfo=1 to cgi.fix_pathinfo=0

The file is huge, however, you can use these combinations to easily find the requested line: hit Ctrl+V to jump to the next page, Ctrl+W combination allows searching.

Save the file hitting Ctrl+O, Enter and then close it with Ctrl+X.

1 value of cgi.fix_pathinfo will allow searching for the file that is as near to the requested file as possible which in turn may allow non-PHP files to be executed as PHP. It is a possible security risk. 0 causes the PHP interpreter to only try the literal path given and to stop processing if the file is not found. You can check more information here.

Then open Nginx mail configuration file:

nano /etc/nginx/nginx.conf

NGINX can run multiple worker processes, each capable of processing a large number of simultaneous connections. In most cases, running one worker process per CPU core works well. In this example we have a server with 2 CPU cores, so set 2.

Worker connections is a maximum number of connections that each worker process can handle simultaneously. Value between 512 and 1024 is fine:

Nearly every browser supports receiving compressed content so we definitely want to turn that on. Find gzip statement and copy these lines:

gzip on;
gzip_min_length 1000;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

Save and close the file. Then edit php-fpm configuration file:

nano /etc/php-fpm.d/www.conf

First we force php-fpm to listen on Unix socket instead of TCP which should be slightly faster.

listen should be changed to listen = '/var/run/fastcgi.sock'

Then set Nginx user and group as by default it contains Apache settings which we do not need:

Also we set permissions for Unix socket to the same nginx user and set 0660 permissions for it:

Save and close the file.

Start Nginx server and php-fpm service:

service nginx start
service php-fpm start

And enable auto load of both after the server startup:

chkconfig nginx on
chkconfig php-fpm on

In case domain name is pointed to the server you can check Apache start page. Otherwise, you can enter the server IP in browser:

4. Install MySQL server

We use the same Yum installer:

yum install mysql-server

Once done check MySQL version installed and start MySQL service:

mysql -V
service mysqld start
chkconfig mysqld on

MySQL server has its own root user with different password. So we need to set root MySQL password:


As we do not have any MySQL password set yet, you need to hit Enter when it asks for the current root password and set a new one:

Answer y when it prompts you to:

Set root password? [Y/n]
New password: Enter
Re-enter new password: Enter
Remove anonymous users? [Y/n]
Disallow root login remotely? [Y/n]
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]

At the end it you should see Thanks for using MySQL!

You can launch MySQL command line using this command:

mysql -u root -p

And enter MySQL password which you have just set:

Below you can find the most useful MySQL commands.

Create database:

CREATE DATABASE databasename;

databasename should be a database you wish to create. You better not use space in the name. In this example we use wp_357 database and wp user:

Create user and grant it all permissions:

GRANT ALL ON databasename.* TO user@localhost IDENTIFIED BY 'password';

Change databasename, sqluser and password to your own. The command GRANT ALL assigns all privileges (read, delete, modify or add data) to that user for only this database:

Do not forget to save username and password. You will need it to connect your website to MySQL database.

Database you created can be listed using:

show databases;

Type quit to close MySQL console.

5. Add your website or setup server blocks (Nginx virtual hosts)

Basically we need to create a folder to upload a specific website files and tell Nginx where specific host files are located. Replace with your actual domain name. Do this in all part of this tutorial.

Switch to the root directory of the server and create a website folder. Once created, we need to change ownership permissions to the Nginx user (each service has its own user) and set appropriate www folder permissions of 755.

cd /

mkdir -p /var/www/

chown -R nginx:nginx /var/www/

chmod 755 /var/www

In example below we used

Nginx fetches server blocks (virtual hosts) from /etc/nginx/conf.d folder by default. So everything you need is to create a configuration file with your first host there. You can set any name for it, but, to avoid any confusion further it is a good idea to set an appropriate name. For example, website name + conf part. In this example we will create a configuration file for (and alias)

nano /etc/nginx/conf.d/

Below you can see how server block lines look like, paste the lines to the opened file and modify hostname and folder path (we marked fields to modify in green):

  server {
    listen 80;

  location / {
    root /var/www/;
    index index.php index.html index.htm;


# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {

   fastcgi_split_path_info ^(.+\.php)(/.+)$;
if (!-f /var/www/$fastcgi_script_name) {
    return 404;

  include fastcgi_params;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name;
  fastcgi_pass unix:/var/run/fastcgi.sock;


More detailed explanation:

In case we need another hostname to be hosted, we repeat the same: creating the directory, setting correct owner and permissions for it, and creating another configuration file in /etc/nginx/conf.d

Each time modifying anything in /etc/nginx folder we restart nginx:

service nginx restart

That is all! You can place some index file to the folder you created for the website and check the page in browser.

nano /var/www/

Lets create phpinfo() page to check either server block we set and PHP configuration:


We can test some PHP template like Wordpress. Change directory to your website directory which was set previously:

cd /var/www/

Make sure that you delete index.php file with phpinfo() in case you created a test page previously. File can be removed by using:

rm -f index.html

Then run these commands:

tar --strip-components=1 -xvf latest.tar.gz
rm -f latest.tar.gz

Set correct ownership for the website folder again as wget uploaded files with nginx permissions instead of root:

sudo chown -R nginx:nginx /var/www/

Then you can access your website at link like and complete the installation by filling MySQL database details.

More advanced WordPress+Nginx configurations can be checked here. However, this is already not the part of this tutorial, as each CMS has its own configuration.

That’s it! You can build your websites now.

24761 times

Need help? We're always here for you.