In this tutorial we will learn how to install (LEMP) Nginx, MySQL or MariaDB and PHP with PHP-FPM on CentOS 6 server. Before starting just a little introduction what is “Nginx”. Nginx is an open-source, light weight high-performance HTTP server packed with lots of great features and that is very low in resource consumption.
This comprehensive guide walks you through the installation while expecting you to run the given commands on the linux machine. if you proceed as a privileged user you can simply run them but if you proceed as a non privileged one, you will proceed by using the commands with
sudo
Prerequisite
Since we have to install MySQL or Maria DB and PHP modules, we need to set up EPEL repository. EPEL (Extra Packages for Enterprise Linux) provides us the extra packages we need to install on our CentOS 6 server.
By default, CentOS 6 does not have these packages as native, so we will enable the EPEL. Using wget, we will download and install the EPEL RPM to enable it.
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
Once we have enabled the EPEL, we can verify it using yum.
yum repolist
We should see a list, where all the EPEL packages were shown. Now lets move forward and install our LEMP stack.
Install Nginx
To serve web pages to our visitors, we will be installing Nginx, a modern and efficient web server. For this we will be using yum package manager to complete the installation.
yum install nginx
after that start nginx from init.d
/etc/init.d/nginx start
OR
service nginx start
Now make the service permanent during boot by below command
chkconfig nginx on
Now we need to open the required ports on our CentOS 6 server to accept the connections. CentOS 6 uses iptables for this purpose, open the ports using iptables utility as shown. We will be adding the rules to ipables file.
- vi /etc/sysconfig/iptables
-
-A INPUT -p udp -m state --state NEW --dport 80 -j ACCEPT -A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT
Type in your server’s IP address or hostname or http://Your_Server_ip
and you will see a default with Nginx output.
If you are not getting the output from the we page, then try stopping the iptables and proceed
Install MySQL / MariaDB
Install MariaDB
Since MariaDB is a MySQL fork of the original MySQL developer Monty Widenius, We will go with MariaDB.
First, add the MariaDB YUM repository entry for RHEL/CentOS 6 systems. Create the file /etc/yum.repos.d/MariaDB.repo.
vi /etc/yum.repos.d/MariaDB.repo
- /etc/yum.repos.d/MariaDB.repo
-
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.1/centos6-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
yum -y install mariadb-server mariadb
After installation we need to start and enable the DB services.
chkconfig --levels 235 mysqld on
service mysqld start
Now we need to configure the Database to complete the installation n
mysql_secure_installation
We will be asked to enter the password for the MySQL root account, since
we haven’t set this yet, so just hit ENTER . Then we will be asked to set that password. we should type Y to set a root password.
To rest of all the questions, the script asks, you should press Y, followed by the ENTER key at each prompt. During this process we will remove anonymous users and the test database, disable remote root logins, and load these new rules MySQL.
Once all set restart MySQL:
/etc/init.d/mysqld restart
Install MySQL
NOTE :This is an optional step, since MariaDB is already installed. If you want to go with MySQL instead of MariaDB, then this for you.
yum install mysql-server
/etc/init.d/mysqld start
make MySQL to auto start when system boot
chkconfig mysql on
Further installation is similar to MariaDB..
mysql_secure_installation
Install PHP And PHP-FPM
By default, php and php-fpm packages are not available on CentOS 6, so we will be adding remi repository to install php packages
Follow the below commands to install remi repository
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -ivh remi-release-6.rpm
Now install php and php-fpm packages using yum.
yum install php php-common php-fpm -y
Due to some security reasons, we need to edit the following file.
vi /etc/php.ini
Find the line, cgi.fix_pathinfo=1, and change the 1 to 0.
cgi.fix_pathinfo=0
If this number is set to 0, the interpreter will only process the exact file path, which minimize the risk.
Now start and enable php-fpm
service php-fpm start
chkconfig php-fpm on
Configure Nginx
We can increase number of workers for Nginx to our cpu value in /etc/nginx/nginx.conf file. we can get this number from “lscpu” command.
Here our value is 2 from the value, hence we set the value to 2.
vi /etc/nginx/nginx.conf
- /etc/nginx/nginx.conf
-
worker_processes 2;
Now save and close the file, edit the “/etc/nginx/conf.d/default.conf” file with your server details as shown below
vi /etc/nginx/conf.d/default.conf
- /etc/nginx/conf.d/default.conf
-
listen x.x.x.x:80; server_name www.codesposts.com
Modify the above lines as per your details, save and close the file.
Now restart Nginx service to make the changes apply
service nginx restart
Configure PHP
We need to change the user from Apache to Nginx in php-fpm configuration file.
vi /etc/php-fpm.d/www.conf
- /etc/php-fpm.d/www.conf
-
; Unix user/group of processes ; Note: The user is mandatory. If the group is not set, the default user's group ; will be used. ; 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
In the above config file, user and group are replaced with “nginx”, instead of “apache”. Now save and close the file and restart php-fpm service
service php-fpm restart
We will create a test file called “codesposts.php” to check the php components. Enter “<?php phpinfo(); ?>” into the below file and save it.
vi /usr/share/nginx/html/codesposts.php
- /var/www/codesposts.com/test.php
-
<?php phpinfo(); ?>
Save and close the file, restart the Nginx service.
service nginx restart
we can visit this web page in a web browser from the below shown URL.
http://<Your_serverIP>/codesposts.php
we should see a web page that has been generated by PHP with information about your server.
With this, we have successfully completed the installation of LEMP with php-fpm on CentOS 6 server.
Thanks for being with us, hope this help you in gaining knowledge and we are always ready to help you. For furthers queries or suggestions, kindly drop a comment below.