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 ECS instance that runs the CentOS 8 operating system.

Prerequisites

  • An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the account registration page.
  • An ECS instance that runs the CentOS 7 operating system is created and assigned a public IP address. For information about how to create an ECS instance, see Create an ECS instance.
  • An inbound rule is added to the security group of the ECS instance to allow traffic on port 80. For information about how to allow traffic on port 80, see Add security group rules.
    Rule direction Action Protocol type Port range Priority Authorized object
    Inbound Allow HTTP (80) 80/80 1 The public IP addresses of clients to be allowed to access the LNMP environment. Separate multiple IP addresses with commas (,).

    The value of 0.0.0.0/0 indicates that all IP addresses are allowed to access the LNMP environment.

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 the Linux operating system, but new to using Alibaba Cloud ECS to build websites.

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

An ecs.c6.large instance with the following configurations is used as an example. The actual operation depends on your instance configurations.
  • CPU: 2 vCPUs
  • Memory: 4 GiB
  • Network type: VPC
  • IP address: public IP address
The procedure described in this topic is applicable to the following operating system and software versions. If you use software versions different from the ones listed, you may need to adjust the commands and parameter settings.
  • Operating system: public images that run CentOS 8.1 64-bit
  • NGINX 1.16.1
  • MySQL 8.0.17
  • PHP 7.3.5

Step 1. Prepare the compilation environment

  1. Connect to a Linux 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. Therefore, 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 temporarily disabled. It will enter the active state when you restart the Linux operating system.
      • 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 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. Therefore, 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 temporarily disabled. It will enter the Enforcing state when you restart the Linux operating system.
      • To permanently disable SELinux, run the vim /etc/selinux/config command to edit the SELinux configuration file. Press the Enter key. Move the pointer to the SELINUX=enforcing row and press the I key to edit the configuration file. 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.
        Note You can re-enable SELinux after it is disabled. For more information, see Enable or disable SELinux.
    3. Restart the system to apply the settings.

Step 2: Install NGINX

  1. Run the following command to install NGINX.
    NGINX 1.16.1 is used in this tutorial.
    Note Click here 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 NGINX version:
    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 MySQL version:
    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. Start the php:7.3 module.
    Note PHP 7.3 is used in the example. 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 PHP version:
    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. 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 forwards your PHP requests to PHP FastCGI Process Manager (PHP-FPM) over port 9000 of the ECS instance.
          fastcgi_pass   127.0.0.1:9000;
          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;
      }
    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 upon system startup:
    systemctl enable nginx

Step 6: Configure MySQL

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

      Value 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 any other 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 display 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:
      <? 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 upon system startup:
    systemctl enable php-fpm

Step 8: Test the connection to the LNMP environment

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

What to do next

After you confirm that the LNMP environment has been 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. You can run the following command:

rm -rf /usr/share/nginx/html/phpinfo.php