Step-by-Step Guide
- Update the Package Index
Open a terminal and update the package index to ensure you have the latest information about available packages:
sudo yum update -y
- Install PHP
Install PHP along with some common PHP modules using theyum
package manager:
sudo yum install php php-mysqlnd php-fpm -y
This command installs PHP, the PHP MySQL extension, and PHP-FPM (FastCGI Process Manager).
- Verify PHP Installation
Check the PHP version to ensure it is installed correctly:
php -v
You should see the PHP version information displayed.
- Configure PHP-FPM
Open the PHP-FPM configuration file for editing:
sudo nano /etc/php-fpm.d/www.conf
Make sure the user
and group
directives are set to nginx
if you plan to use PHP with Nginx, or apache
if you plan to use PHP with Apache:
user = apache
group = apache
For Nginx, use:
user = nginx
group = nginx
Save and close the file.
- Start and Enable PHP-FPM
Start the PHP-FPM service:
sudo systemctl start php-fpm
Enable PHP-FPM to start on boot:
sudo systemctl enable php-fpm
- Install and Configure Apache (Optional)
If you plan to use PHP with Apache, install Apache:
sudo yum install httpd -y
Start and enable Apache:
sudo systemctl start httpd
sudo systemctl enable httpd
Configure Apache to use PHP by editing the default virtual host configuration file:
sudo nano /etc/httpd/conf.d/php.conf
Add the following lines to configure Apache to handle PHP files:
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php-fpm/php-fpm.sock|fcgi://localhost"
</FilesMatch>
Save and close the file. Restart Apache to apply the changes:
sudo systemctl restart httpd
- Install and Configure Nginx (Optional)
If you plan to use PHP with Nginx, install Nginx:
sudo yum install nginx -y
Start and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
Configure Nginx to use PHP by editing the default server block configuration file:
sudo nano /etc/nginx/conf.d/default.conf
Add the following lines to configure Nginx to handle PHP files:
server {
listen 80;
server_name your_domain www.your_domain;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
location ~ /\.ht {
deny all;
}
}
Save and close the file. Restart Nginx to apply the changes:
sudo systemctl restart nginx
- Test PHP Installation
To test PHP with Apache or Nginx, create a new PHP file in the web root directory. For Apache, use:
sudo nano /var/www/html/info.php
For Nginx, use:
sudo nano /usr/share/nginx/html/info.php
Add the following PHP code to the file:
<?php
phpinfo();
?>
Save and close the file.
- Access the PHP Info Page
Open your web browser and visithttp://your_server_ip/info.php
. You should see the PHP information page, indicating that PHP is working correctly with Apache or Nginx. - Install Additional PHP Modules (Optional)
You can install additional PHP modules as needed. To search for available PHP modules, use:sudo yum search php-
To install a specific PHP module, use:sudo yum install php-module_name -y
Replacemodule_name
with the name of the module you wish to install. Restart PHP-FPM after installing additional modules:sudo systemctl restart php-fpm
- Remove the PHP Info Page
For security reasons, it’s a good idea to remove the PHP info page after testing:bash sudo rm /var/www/html/info.php