LNMP is an acronym of the names of its original four open source components: the Linux operating system, NGINX web server, MySQL relational database management system, and PHP programming language. This topic describes how to manually build an LNMP environment on an Elastic Compute Service (ECS) instance that runs a CentOS 8 operating system.

Prerequisites

  • An ECS instance is created and a public IP address is assigned to the instance. For more information, see Creation method overview.
  • An inbound rule is added to a security group of the ECS instance to allow traffic on ports 22, 80, and 443. For more information, see Add security group rules.
    Note For security purposes, this topic describes only the ports on which traffic must be allowed to deploy and test an LNMP environment. You can configure security group rules to allow traffic on more ports based on your needs. For example, if you want to connect to a MySQL database on an ECS instance, you can configure an inbound rule in a security group of the instance to allow traffic on port 3306, which is the default port used for MySQL.

Background information

By default, the Dandified YUM (DNF) package manager is installed in CentOS 8. DNF is the next-generation version of Yellowdog Updater Modified (YUM). You can run the dnf command in CentOS 8 to obtain related instructions.

This topic is intended for individual users who are familiar with Linux operating systems but new to using Alibaba Cloud ECS to build websites.

You can also purchase an LNMP image in Alibaba Cloud Marketplace and create an ECS instance from the image to build websites.

In the topic, an ECS instance with the following configurations is used. Operations may vary based on the configurations of your instance.
  • Instance type: ecs.c6.large
  • Operating system: CentOS 8.1 64-bit public image
  • CPU: 2 vCPUs
  • Memory: 4 GiB
  • Network type: Virtual Private Cloud (VPC)
  • IP address: public IP address
The following software versions are used. If you use software versions different from the following ones, you may need to adjust commands and parameter settings.
  • NGINX 1.16.1
  • MySQL 8.0.17
  • PHP 7.3.5

Step 1: Prepare the compilation environment

  1. Connect to the CentOS 7 instance.
  2. Disable the firewall.
    1. Run the systemctl status firewalld command to check the status of the firewall.
      Check the status of the firewall
      • If the firewall is in the inactive state, the firewall is disabled.
      • If the firewall is in the active state, the firewall is enabled. In this example, the firewall is in the active state, and you must disable the firewall.
    2. Disable the firewall. Skip this step if the firewall is already disabled.
      • To temporarily disable the firewall, run the systemctl stop firewalld command.
        Note After you run this command, the firewall is disabled. The next time you restart the Linux operating system, the firewall is enabled and enters the active state.
      • To permanently disable the firewall, run the systemctl disable firewalld command.
        Note You can re-enable the firewall after it is disabled. For more information, visit the official firewalld website.
  3. Disable Security-Enhanced Linux (SELinux).
    1. Run the getenforce command to check the status of SELinux.
      Check the status of SELinux
      • If SELinux is in the Disabled state, SELinux is disabled.
      • If SELinux is in the Enforcing state, SELinux is enabled. In this example, SELinux is in the Enforcing state, and you must disable SELinux.
    2. Disable SELinux. Skip this step if SELinux is already disabled.
      • To temporarily disable SELinux, run the setenforce 0 command.
        Note After you run this command, SELinux is disabled. The next time you restart the Linux operating system, SELinux is enabled and enters the Enforcing state.
      • To permanently disable SELinux, run the vim /etc/selinux/config command to edit the SELinux configuration file. Press the Enter key. Move the pointer over the SELINUX=enforcing line and press the I key to enter the edit mode. Change SELINUX=enforcing to SELINUX=disabled and press the Esc key. Enter :wq and press the Enter key to save and close the SELinux configuration file. Restart the operating system to apply the settings.
        Note You can re-enable SELinux after it is disabled. For more information, see Enable or disable SELinux.

Step 2: Install NGINX

  1. Run the following command to install NGINX.
    In this example, NGINX 1.16.1 is used.
    Note Go to the NGINX official website to view the list of NGINX packages suited for CentOS 8.
    dnf -y install http://nginx.org/packages/centos/8/x86_64/RPMS/nginx-1.16.1-1.el8.ngx.x86_64.rpm
  2. Run the following command to check the version of NGINX:
    nginx -v
    The following command output shows the NGINX version:
    nginx version: nginx/1.16.1

Step 3: Install MySQL

  1. Run the following command to install MySQL:
    dnf -y install @mysql
  2. Run the following command to check the version of MySQL:
    mysql -V
    The following command output shows the MySQL version:
    mysql  Ver 8.0.17 for Linux on x86_64 (Source distribution)

Step 4: Install PHP

  1. Run the following commands to add and update the EPEL repository:
    dnf -y install epel-release
    dnf update epel-release
  2. Run the following commands to delete unneeded cached software packages and update the software repository:
    dnf clean all
    dnf makecache
  3. Run the following command to start the php:7.3 module.
    Note In this example, PHP 7.3 is used. If you want to use PHP 7.4, you must install the remi repository by running the dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm command.
    dnf module enable php:7.3
  4. Run the following command to install the PHP modules:
    dnf install php php-curl php-dom php-exif php-fileinfo php-fpm php-gd php-hash php-json php-mbstring php-mysqli php-openssl php-pcre php-xml libsodium
  5. Run the following command to check the version of PHP:
    php -v
    The following command output shows the PHP version:
    PHP 7.3.5 (cli) (built: Apr 30 2019 08:37:17) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.3.5, Copyright (c) 1998-2018 Zend Technologies

