Install Nginx Php Fpm Centos 7
Prerequisites
Before you start installing Nginx and Php-Fpm on CentOS 7, make sure the following prerequisites are in place.
- CentOS 7 must be installed onto your machine.
- Create a new user to run the Nginx web server.
- The firewall should be disabled or opened up to allow access to Nginx server.
- If you are using SELinux, ensure it is in permissive mode.
- Make sure all the packages are up to date.
Now let’s start the installation.
Installing Nginx
Nginx is available in the default CentOS 7 repositories. To install it, issue the following command.
$ yum install nginx
Confirm if you need to install any additional packages and type y to continue.
Is this ok [y/d/N]: y
Once Nginx is installed, start its services and enable it to start automatically on system reboot.
$ systemctl start nginx
$ systemctl enable nginx
You can verify the status of the Nginx service by running the following command.
$ systemctl status nginx
Now check whether Nginx is running successfully by visiting the machine’s IP address in web browser.
Installing PHP-FPM
To install PHP-FPM, issue the following command.
$ yum install php php-fpm
Press y to confirm if you need to install any additional packages.
Is this ok [y/d/N]: y
Now start the PHP-FPM service and make sure it starts automatically on system reboot.
$ systemctl start php-fpm
$ systemctl enable php-fpm
You can verify the status of the PHP-FPM service by running the following command.
$ systemctl status php-fpm
Configure Nginx for PHP-FPM
By default, Nginx uses the /usr/share/nginx/html directory for serving the website files. To configure Nginx to serve PHP, open its configuration file.
$ nano /etc/nginx/conf.d/default.conf
And add the below lines of code at the end of the file.
location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Save the changes and exit the file. Now, create a PHP file for testing the setup. Create a symbolic link of this file in the web document root directory.
$ touch /usr/share/nginx/html/test.php
$ ln -s /usr/share/nginx/html/test.php /var/www/html/test.php
Then, open the test.php file and add the following code to it.
phpinfo();
?>
Save the file and restart the Nginx service.
$ systemctl restart nginx
Now open the URL http://your_server_ip/test.php in your web browser. If you see the output of ., then your setup was successful.
Configuring SELinux
By default, SELinux blocks access to Nginx. You need to update the SELinux rules to allow Nginx access. To do that, install the policycoreutils package, which includes the semanage commands.
$ yum install policycoreutils-python
Now, run the following command. This command will create the necessary SELinux rules to allow access to Nginx.
$ semanage port -a -t http_port_t -p tcp 8080
Now restart the Nginx service to apply the SELinux rule changes.
$ systemctl restart nginx
Configuring Firewall
If you have a firewall enabled, you need to open the TCP port 8080 that we used to serve the Nginx server. To do that, run the following command.
$ firewall-cmd --permanent --zone=public --add-port=8080/tcp
$ firewall-cmd --reload
Now you can access the server using port 8080.
Conclusion
In this article, we have seen how to install and configure Nginx and PHP-FPM on CentOS 7. We also discussed configuring SELinux and firewall in order to make our setup work correctly.
FAQs
Q1. How do I confirm that Nginx is running?
Answer: The easiest way to confirm that Nginx is running is to visit the machine’s IP address in web browser.
Q2. How do I check PHP-FPM service status?
Answer: To check the status of PHP-FPM, use the command `systemctl status php-fpm`.
Q3. How do I configure Nginx for PHP-FPM?
Answer: To configure Nginx for PHP-FPM, open the `/etc/nginx/conf.d/default.conf` file and add the following lines of code at the end.
location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Thank you for reading this article. If you liked this article, please consider checking out our other articles.
Related Posts:
- Install Nginx Php Java On Centos 7 Install Nginx Php Java On Centos 7 What is Nginx? Nginx is an open source Web server created to serve Web traffic efficiently, reliably, and quickly. Nginx has become the…
- Centos 7 Nginx Php Worker Process And Worker Connection Centos 7 Nginx PHP Worker Process And Worker Connection What is Nginx? Nginx (pronounced "engine-x") is an open source web server software designed with high performance, stability and low memory…
- Instal Nginx Centos Di Webuzo Instal Nginx Centos Di Webuzo Background Webuzo is a leading web server platform used by individuals and businesses alike. It supports a range of operating systems, including the popular CentOS…
- Centos 7 Enable Nginx On Boot Centos 7 Enable Nginx On Boot Overview The ability to enable Nginx on Boot in CentOS 7 is a useful feature for web developers and server administrators. This article will…
- Install Nginx With Waf Centos 7 Install Nginx With Waf Centos 7 Overview Nginx is a popular open-source web server used for hosting websites and applications. It is widely used due to its high performance, efficient…
- Nginx Service Is Failed On Centos 7 Nginx Service Is Failed On Centos 7 Introduction Nginx is an open source Web server and a reverse proxy for HTTP, SMTP, POP3 and IMAP protocols. It can also be…
- Install Nginx Centos 7 Offline Install Nginx Centos 7 Offline Introduction Nginx is an open-source web server software, which is used to serve web pages and associated content. It is a fast, reliable, and robust…
- Nginx Config Test Centos 7 Nginx Config Test Centos 7 What is Nginx? Nginx is an open-source, high-performance web server that can be used to host static files, and also to serve dynamic requests such…
- Selinux Enable Php Fpm Nginx Centos 7 Selinux Enable Php Fpm Nginx Centos 7 What is Selinux Enable Php Fpm Nginx Centos 7? Selinux Enable Php Fpm Nginx CENTOS 7 is an easy-to-use web server and operating…
- How Instal Nginx Debian 9 How to Install Nginx Debian 9 What is Nginx? Nginx (pronounced "engine-x") is an open source web server and reverse proxy software. It is known for its light weight and…
- Certbot Centos 7 Nginx Certificate Invalid Certbot Centos 7 Nginx Certificate Invalid What is Certbot & Centos 7 Nginx Certificate? Certbot is an open-source software project from the Electronic Frontier Foundation (EFF). It enables website owners…
- Remove Apache And Install Nginx Centos 7 Remove Apache And Install Nginx Centos 7 Introduction Apache is an open source HTTP server used for hosting websites and web applications on the web. Although Apache is reliable and…
- How To Configure Virtual Host In Nginx On Centos 7 How To Configure Virtual Host In Nginx On Centos 7 Introduction We all need to configure virtual host on Nginx webserver on CentOS 7 in order to place multiple websites…
- Centos 6 Running 2 Version Php On Nginx Centos 6 Running 2 Version Php On Nginx Introduction Centos 6 can be used for web applications and websites based on php and nginx. This tutorial provides instructions on how…
- Install Nginx Postgres Centos 7 Install Nginx Postgres Centos 7 Introduction to Nginx and Postgres Nginx is an open source web server that has been gaining in popularity in recent years due to its reliability,…
- Centos 6 Nginx Phpmyadmin Forbideen Centos 6 Nginx Phpmyadmin Forbidden Introduction CentOS 6, an open-source Linux operating system, is one of the most popular web server operating systems. It is considered to be a reliable…
- Php-Fpm Cache Nginx Centos Php-Fpm Cache Nginx Centos Introduction to Nginx, Php-Fpm, and Centos Nginx, PHP-FPM, and Centos are three powerful, open-source technologies that are used to create powerful applications, websites, and services. Nginx…
- Nginx Dev Mapper Centos-Root Is 100 Full Nginx Dev Mapper CentOs-Root Is 100 Full What Is Nginx Dev Mapper? Nginx Dev Mapper is a file system space mapping tool for the Linux operating system and is shipped…
- Setting Html Nginx Centos 7 Setting HTML Nginx Centos 7 Overview This tutorial provides instructions on setting up an Nginx server to host HTML content on a CentOS 7 machine. It covers installing and configuring…
- Install Php 7 In Nginx Centos 7 Install Php 7 In Nginx Centos 7 1. Overview & Background We will start from having a quick overview and understanding the background of the installation of PHP 7 in…
- 404 Nginx Phpmyadmin Centos 7 404 Nginx Phpmyadmin Centos 7 Configuration of Nginx on Centos 7 for Phpmyadmin Setting up Nginx on Centos 7 to work with Phpmyadmin is a very simple process. The first…
- Install Nginx Phpmyadmin Centos 7 Install Nginx Phpmyadmin Centos 7 What is Nginx? Nginx is a web server that is used in Linux-based operating systems such as Centos 7. It is designed to handle high-traffic…
- Install Nginx 1.17 Centos 8 Install Nginx 1.17 Centos 8 Introduction to Nginx Nginx is one of the most popular web servers in the world. It is reliable, free, and open source software. It is…
- Nginx Multi Domain Centos 7 Nginx Multi Domain Centos 7 Introduction to Nginx Nginx is an open source, high performance web server software written in C language, designed to be deployed on Linux and Unix-like…
- Failed Install Nginx On Centos Failed Install Nginx On Centos What is Nginx? Nginx is an open source web server designed to handle high traffic websites. It has proven to be reliable over the years…
- Install Php Nginx Ubuntu 12.04 Install Php Nginx Ubuntu 12.04 Introduction Welcome to our guide on How to install PHP, Nginx & MySQL on Ubuntu 12.04 LTS. This guide should work on other Linux VPS…
- Wordpress Mariadb Nginx On Centos 7 Wordpress Mariadb Nginx On Centos 7 Installing Apache Apache is the most popular web server in the world. It is a powerful, versatile, and free open source software available for…
- Reverse Proxy Nginx Centos 7 Reverse Proxy Nginx Centos 7 What is a Reverse Proxy? A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or…
- Centos 6 Nginx Multiple Php Version Centos 6 Nginx Multiple PHP Versions What is CentOS 6? CentOS 6 is a Linux-based operating system. It is a free, open-source operating system that is based on Red Hat…
- Nginx What's Folder Dev Mapper Centos-Root Nginx What's Folder Dev Mapper Centos-Root What Is Nginx? Nginx is an open source web server created in 2004 by Igor Sysoev. It is a high-performance web server with a…