Did You Know?
Got a tip? Let us know

HostGator Diwali Mega Offer 50% Flat Discount on all Hosting Plans - Grab it Now

Install LEMP (Linux, Nginx, MySQL 5.5.29, PHP 5.4.11) on RHEL/CentOS 5-6 & Fedora 18-12

Download Your Free eBooks NOW - 10 Free Linux eBooks for Administrators
CW

This howto guide explains more deeply on how to install LEMP (Linux, Nginx, MySQL, PHP) on pre-installed Linux servers and installation instructions can be used on various Linux distribution such as RHEL 6.2/6.1/6/5.8, CentOS 6.2/6.1/6/5.8 and Fedora 12,13,14,15,16,17,18 servers using Remi and Nginx repositories with YUM command.

In this guide we use latest MySQL 5.5.29 and PHP 5.4.11 versions with PHP-FPM modules with Nginx. So, before moving further for installation let’s discuss about Nginx and PHP-FPM.

Install Nginx MySQL PHP in Linux

Setup LEMP Stack in Linux

What is Nginx?

Nginx (Engine X) is open source robust light and high performance Web server, reverse proxy sever and also mail proxy server for HTTP, SMTP, POP3 and IMAP protocols. To know more about features visit http://wiki.nginx.org/Overview

What is PHP-FPM?

PHP-FPM stands for FastCGI Process Manager is an alternative PHP FastCGI implementation with some additional useful features for heavily loaded websites. For more information visit http://php-fpm.org/

Install LEMP (Linux, Nginx, MySQL, PHP) on RHEL 6.2/6.1/6/5.8, CentOS 6.2/6.1/6/5.8 and Fedora 12,13,14,15,16,17,18

Step 1: Installing Remi Repository

## Install Remi Repository on Fedora 18, 17, 16, 15 ##
# rpm -Uvh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm 
# rpm -Uvh http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-stable.noarch.rpm

## Fedora 18 ##
# rpm -Uvh http://rpms.famillecollet.com/remi-release-18.rpm

## Fedora 17 ##
# rpm -Uvh http://rpms.famillecollet.com/remi-release-17.rpm

## Fedora 16 ##
# rpm -Uvh http://rpms.famillecollet.com/remi-release-16.rpm

## Fedora 15 ##
# rpm -Uvh http://rpms.famillecollet.com/remi-release-15.rpm

## Fedora 14 ##
# rpm -Uvh http://rpms.famillecollet.com/remi-release-14.rpm

## Fedora 13 ##
# rpm -Uvh http://rpms.famillecollet.com/remi-release-13.rpm

## Fedora 12 ##
# rpm -Uvh http://rpms.famillecollet.com/remi-release-12.rpm

## Install Remi Repository on RHEL/CentOS 6.3-6.0 ##
# rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm 

## Install Remi Repository on RHEL/CentOS 5.4-5.0 ##
# rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm

Step 2: Installing Nginx Repository

Nginx repository only needed in RHEL/CentOS distributions. So, create a file called /etc/yum.repos.d/nginx.repo and add the following lines to it.

For RHEL 6.3/6.2/6.1/6/5.8

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
gpgcheck=0
enabled=1

For CentOS 6.3/6.2/6.1/6/5.8

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

Step 3: Installing Ngnix, MySQL 5.5.29, PHP 5.4.11 & PHP-FPM

## Installing on Fedora 12, 13, 14, 15, 16, 17, 18 ##
# yum --enablerepo=remi install nginx mysql mysql-server php php-common php-fpm

## Installing on RHEL/CentOS 5-6 ##
# yum --enablerepo=remi,remi-test install nginx mysql mysql-server php php-common php-fpm

Step 4: Installing PHP 5.4.11 Modules

## Installing on Fedora 12, 13, 14, 15, 16, 17, 18 ##
# yum --enablerepo=remi install php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo

## Installing on RHEL/CentOS 5-6 ##
# yum --enablerepo=remi,remi-test install php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-apc php-cli php-pear php-pdo

Step 5: Stopping Apache Service

Apache and Nginx both services are bind to same port (i.e. 80). So, you must stop turn-off Apache in order to use Nginx.

# chkconfig --levels 235 httpd off
# /etc/init.d/httpd stop

Step 6: Starting/Stopping Nginx MySQL and PHP-FPM

## Enable Nginx, MySQL and PHP-FPM on Boot ##
# chkconfig --add nginx
# chkconfig --add mysqld
# chkconfig --add php-fpm

