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:
- Centos 7 Install Nginx Php 7 Centos 7 Install Nginx Php 7 Nginx Server Overview and Prerequisites Nginx is a highly popular open source web server and reverse proxy software, known for its scalability and performance…
- Nginx Php Fpm 7.2 Nginx Php Fpm 7.2 Introduction to Nginx and PHP-FPM Nginx is an open-source web server, reverse proxy server, and load balancer. It is known for being lightweight and fast, and…
- Install Phpmyadmin On Nginx Ubuntu 18.04 Install Phpmyadmin On Nginx Ubuntu 18.04 Introduction PhpMyAdmin is one of the most popular and widely used web-based database management tools available. It is used for administering, managing and maintaining…
- Ldap Not Found Centos Nginx Php Ldap Not Found Centos Nginx Php What is LDAP? LDAP, or Lightweight Directory Access Protocol, is a standard protocol for storing user and server information over a network. It is…
- How To Install Nginx On Ubuntu 18.04 How To Install Nginx on Ubuntu 18.04 Introduction Nginx is a popular open-source web server and reverse proxy software that’s used by millions of websites. It’s the most widely used…
- Install Nginx In Ubuntu 16.04 Install Nginx In Ubuntu 16.04 Overview of Nginx Nginx (pronounced "engine-x") is an open-source Web server that is designed to provide a balance of flexibility, performance, and scalability. It is…
- Install Rails On Ubuntu Nginx Rbenv Install Rails On Ubuntu Nginx Rbenv Introduction Rails is an open-source web application framework written in Ruby. It is designed to make programming web applications easier by providing a full…
- Create Virtual Host Nginx Centos 7 Create Virtual Host Nginx Centos 7 What is a Virtual Host? A virtual host is a server hosting multiple domain names on the same web server. This type of hosting…
- Setting Up Passenger And Nginx Ubuntu 18.04 Setting Up Passenger And Nginx Ubuntu 18.04 Installing the Passenger Gem Passenger is an open source framework created by Phusion which allows easy integration of Nginx with Ruby applications such…
- Seting Var Ww On Nginx Centos7 Setting Var WW On Nginx Centos7 Overview Var WW is a powerful tool used to configure and control Nginx websites. It provides technical and graphical interface to manage the Nginx…
- Setup Centos 7 Nginx With Php Setup Centos 7 Nginx With Php Overview of Centos7 Nginx and PHP Centos 7 is one of the most popular operating systems for web servers. Many businesses use Centos 7…
- Error 502 Nginx On Server Centos Error 502 Nginx On Server Centos What is Error 502 Nginx On Server Centos? Error 502 Nginx On Server Centos is a type of HTTP status code error that occurs…
- Install Nginx Mysql Phpmyadmin Ubuntu Install Nginx Mysql Phpmyadmin Ubuntu 1. Introduction In today's tutorial, we will show you how to install Nginx, MySQL, and phpMyAdmin on Ubuntu 20.04. Nginx is an open source web…
- Install Mariadb Nginx Debian 9.7 Install Mariadb and Nginx on Debian 9.7 Overview of Installing MariaDB and Nginx on Debian 9.7 Installing MariaDB and Nginx on Debian 9.7 can be an essential step when setting…
- 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…
- How To Install Nginx And Mariadb 10 How To Install Nginx And Mariadb 10 What is Nginx and MariaDB 10 Nginx is a free, open-source web server that is known for its scalability and performance. It is…
- E Unable To Locate Package Nginx E Unable To Locate Package Nginx What is Nginx? Nginx is an open-source web server and reverse proxy that is used for hosting websites, web applications, and other network services.…
- Centos 7 Nginx Multiple Websites Centos 7 Nginx Multiple Websites Introduction Are you looking for a way to set up multiple websites on your CentOS 7 server utilizing the Nginx web server? If so, you’ve…
- How To Hide Nginx In Centos 7 How To Hide Nginx In Centos 7 What is Nginx? Nginx is a powerful web server and is one of the most popular web server software available today. It's a…
- Web Server Nginx Install Di Centos 7 Web Server Nginx Install Di Centos 7 Introduction to Nginx Nginx is a popular open source web server used to host websites and other applications. It is known for its…
- 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…
- How To Install Cachet Nginx How To Install Cachet Nginx Overview Cachet is an open-source monitoring platform that is widely used by web developers, DevOps engineers, and system administrators for monitoring the performance of a…
- Cannot Start Nginx On Centos 7 Failed To Exec Airflow Cannot Start Nginx On Centos 7 Failed To Exec Airflow What Is Nginx in Centos 7? Nginx is an open source web server that powers some of the largest and…
- Step By Step Install Nginx And Phpmyadmin On Ubuntu Step By Step Install Nginx And Phpmyadmin On Ubuntu Introduction Setting up a web server on Ubuntu is easy and straightforward. Nginx is a popular web server for Ubuntu, and…
- How To Config Nginx Upstream On Centos 7 How To Config Nginx Upstream On Centos 7 What is Nginx? Nginx is an open source, high performance web server and reverse proxy developed by Igor Sysoev in 2004. It…
- Remove Nginx Completely Centos 7 Remove Nginx Completely Centos 7 Overview This article will provide a brief overview of Nginx, some of the ways it can be removed from a Centos 7 system and directions…
- Centos 7 Migrate From Apache To Nginx Centos 7 Migrate From Apache To Nginx Introduction CentOS 7 is a popular Linux operating system (OS) that is used in many businesses and organizations. It is a stable and…
- How To Start Nginx On Centos 7 How To Start Nginx On Centos 7 Overview Nginx is a high-performance web server that is used to serve web pages and applications. It is a widely-used open source software…
- 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…
- Centos 6 Nginx Connection Timeout Centos 6 Nginx Connection Timeout Understanding What is a Connection Timeout? A connection timeout is when a particular connection has not completed the necessary steps for establishing a full connection…