Before you begin, make sure that your Linux operating system (such as Ubuntu, CentOS, etc.) is installed, and that Nginx and MySQL are running properly. Below are the prerequisites for installing PHP:
Before installing PHP, it is recommended to update your system to get the latest packages and security updates. Use the following command to update your system:
Next, install PHP and its required extensions. Open the terminal and run the following command:
The above command will install PHP along with common extensions, such as MySQL support and XML handling capabilities.
Once installation is complete, you need to configure PHP-FPM to ensure it works correctly with Nginx. Edit the PHP-FPM configuration file, typically located at:
Select the appropriate PHP version for your environment and ensure the following setting is correct:
Once the configuration file is modified, start the PHP-FPM service and ensure it starts automatically on boot:
Next, configure the Nginx server to handle PHP requests. Locate your Nginx configuration file, typically found at:
In the server block, add the following content to process PHP files:
After the configuration is complete, restart Nginx to apply the changes:
To verify that PHP was successfully installed, create a test file. Navigate to your web root directory and create a file named info.php:
Then, visit http://your-server-ip/info.php in your browser. If you see the PHP info page, congratulations! You have successfully installed PHP in the LNMP environment!
By following the steps above, you can successfully install and configure PHP in the LNMP environment. This guide will help you easily set up and adjust your web server to meet development and production needs. Remember to regularly check for updates and install additional PHP extensions as needed to improve functionality and performance.
We hope this LNMP environment PHP installation guide is helpful to you! If you have any questions, feel free to discuss them in the comments section.