## Enable Nginx, MySQL and PHP-FPM on Run Levels ##
# chkconfig --levels 235 nginx on
# chkconfig --levels 235 mysqld on
# chkconfig --levels 235 php-fpm on

## Nginx Startup Commands ## 
# /etc/init.d/nginx start
# /etc/init.d/nginx stop
# /etc/init.d/nginx status

## MySQL Startup Commands ## 
# /etc/init.d/mysqld start
# /etc/init.d/mysqld stop
# /etc/init.d/mysqld status

## PHP-FPM Startup Commands ##
# /etc/init.d/php-fpm start
# /etc/init.d/php-fpm stop
# /etc/init.d/php-fpm status

Step 7: Configuring Nginx and PHP-FPM

Creating Website Directory

Creating a directory structure for your website under /srv/www/. In my case I used www.tecmint.com. This way you can create for your real site.

## public_html directory and logs directory ##
# mkdir -p /srv/www/tecmint/public_html
# mkdir /srv/www/tecmint/logs
# chown -R nginx:nginx /srv/www/tecmint

Creating Website Logs

Creating log directories under /var/log.

## public_html directory and logs directory ##
# mkdir -p /srv/www/tecmint/public_html
# mkdir -p /var/log/nginx/tecmint
# chown -R nginx:nginx /srv/www/tecmint
# chown -R nginx:nginx /var/log/nginx

Configuring Virtual Host Directories

Creating virtual host directoires under /etc/nginx.

# mkdir /etc/nginx/sites-available
# mkdir /etc/nginx/sites-enabled

Add following line of code to /etc/nginx/nginx.conf file at the end, before closing (http block) tag.

