To install and configure the nginx web server with PHP5 and FastCGI on Red Hat or CentOS, you can follow these steps:
- Install the EPEL repository: nginx and PHP5 are not included in the default repositories for Red Hat or CentOS, so you will need to install the EPEL (Extra Packages for Enterprise Linux) repository. You can install the EPEL repository by running the following command:
sudo yum install epel-release
- Install nginx and PHP5: Once you have installed the EPEL repository, you can install nginx and PHP5 by running the following command:
sudo yum install nginx php-fpm
- Configure PHP-FPM: PHP-FPM is the FastCGI Process Manager for PHP. You need to configure PHP-FPM to work with nginx. Open the PHP-FPM configuration file
/etc/php-fpm.d/www.conf
using your preferred text editor, and make the following changes:listen = /var/run/php-fpm/php-fpm.sock
Uncomment the
listen
line and change it to the above.user = nginx
group = nginx
Uncomment the
user
andgroup
lines and set them tonginx
. - Configure nginx: Open the nginx configuration file
/etc/nginx/nginx.conf
using your preferred text editor, and make the following changes:server {
listen 80;
server_name example.com;root /var/www/html;
index index.php index.html index.htm;location / {
try_files $uri $uri/ /index.php?$args;
}location ~ \.php$ {
try_files $uri =404;
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;
}
}
Replace
example.com
with the hostname or IP address of your server, and set theroot
directive to the location of your website’s files. This configuration sets up nginx to listen on port 80 and pass PHP requests to PHP-FPM over a Unix socket. - Start the nginx and PHP-FPM services: Once you have configured nginx and PHP-FPM, you can start the services using the following commands:
sudo systemctl start nginx
sudo systemctl start php-fpm
You can also enable the services to start automatically at boot time using the following commands:
sudo systemctl enable nginx
sudo systemctl enable php-fpm
This will ensure that nginx and PHP-FPM start automatically when the system boots.
That’s it! You should now have a working nginx web server with PHP5 and FastCGI on your Red Hat or CentOS system.