How to install Nginx, PHP-FPM, MySQL on CentOS 6/7

Sean78Black

Member
Joined
May 29, 2016
Messages
84
Points
8
Nginx is a popular webserver behind Apache, you can easily install Nginx using PHP and MySQL with PHP-FPM on CentOS 5/6/7. Unlike Apache, Nginx does not have the ability to handle PHP as Apache's mod_php. However, Nginx support module to communicate with PHP as FastCGI handler. This thread will guide you to install Nginx runs PHP-FPM running unix socket and MySQL on CentOS 6/7.

Note: The following steps are based on CentOS 6 guidelines, it totally works on CentOS 7. However, with some commands such as service and chkconfig, you should use systemctl instead as follows:
For CentOS 7, you should use the command to launch systemctl Nginx, PHP-FPM and MySQL:
Code:
systemctl start nginx
systemctl start php-fpm
systemctl start mysqld
Replace start to restart or stop to restart and stop Nginx, PHP-FPM ...
To restart Nginx, PHP-FPM and MySQL, using the chkconfig command instead of systemctl as following:

Code:
systemctl enable nginx
systemctl enable php-fpm
systemctl enable mysqld
Install the latest Nginx from Nginx repository
Nginx is not available on CentOS but Nginx provides installation package for CentOS through repository expansion. To install the latest version of Nginx, you need to add this repository into CentOS.
Creat repository nginx.repo cho Nginx:
Code:
# nano /etc/yum.repos.d/nginx.repo
Go to the Official site of Red Hat/CentOS packages and copy the code for CentOS on nginx.repo
Code:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
Replace $releasever with CentOS version you are using (CentOS 5.x is 5, CentOS 6.x is 6, CentOS 7.x is 7)

You can install Nginx now with the following command:
Code:
# yum install nginx
Use the following command to check the version of Nginx installed
Code:
# nginx -v
nginx version: nginx/1.8.0
Launch Nginx with the following command:
Code:
# service nginx start
Starting nginx: [OK]
Let Nginx start automatically when your CentOS restarts, use the following command:
Code:
# chkconfig --level 235 nginx on
So you've installed the latest version of Nginx on CentOS. Now you need to configure Nginx to run PHP through PHP-FPM.

Configurate Nginx to run PHP through PHP-FPM

To Configurate Nginx run PHP-FPM, you must edit virtual host in file
Nginx configuration for running PHP-FPM, you need to edit the config file /etc/nginx/conf.d/default.conf.

Code:
# nano /etc/nginx/conf.d/default.conf
Navigate to the following paragraph and delete the # at the front end of each line to PHP works with Nginx:

