Setting up a website Ngnix Mysql PHP(LEMP) on CentOS x86&x64

edited August 2014 in Proposed How-Tos
LEMP means Nginx MySQL and PHP installed on Linux. Sometime it also called LNMP. Nginx is a good alternative to Apache webserver for building websites on low memory hosts.

This How-To is telling you to install LEMP by yum, NOT by compiling from their Tarball. It is saving your time and effort.

Here is the steps to how to install, I used the Centos6.5_x86 as the Linux system, All steps are running as root:

1. Preparation
The default CentOS repositories do not have Nginx so we need to add the Atomic Corp. repository. Adding this repository is easy.

[root@vultr ~]# wget -qO - http://www.atomicorp.com/installers/atomic | sh

It will show their agreement on-screen , accept it will install the repo for you.

Then run below command to check-update

[root@vultr ~]# yum check-update

2. Nginx installation
Nginx on Atomic Corp. repository is available on version 1.6.0, To install is to run:
[root@vultr ~]# yum install nginx
Answer “y” when the screen appeared some messages. Start the web service Nginx is to run:
[root@vultr ~]# service nginx start
Now open the address http://serverip ,You will see the nginx default welcome page. It tells you the nginx works well. If you web browser can’t show the page. Most of time it is because of the Centos system firewall. So we should run below commands to change the firewall setting.
[root@vultr ~]# vi /etc/sysconfig/iptables
Modify the iptables to add two lines in it. The added two rules must right after the line “-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT” . DO NOT insert them in the end of file. This is very critical. Here is the modified sample. It only shows modified part of it:
#########################################################

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited

#########################################################

After that we must restart iptables by run this,

[root@vultr ~]# service iptables restart
Now try again via your web browser to access http://serverip :

Tips: You can find your VPS public IP from vultr website control panel or use the ifconfig command in SSH.

3. MySQL installation
Mysql on Atomic Corp. repository is available on version 5.5, To install is to run:
[root@vultr ~]# yum install mysql mysql-server
Answer “y” when the screen appeared some messages. Then do a secure installation to set the root password, Then Let's run the mysql:
[root@vultr ~]# service mysqld start
[root@vultr ~]# mysql_secure_installation
Before you run "mysql_secure_installation", you must make sure the mysql is running on your CentOS system. If it prompts “Enter current password for root (enter for none);” Please press Enter key to continue. After set the root password for mysql, You could restart the mysql is to run:
[root@vultr ~]# service mysqld restart

4. PHP installation
Run command below to install PHP and Some PHP components:
[root@vultr ~]# yum install php php-fpm
[root@vultr ~]# yum install php-mysql php-gd libjpeg-turbo-devel libjpeg-turbo-static php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel

5. Nginx Configuration
Firstly, We could set the nginx mysql and php will run automatically after booting the VPS each time. To set is to run:
[root@vultr ~]# chkconfig nginx on
[root@vultr ~]# chkconfig mysqld on
[root@vultr ~]# chkconfig php-fpm on

The nginx configuration file is available here: /etc/nginx/nginx.conf

One thing about this file is you could modify access log setting. In my experience, if you don’t keep it default, the error.log will be very big, It may reach 10G. Here is the modified sample of it, It only shows modified part of the whole nginx.conf :
#########################################################

#error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

#########################################################

To modify the default site example:
[root@vultr ~]# vi /etc/nginx/conf.d/default.conf
Two places you should change, one is the index setting(Line 14), another one is set the php works together with nginx(Line 40-46). Be careful on Line 44 content!! Here is the modified sample of it, It only shows modified part of it:
##########################################################
#
#
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
#
#
#
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#
#
#
##########################################################

After change it, Remember to restart the nginx:
[root@vultr ~]# service nginx restart

6. PHP-fpm configuration
The php-fpm configuration file is available here: /etc/php-fpm.d/www.conf
The default setting is running user is apache, We could change it to user:nginx and group:nginx (Line 39 and 41).
[root@vultr ~]# vi /etc/php-fpm.d/www.conf
##########################################################


; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

##########################################################

After change it, Remember to restart the php-fpm:
[root@vultr ~]# service php-fpm restart
Now let’s test if php is working well, To create php test page is to run:
[root@vultr ~]# vi /usr/share/nginx/html/info.php
Add following 3 lines and save the file:
##########################################################
<?php

phpinfo();
?>
##########################################################
When you copy it, please maunally delete the unnecessary part after "php" in the first line.

Go to http://serverip/info.php in your web browser and you’ll see the php infomation:

7. phpMyAdmin installation:
This step is optional, you can choose to run it or not. on Atomic Corp. repository phpMyAdmin is available, to install is to run:
[root@vultr ~]# yum install phpmyadmin
The default installation is /usr/share/phpMyAdmin, but our default website root directory is /usr/share/nginx/html, To fix it is to create a symbolic link between phpMyAdmin and your site’s directory. Be carefully, the letter is case sensitive.
[root@vultr html]# ln -s /usr/share/phpMyAdmin/ /usr/share/nginx/html/phpmyadmin
Finally you can access phpMyAdmin via http://serverip/phpmyadmin . If there is a error, try to refresh it via your web browser. Finally It will appear a window, input username (default is root) and password (We set it on step 3)

Done!

All steps are tested successfully on CentOS6_i386 and CentOS6_X64 of vultr VPS. Good luck to you!

BTW: This How-To is also applicable in CentOS5_i386 and CentOS5_x64. Only difference is the changing iptables setting on the step 2. Here is the modified sample /etc/sysconfig/iptables on CentOS5_i386 and CentOS5_x64. It only shows modified part of it:
#########################################################
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

#########################################################
Sign In or Register to comment.