Current Location: Home> Latest Articles> LNMP Environment PHP Installation and Configuration Step-by-Step Guide

LNMP Environment PHP Installation and Configuration Step-by-Step Guide

gitbox 2025-06-17

Preparing the Environment

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:

  • Linux operating system (e.g., Ubuntu, CentOS, etc.)
  • Nginx web server
  • MySQL database

Step 1: Update the System

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:

sudo apt-get update && sudo apt-get upgrade

Step 2: Install PHP and Its Extensions

Next, install PHP and its required extensions. Open the terminal and run the following command:

sudo apt-get install php php-fpm php-mysql php-cli php-curl php-mbstring php-xml

The above command will install PHP along with common extensions, such as MySQL support and XML handling capabilities.

Configure PHP-FPM

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:

/etc/php/7.x/fpm/php.ini

Select the appropriate PHP version for your environment and ensure the following setting is correct:

cgi.fix_pathinfo=0

Start PHP-FPM Service

Once the configuration file is modified, start the PHP-FPM service and ensure it starts automatically on boot:

sudo systemctl start php7.x-fpm
sudo systemctl enable php7.x-fpm

Step 3: Configure Nginx to Support PHP

Next, configure the Nginx server to handle PHP requests. Locate your Nginx configuration file, typically found at:

/etc/nginx/sites-available/default

In the server block, add the following content to process PHP files:

location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.x-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

Restart Nginx

After the configuration is complete, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 4: Test PHP Installation

To verify that PHP was successfully installed, create a test file. Navigate to your web root directory and create a file named info.php:

echo "<?php phpinfo(); ?>" > /var/www/html/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!

Conclusion

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.