Code:
# location ~ \.php$ {
#     root           html;
#     fastcgi_pass   127.0.0.1:9000;
#     fastcgi_index  index.php;
#     fastcgi_param  SCRIPT_FILENAME /scripts$fastcgi_script_name;
#     include        fastcgi_params;
# }
And revise “/scripts” to “$document_root” as following:
Code:
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
to
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
After that, you will have a config file as following:
Code:
location ~ \.php$ {
        root           html;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
Next you need to install PHP-FPM and configured to process PHP.

Install PHP-FPM

First, you need to select the version of PHP that you want to install. To check the version of PHP-FPM is currently on CentOS, use the following command:
Code:
# yum info php-fpm 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.sitbv.nl
 * extras: mirror.netrouting.net
 * updates: mirrors.supportex.net
Available Packages
Name        : php-fpm
Arch        : x86_64
Version     : 5.3.3
Release     : 46.el6_6
Size        : 1.1 M
Repo        : updates
Summary     : PHP FastCGI Process Manager
URL         : http://www.php.net/
License     : PHP
Description : PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI
            : implementation with some additional features useful for sites of
            : any size, especially busier sites.

If you want to install PHP version as 5.4, 5.5, 5.6 or later, see the instructions to install the latest PHP on CentOS 6/7.

Install PHP-FPM and modules that you need:

Code:
# yum install php-fpm php-cli php-mysqlnd php-gd php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-soap
To install APC for PHP, you need to install php-pecl-apc package.

Start PHP-FPM
Code:
# service php-fpm start
Starting php-fpm:                                          [  OK  ]
To make PHP-FPM start automatically when your system restarts CentOS, use the following command:
Code:
# chkconfig --levels 235 php-fpm on
Config PHP-FPM to use for Nginx

If you want to use Unix sockets instead of TCP connections to increase performance as the above, you need to reconfigure PHP-FPM in file /etc/php-fpm.d/www.conf.

Code:
# nano /etc/php-fpm.d/www.conf
Find and change the value of "listen =" to "fastcgi_pass" that you have configured for Nginx above:
Code:
listen = 127.0.0.1:9000
to
listen = /var/run/php5-fpm.sock
Find and replace the value to nginx (remove “;” before each line)
Code:
;listen.owner = nobody
;listen.group = nobody
user = apache
group = apache
After changed values, you will get these lines:
Code:
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx
I would explain the change of the configuration above. PHP-FPM when booting, /var/run/php5-fpm.sock socket file will be created under the (permission) of users and groups is indicated in "listen.owner" and "listen.group". Only new users and groups are allowed to communicate with the socket file. If you want to use PHP-FPM Nginx via unix socket, you must set permissions for the socket file. That is why you need to change that position.

If you do not set permissions for Nginx, you'll get an error "502 Bad Gateway" because Nginx not allowed to communicate with socket file..
An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.
If you are the system administrator of this resource then you should check the error log for details.
Faithfully yours, nginx.
So the configuration for PHP-FPM use Nginx is done, now you need to restart Nginx and PHP-FPM:
Code:
# service php-fpm restart
# service nginx restart
Test Nginx can run PHP-FPM or not
To check if you're running PHP-FPM Nginx use or not, you just see the server's phpinfo information.
Create a file in the root directory phpinfo.php your:
Code:
# nano /usr/share/nginx/html/phpinfo.php
With the following content:
Code:
<?php phpinfo(); ?>
Now visit your_IP_address/phpinfo.php and check, if you see in the "Server API" is FPM/FastCGI, then you have succeeded.

If you get an error "File not found.", It may be because the wrong name or configuration file follows the root directory of Nginx.
Open Nginx configuration file and edit the root:
Code:
# nano /etc/nginx/conf.d/default.conf
    location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
Install and configure security for MySQL
MySQL is a popular database management, you can easily install and use. You can also install MariaDB on CentOS alternative to MySQL for better performance. Use the following command to install MySQL on CentOS:
Code:
# yum install mysql mysql-server
Restart MySQL:
Code:
# service mysqld start
Let MySQL start automatically when your system restarts CentOS, use the following command:
Code:
# chkconfig --levels 235 mysqld on
When MySQL was first boot, you may see the following message:

Code:
Initializing MySQL database: Installing MySQL system tables…
150917 3:28:42 [Note] /usr/libexec/mysqld (mysqld 5.5.45) starting as process 11970 …
OK
Filling help tables…
150917 3:28:42 [Note] /usr/libexec/mysqld (mysqld 5.5.45) starting as process 11977 …
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password ‘new-password’
/usr/bin/mysqladmin -u root -h production.vn password ‘new-password’
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems at bugs.mysql.com
This message above is remind you to create password for root user. To create password fo Mysql, using mysql_secure_installation

Code:
# mysql_secure_installation
In the line “Enter current password for root (enter for none):” press Enter to set password for root
Code:
# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: --> your password
Re-enter new password: --> retype your password
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!

Cleaning up...


All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!
So you've completed the install and secure MySQL. You can configure to optimize and improve security for MySQL if it is necessary. Use Nginx, PHP-FPM and MySQL for performance and speed up loading of web pages with mostly static content such as images, videos or documents. But if your site mainly running PHP and dynamic content, maybe you should use Apache and PHP-FPM to have a better performance.

If you have any extra note, don't forget to add below. I am looking forward to reading all your comments. Thank you for reading, wish you success!
 

anhbloginc

Member
Joined
Jun 24, 2015
Messages
101
Points
18
Do you have any step by step tutorial to guide us to install ngnix as a proxy reserve which stand front the Apache? I heard a lot about this.
 
Older threads
Latest threads
Replies
1
Views
86
Replies
1
Views
176
Replies
4
Views
385
Replies
11
Views
526
Replies
2
Views
229

Latest postsNew threads

Referral contests

Referral link for :

Sponsors

Popular tags

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top