## Load virtual host conf files. ##
include /etc/nginx/sites-enabled/*;

For reference see below red line of code, how i added to the file /etc/nginx/nginx.conf.

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Creating Virtual Host file for Tecmint

Add the following lines of code to /etc/nginx/sites-available/tecmint file. This is a basic virtual host config file.

server {
    server_name tecmint;
    access_log /srv/www/tecmint/logs/access.log;
    error_log /srv/www/tecmint/logs/error.log;
    root /srv/www/tecmint/public_html;

    location / {
        index index.html index.htm index.php;
    }

    location ~ .php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/tecmint/public_html$fastcgi_script_name;
    }
}

Linking Tecmint Virtual Host

Linking tecmint virtual host to /etc/nginx/sites-enabled.

# cd /etc/nginx/sites-enabled/
# ln -s /etc/nginx/sites-available/tecmint

Restarting Nginx Service

We need here restart for Nginx service to reflect all the configuration which we have done above.

# /etc/init.d/nginx restart

Adding Virtual Domain to Host File

Add the newly created virtual host domain to your local host file /etc/hosts.

127.0.0.1      localhost.localdomain localhost tecmint

Step 8: Testing Nginx, MySQL, PHP and PHP-FPM

Create a file called phpinfo.php under /srv/www/tecmint/public_html/ and add the following lines of code to it. For example (/srv/www/tecmint/public_html/phpinfo.php).

<?php

     phpinfo ();
?>

Now access the page with web browser using http://tecmint/phpinfo.php and verify all versions.

That’s It! for now, If you’re having any problems in installation and configuration, please feel free to ask your questions using our comment form below.

Ravi Saive

Owner at TecMint.com
Simple Word a Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux.

Linux Services & Free WordPress Setup

Our post is simply ‘DIY’ aka ‘Do It Yourself, still you may find difficulties and want us to help you out. We offer wide range of Linux and Web Hosting Solutions at fair minimum rates. Please submit your orders by Clicking Here.

37 Responses

  1. Gary says:

    In step 7, why some of commands in “Creating Website Directory” and “Creating Website Logs” such as mkdir -p /srv/www/tecmint/public_html & chown -R nginx:nginx /srv/www/tecmint are duplicate?

  2. ajay says:

    YUM SERVER & DNS SERVER configuration on RHEL 6

  3. Ravi says:

    Hi,

    I am unable to install php-fpm on centos 6.2. getting below mentioned error.

    [root@localhost home]# yum –enablerepo=remi,remi-test install nginx mysql mysql-server php php-common php-fpm
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * base: centos.01link.hk
    * extras: centos.01link.hk
    * updates: centos.01link.hk
    Setting up Install Process
    Package nginx-1.2.5-1.el6.ngx.x86_64 already installed and latest version
    Package mysql-5.1.66-1.el6_3.x86_64 already installed and latest version
    Package mysql-server-5.1.66-1.el6_3.x86_64 already installed and latest version
    Package php-5.3.3-14.el6_3.x86_64 already installed and latest version
    Package php-common-5.3.3-14.el6_3.x86_64 already installed and latest version
    No package php-fpm available.
    Nothing to do

    • Ravi Saive says:

      @Ravi

      You have not added remi repository. see the above errors the mirrors loading from CentOS base. Install remi repository and try again.

  4. Yafanz says:

    # chkconfig –add mysqld
    error reading information on service mysqld: No such file or directory

    # /etc/init.d/mysqld start
    bash: /etc/init.d/mysqld: No such file or directory

    # /etc/init.d/php-fpm start
    Starting php_fpm /etc/init.d/php-fpm: line 44: /usr/local/bin/php-cgi: No such file or directory
    failed

  5. Ravi Saive says:

    Have you installed MySQL and php-cgi packages correctly? If not, install them correctly. Because the errors clearly saying you don’t have those files.

  6. Yafanz says:

    i have installed mysql

    # rpm -qa mysql\*
    mysql-libs-5.5.29-1.fc18.x86_64
    mysql-server-5.5.29-1.fc18.x86_64
    mysql-5.5.29-1.fc18.x86_64

    how to install php-cgi ??

    • Ravi Saive says:

      Please use the below command is being used for latest fedora 18/17 onwards to start services.

      # systemctl start mysqld.service 
      
  7. Yafanz says:

    how to fix error 502 Bad Gateway???
    nginx , php-fpm status is enable and active.

  8. Dabney says:

    http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

    The 6-7 release is no longer active via the link in the first section of this post. Just a heads up in case someone encounters a 404 when attempting to install the RPM.

  9. Cristian says:

    hello, I have the following problem in php-fpm. I set up nginx + php-fpm + mysql and it occurs, the processes of php-fpm are always running on Linux, even after the client close the connection, the processes remain active, the only way to solve it by restarting the service php -fpm. Did this happen to you?

  10. Dev says:

    Sir can you tell me the default web directory…….
    Were I will just copy and paste my html and php file that can browsable from other clients to view that web page.

  11. David Nguyen says:

    Hi Ravi,

    Can Nginx run both php 5.2 and 5.4 ?

    Thanks,

  12. Tapash Datta says:

    Hi Ravi,
    Thank you for great work. I am a beginner with linux and planning to host a website from my homeserver on CentOS. I Have followed both of your tutorial on Nginx + PHP & Phpmyadmin. However I am not able to get the phpmyadmin screen. I would also like to install drupal on it. I have created 2 vhost, drupal & phpmyadmin. But only able to get the drupal installation screen. phpmyadmin vhost gives 404 error. Would you please be kind to make tutorial for CentOS+ Nginx+PHP+ MysQL+ Phpmyadmin+ drupal on a combined vhost file please? I would be very grateful to you. I am sure it will be useful for many others like me…. Thanks again

    • Ravi Saive says:

      OK noted your requirement, will write up on this soon, if we get time. stay tuned for this.

      • Corey Jones says:

        This is exactly the problem im having too. A tutorial for that would be great. I’m hoping this will be for 6.4? Thanks for the tutorials though they are great.

  13. Daniel says:

    Hey Ravi,

    Thanks for the tutorial. I have a little problem at the end of it.

    When I try to access http://my-website-name.com/phpinfo.php I get a “404 Not Found” error.
    Could this be because i wrote “my-website-name” instead of “my-website-name.com” evertime?
    http://i.imgur.com/R1RiRPY.png

  14. vikas027 says:

    Excellent work. Keep it up.

  15. Ashiq says:

    Hello Ravi,

    While accessing http://tecmint/phpinfo.php , its shows 404 error.What maybe the problem.I checked ownership permissions and all. But accessing techmint or localhost, it says “Welocome to nginx………………….”

  16. hello says:

    Hi Ravi,
    I installed LEMP with this document.
    But I can’t Install MariaDB.
    ( http://www.tecmint.com/install-mariadb-in-linux/ )

    How can I solve this?
    Thank you.

    CentOS 6.3 64bit.
    Error Summary is below.

    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * base: data.nicehosting.co.kr
    * epel: http://ftp.neowiz.com
    * extras: data.nicehosting.co.kr
    * remi: remi.kazukioishi.net
    * remi-test: remi.kazukioishi.net
    * updates: data.nicehosting.co.kr
    Setting up Install Process
    Package nginx-1.4.4-1.el6.ngx.x86_64 already installed and latest version
    Package mysql is obsoleted by MariaDB-client, trying to install MariaDB-client-5.5.34-1.x86_64 instead
    Package mysql-server is obsoleted by MariaDB-server, trying to install MariaDB-server-5.5.34-1.x86_64 instead
    Package php-5.4.23-1.el6.remi.x86_64 already installed and latest version
    Package php-common-5.4.23-1.el6.remi.x86_64 already installed and latest version
    Package php-fpm-5.4.23-1.el6.remi.x86_64 already installed and latest version
    Resolving Dependencies
    –> Running transaction check
    —> Package MariaDB-client.x86_64 0:5.5.34-1 will be installed
    –> Processing Dependency: MariaDB-common for package: MariaDB-client-5.5.34-1.x86_64
    —> Package MariaDB-server.x86_64 0:5.5.34-1 will be installed
    –> Running transaction check
    —> Package MariaDB-common.x86_64 0:5.5.34-1 will be installed
    –> Processing Dependency: MariaDB-compat for package: MariaDB-common-5.5.34-1.x86_64
    –> Running transaction check
    —> Package MariaDB-compat.x86_64 0:5.5.34-1 will be installed
    –> Finished Dependency Resolution

    Dependencies Resolved

    ===================================================================================================================================================================
    Package Arch Version Repository Size
    ===================================================================================================================================================================
    Installing:
    MariaDB-client x86_64 5.5.34-1 mariadb 10 M
    MariaDB-server x86_64 5.5.34-1 mariadb 34 M
    Installing for dependencies:
    MariaDB-common x86_64 5.5.34-1 mariadb 23 k
    MariaDB-compat x86_64 5.5.34-1 mariadb 2.7 M

    Transaction Summary
    ===================================================================================================================================================================
    Install 4 Package(s)

    Total size: 47 M
    Installed size: 195 M
    Is this ok [y/N]: y
    Downloading Packages:
    Running rpm_check_debug
    Running Transaction Test

    Transaction Check Error:
    file /usr/share/mysql/czech/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/danish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/dutch/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/english/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/estonian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/french/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/german/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/greek/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/hungarian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/italian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/japanese/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/korean/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/norwegian-ny/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/norwegian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/polish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/portuguese/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/romanian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/russian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/serbian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/slovak/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/spanish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/swedish/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/ukrainian/errmsg.sys from install of MariaDB-server-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /etc/my.cnf from install of MariaDB-common-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64
    file /usr/share/mysql/charsets/Index.xml from install of MariaDB-common-5.5.34-1.x86_64 conflicts with file from package mysql-libs-5.5.35-1.el6.remi.x86_64

    Error Summary
    ————-

  17. Ramos says:

    Hello,

    Nginx is perfectly working in my server with your help…thanks for that…..

    I think the nginx is working is based on the reverse proxy concept…. So it needs two or more machines to setup and understand the actual working of nginx…,…..

    1. So, is this configuration is for proxy server or proxy client machine…………? Or Do we have to install nginx on both machines..?

    2. In which configuration file, will we mention the proxy client details…… Is it in Virtual host file?

    Thank you

  18. fedorian says:

    Hello Ravie. Great tutorial here. Thank’s.

    I was success installed nginx and sql by the following howtoforge tutorial and try to install wp on it but i have 403 error Forbidden when go to http://wordpress/. i has ckecked my code, and i am sure it correctly.

    Please advice me.

  19. Kyle says:

    Hi,
    I installed nginx and php follow above tips, the nginx was ok and I can see the “Welcome to nginx!” site. But when I visit the php site, the browser download the php file. How can I fix this issue?

  20. Not work! says:

    I did exactly what it says several times and I do not phphinfo displays. Only The webpage cannot be found.
    My system is VPS, CentOS 6, nginx, mysql, php.

  21. David says:

    How to install LEMP on CentOS 7 x86_64 ?

Leave a Reply

This work is licensed under a (cc) BY-NC | TecMint uses cookies. By using our services, you comply to use of our cookies. More info: Privacy Policy.
© 2012-2014 All Rights Reserved.