How To Install Nginx, Mysql, Php V7 (Lemp) Stack On Centos 7

How lớn Install Nginx on CentOS 7 with PHP-FPM

The Nginx Web hệ thống is a good Alternative for the Apache website Server. While Apache is still the most commonly used web server, Nginx is a Lightweight Web hệ thống design for the high trafficking websites.

Bạn đang xem: How to install nginx, mysql, php v7 (lemp) stack on centos 7

In This Tutorial We are going lớn Learn How to lớn Install Nginx on CentOS 7 with php-fpm enable.

The Nginx Web vps still does not come with the official CentOS 7 repository, But We have several methods available lớn install nginx on CentOS 7.

One method to install nginx using the repository provides by the Nginx itself, another method is khổng lồ use fedora epel repository. And also We can install nginx from the source.

In This this tutorial first we are going lớn install nginx on CentOS 7 using Nginx CentOS 7 repository and configure php-fpm for Nginx website Server.


Install Nginx on CentOS 7 using CentOS Nginx repository.

The Preferred method to lớn install nginx on CentOS 7 is to use the Software repository Provides by the Nginx. First, we Create the .repo file and then install nginx using yum install command.

Step One
Create Nginx CentOS Repository for CentOS 7

First, create a file called nginx.repo inside the /etc/yum.repos.d directory.

touch /etc/yum.repos.d/nginx.repo

Then, mở cửa the nginx.repo file using a text editor and địa chỉ the following lines.

name=nginx repo




Save the nginx.repo file và run the yum repolist command lớn make sure that the repository is active.

yum repolist

Step Two
yum install nginx

Now we can install centos nginx using yum command.

yum install nginx

After the installation is finished, start và enable nginx using systemctl command.

systemctl start nginx

systemctl enable nginx

Step Three
Allow HTTP Protocol From the Firewall

Next adds firewall rules to allow http service to the outside using firewall-cmd command.

firewall-cmd –permanent –add-service=http

firewall-cmd –permanent –add-service=https

firewall-cmd –reload

Now if typed IP address of your server on the website browser you will get default nginx welcome page.


Location of the Nginx Main Configuration tệp tin in CentOS 7 is /etc/nginx/nginx.conf file.Default Document root is /usr/share/nginx/html/ folder.
Step Four
Install PHP-FPM on CentOS 7

Nginx works with php-fpm which stands for PHP FastCGI Process Manager. We can easily install php-fpm on CentOS 7 using yum command.

yum install php-fpm

php-fpm on CentOS 7 run as a service, so we need lớn start & enable php-fpm using systemctl command.

systemctl start php-fpm

systemctl enable php-fpm

Step Five
Configure PHP-FPM

First xuất hiện the /etc/php-fpm.d/www.conf file & change value of listen from listen = khổng lồ listen = /var/run/php-fpm/php-fpm.sock.

Xem thêm: Điều Kiện Fca Là Term Gì - Điều Kiện Giao Hàng Free Carrier Trong Incoterms

listen = /var/run/php-fpm/php-fpm.sock

Next, open the /etc/php.ini file & set the value of cgi.fix_pathinfo to lớn 0 (uncomment the line if it’s been commented).


Now, Restart the centos php-fpm service using systemctl command.

systemctl restart php-fpm
Step Six
Configure Nginx for PHP-FPM

Open /etc/nginx/conf.d/default.conf file & make sure the following configurations are exist in the server block.


listen 80;

server_name localhost;

root /usr/share/nginx/html;

location /

root /usr/share/nginx/html;

index index.php index.html index.htm;

location ~ <^/>.php(/

And restart the centos nginx web Server.

systemctl restart nginx

Now you can create .php tệp tin with phpinfo() function inside the /usr/share/nginx/html thư mục to demo the centos nginx và php-fpm installation.

Create Nginx Virtual Host

Just like Apache website Server, Nginx Also allows to lớn host multiple trang web on the same server. So Let’s see an example how khổng lồ configure a virtual host on CentOS 7 Nginx website Server.

For This Example our domain name is going to be & the document root of the going khổng lồ be /usr/share/nginx/example directory.

So first create the document root using the mkdir command.

mkdir -p /usr/share/nginx/example

Create the configuration tệp tin for example.con inside the /etc/nginx/conf.d directory with .conf extension.

touch /etc/nginx/conf.d/example.conf

Then add the Following php-fpm Configurations.

server $)

fastcgi_split_path_info ^(.+?.php)(/.*)$;

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

Finally, Save the configuration file & restart the centos nginx website server.

systemctl restart nginx

Now we can service our website from the /usr/share/nginx/example directory.

So that is how we can install nginx on CentOS 7 using nginx CentOS 7 Repository. Next We Will See How to Install nginx using fedora epel-release.

Install Nginx Web vps on CentOS 7 using epel repository

The Fedora Epel repository also provides nginx for CentOS 7. First, we enable epel release và then install the nginx web vps using yum install command.

yum install epel-release

yum install nginx

Configuration is going to be the same as we did before except for the nginx php configuration for the default server block.

For the mặc định site, you should create a configuration tệp tin inside the /etc/nginx/default.d directory.

touch /etc/nginx/default.d/default.conf

Then địa chỉ cửa hàng the following php configuration.

location ~ <^/>.php(/|$)

fastcgi_split_path_info ^(.+?.php)(/.*)$;

fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

So This is the kết thúc of This Tutorial. We Learned How to lớn Install and Configure Nginx on CentOS 7 Linux with PHP-FPM Enabled.