Step 5: Configure NGINX

  1. Run the following command to check the default path of the NGINX configuration file:
    cat /etc/nginx/nginx.conf
    The include configuration item within the http braces indicates the default path of the configuration file. conf
  2. Run the following commands to back up the default configuration file in the default path of the configuration file:
    cd /etc/nginx/conf.d
    cp default.conf default.conf.bak
  3. Modify the default configuration file.
    1. Run the following command to open the default configuration file:
      vi default.conf
    2. Press the I key to enter the edit mode.
    3. Make the following modifications to the content within the location braces:
      location / {
          # Replace the path with the root directory of your website. 
          root   /usr/share/nginx/html;
          # Add the default homepage index.php. 
          index  index.html index.htm index.php;
      }
    4. Remove the annotation character # in front of location ~ \.php$ and modify the content within the braces.
      The following modifications are made:
      location ~ \.php$ {
          # Replace the path with the root directory of your website. 
          root           /usr/share/nginx/html;
          NGINX is associated with PHP-FPM by using UNIX sockets. This configuration must be the same as the listen configuration in the /etc/php-fpm.d/www.conf file. 
          fastcgi_pass   unix:/run/php-fpm/www.sock;
          fastcgi_index  index.php;
          # Change /scripts$fastcgi_script_name to $document_root$fastcgi_script_name. 
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          # NGINX calls the FastCGI operation to process the PHP requests. 
          include        fastcgi_params;
      }
      Note The following methods can be used for inter-process communication between NGINX and PHP-FPM.
      • Use TCP sockets: NGINX can communicate with PHP-FPM across instances over the network.
      • Use UNIX domain sockets: NGINX can communicate with PHP-FPM only within a single instance without using the network.
    5. Press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.
  4. Run the following command to start NGINX:
    systemctl start nginx
  5. Run the following command to enable NGINX to start on instance startup:
    systemctl enable nginx

Step 6: Configure MySQL

  1. Run the following command to start MySQL and enable it to start on instance startup:
    systemctl enable --now mysqld
  2. Run the following command to check whether MySQL is started:
    systemctl status mysqld
    If MySQL is started, the command output contains Active: active (running).
  3. Run the following command to make security configurations for MySQL and set the password:
    mysql_secure_installation
    After you run the command, perform the following operations based on the command prompts:
    1. Enter Y and press the Enter key to make the configurations.
    2. Enter 2 as the password strength and press the Enter key.

      0 indicates a low password strength, 1 indicates a medium password strength, and 2 indicates a high password strength. We recommend that you select a high password strength.

    3. Enter a new password and confirm it.

      In this example, the password is PASSword123!.

    4. Enter Y and press the Enter key to use the password.
    5. Enter Y and press the Enter key to delete anonymous users.
    6. Set whether to allow remote access to MySQL.
      • Enter Y and press the Enter key to deny remote access.
      • Enter N or a key instead of Y and press the Enter key to allow remote access.
    7. Enter Y and press the Enter key to delete the test database and access permissions on the test database.
    8. Enter Y and press the Enter key to reload privilege tables.

Step 7: Configure PHP

  1. Modify the PHP configuration file.
    1. Run the following command to open the configuration file:
      vi /etc/php-fpm.d/www.conf
    2. Press the I key to enter the edit mode.
    3. Find the user = apache and group = apache lines, and change apache to nginx.
      php-fpm conf
    4. Press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.
  2. Create the phpinfo.php file to show the PHP information.
    1. Run the following command to create the file:
      vim <Website root directory>/phpinfo.php  # Replace <Website root directory> with your website root directory. 

      The website root directory is the root value within the location ~ .php$ braces that you configured in the nginx.conf file, as shown in the following figure.

      lnmp-root-dir

      In this example, the website root directory is /usr/share/nginx/html. You can run the following command:

      vim /usr/share/nginx/html/phpinfo.php
    2. Press the I key to enter the edit mode.
    3. Enter the following content. The phpinfo() function shows all configuration information of PHP.
      <?php echo phpinfo(); ?>
    4. Press the Esc key, enter :wq, and then press the Enter key to save and close the configuration file.
  3. Run the following command to start PHP-FPM:
    systemctl start php-fpm
  4. Run the following command to enable PHP-FPM to start on instance startup:
    systemctl enable php-fpm

Step 8: Test the connection to the LNMP environment

  1. Open the browser on your computer.
  2. In the address bar, enter http://<Public IP address of the ECS instance>/phpinfo.php.
    The following page indicates that the LNMP environment is deployed. phpinfo

What to do next

After you confirm that the LNMP environment is deployed, we recommend that you run the following command to delete the phpinfo.php file to ensure system security:
rm -rf <Website root directory>/phpinfo.php   # Replace the <website root directory> with the website root directory that you configured in the nginx.conf file.
In this example, the website root directory is /usr/share/nginx/html. Run the following command:
rm -rf /usr/share/nginx/html/phpinfo.php