Before you begin installing and configuring PHP 7 and Apache, ensure that your CentOS 7 system is fully updated. Use the following command to update:
sudo yum update -y
Next, install the Apache Web Server. Run the following command to install:
sudo yum install httpd -y
After installation, start the Apache service and enable it to run automatically on system startup:
sudo systemctl start httpd
sudo systemctl enable httpd
To verify that Apache is successfully installed and running, enter the server's IP address in your browser. If everything is working properly, you should see the default Apache welcome page.
After installing Apache, proceed with installing PHP 7. First, enable the Software Collections (SCL) repository to support PHP 7:
sudo yum install centos-release-scl -y
Next, install PHP 7 along with its common modules:
sudo yum install rh-php74 rh-php74-php rh-php74-php-mysqlnd -y
Once the installation is complete, enable PHP 7 by running the following command:
scl enable rh-php74 bash
To make Apache support PHP, you need to install the PHP Apache module:
sudo yum install rh-php74-php-pecl-apcu -y
After installing the module, edit the Apache configuration file to handle PHP files:
sudo vi /etc/httpd/conf/httpd.conf
Ensure the following line is added to the configuration file:
AddHandler php74-script .php
Create a phpinfo file to test if PHP is working properly. In the server's root directory, create a file named info.php and add the following content:
<?php phpinfo(); ?>
Visit this file: http://your-server-ip/info.php, and you should see the PHP configuration page, confirming the installation was successful.
Once all configurations are complete, restart the Apache service to apply the changes:
sudo systemctl restart httpd
For better server security, it's recommended to disable unnecessary modules and configure the firewall. Additionally, consider installing SSL certificates to provide encrypted protection for your website.
By following the steps in this guide, you have successfully set up PHP 7 and Apache Web Server on CentOS 7. This setup provides a solid foundation for web development, and you can further extend it by installing other services and applications as needed.