Before installing PHP, ensure that your CentOS system is up to date. Use the following command to update the system:
In order to install PHP successfully, you'll need to install some necessary dependencies. Run the following command to install these packages:
CentOS's default repositories may not contain the latest version of PHP, so you'll need to enable the EPEL (Extra Packages for Enterprise Linux) and Remi repositories. Use the following commands to enable them:
The Remi repository offers multiple PHP versions. Use the following command to enable the repository for the PHP version you want to install. For example, to install PHP 8.1:
Once you've selected the appropriate PHP version, you can begin installing PHP and the necessary modules. Use the following command to install PHP:
After the installation is complete, you can verify that PHP has been successfully installed by running the following command:
If the installation was successful, you'll see the PHP version information.
After installing PHP on CentOS, you may need to configure PHP to meet your specific needs. Typically, you can edit the php.ini file to adjust settings. Use the following command to open and edit the file:
Depending on your project requirements, you may need to install some PHP extensions. Use the following command to install commonly used extensions:
After installation and configuration, don't forget to restart your web server to apply the changes. Use the following command to restart Apache or Nginx:
Or
By following these steps, you've successfully installed PHP on your CentOS system. Mastering this process will not only help you set up and maintain websites but also enhance your development skills. Regular updates and maintenance of your PHP environment will be an important part of your future work, ensuring its security and stability.