Nginx is a popular and efficient web server and reverse proxy, while PHP-FPM is a FastCGI Process Manager that greatly enhances the performance of PHP applications. Combining these two technologies can provide a robust and high-performance web server environment. In this guide, we’ll walk you through the process of installing and configuring Nginx with PHP-FPM on CentOS 8.
Prerequisites
Before you get started, ensure you have the following prerequisites:
- CentOS 8 Server: You should have a CentOS 8 server or a virtual machine with root or sudo access.
- Terminal: You’ll be using the command line for most of the installation and configuration steps.
Step 1: Update the System
Start by ensuring your system is up-to-date. Open a terminal and run the following commands:
sudo dnf update
This will update your package repositories and install any available updates.
Step 2: Install Nginx
Nginx is not included in the default CentOS 8 repositories, so you’ll need to add the EPEL (Extra Packages for Enterprise Linux) repository. Install EPEL with:
sudo dnf install epel-release
Now, you can install Nginx:
sudo dnf install nginx
After the installation is complete, start and enable Nginx to run at system boot:
sudo systemctl start nginx
sudo systemctl enable nginx
Step 3: Install PHP and PHP-FPM
To run PHP applications with Nginx, you need PHP and PHP-FPM. Install them using the following command:
sudo dnf install php php-fpm
Once PHP-FPM is installed, you need to configure it. Open the PHP-FPM configuration file in your preferred text editor:
sudo nano /etc/php-fpm.d/www.conf
Inside this file, look for the listen
directive and ensure it is set to a Unix socket:
listen = /var/run/php-fpm/php-fpm.sock
Save the file and restart PHP-FPM:
sudo systemctl restart php-fpm
Step 4: Configure Nginx to Use PHP-FPM
Now, you’ll configure Nginx to work with PHP-FPM. Create a new server block configuration for your website. You can do this by creating a new configuration file in the /etc/nginx/conf.d/
directory:
sudo nano /etc/nginx/conf.d/your-website.conf
Inside this configuration file, you can set up the server block to use PHP-FPM. Here’s a basic example:
server {
listen 80;
server_name your-website.com www.your-website.com;
root /var/www/html/your-website;
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Make sure to replace your-website.com
with your actual domain and adjust the root
directive to point to your website’s root directory.
Test the Nginx configuration to ensure there are no syntax errors:
sudo nginx -t
If the test is successful, reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 5: Test Your Setup
To test if Nginx and PHP-FPM are working correctly, create a simple PHP file in your website’s root directory. For example, create a file named info.php
:
sudo nano /var/www/html/your-website/info.php
Inside info.php
, add the following code:
<?php
phpinfo();
Save the file and access it in your web browser by navigating to http://your-website.com/info.php
. You should see the PHP info page, confirming that Nginx and PHP-FPM are set up correctly.
Conclusion
Congratulations, you’ve successfully set up and configured Nginx with PHP-FPM on CentOS 8. This powerful web server and PHP processing manager combination can handle your web applications efficiently and provide you with excellent performance. You can now start deploying and running PHP applications with confidence on your CentOS 8 server.