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 a CentOS 8 Elastic Compute Service (ECS) instance.

Prerequisites

  • An ECS instance is created and a public IP address is assigned to the instance. For more information, see Creation method overview.
    In this topic, an ECS instance with the following configurations is used. When you follow this guide, we recommend that you use the same operating system version used in this example. This helps prevent command compatibility issues between operating system versions.
    • Instance type: ecs.c6.large
    • Operating system: CentOS 8.1 64-bit public image
    • Network type: Virtual Private Cloud (VPC)
    • IP address: public IP address
  • 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 a security group rule.
    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 requirements. For example, if you want to connect to a MySQL database on an ECS instance, you must configure an inbound rule in the 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 help on how to use DNF.

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.

The example in this topic uses an ECS instance with the following configurations. In actual practice, some actions may vary based on the configurations of your ECS 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 ECS instance on which you want to deploy an LNMP environment.
    For more information, see Connection methods.
  2. Disable the firewall.
    1. Run the systemctl status firewalld command to check the state of the firewall.
      Check the state 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.
    2. Disable the firewall. Skip this step if the firewall is already disabled.
      • To temporarily disable the firewall, run the following command:
        systemctl stop firewalld
        Note After you run this command, the firewall is temporarily disabled. When you restart the Linux instance, the firewall is enabled automatically.
      • To permanently disable the firewall, perform the following steps:
        1. Run the following command to disable the firewall:
          systemctl stop firewalld
        2. Run the following command to prevent the firewall from being automatically enabled at system startup:
          systemctl disable firewalld
        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 state of SELinux.
      • If SELinux is in the Disabled state, SELinux is disabled.
      • If SELinux is in the Enforcing state, SELinux is enabled.
    2. Disable SELinux. Skip this step if SELinux is already disabled.
      You can disable SELinux on a temporary or permanent basis depending on your business needs. For more information, see Enable or disable SELinux.

Step 2: Install NGINX

  1. Change the CentOS 8 repository address.

    CentOS 8 has reached its end of life (EOL). In accordance with Linux community rules, all content has been removed from the following CentOS 8 repository address: http://mirror.centos.org/centos/8/. If you continue to use the default CentOS 8 repository configurations on Alibaba Cloud, an error is reported. To use specific installation packages of CentOS 8, change the CentOS 8 repository address. For more information, see Change CentOS 8 repository addresses.

  2. Run the following command to install NGINX.
    In this example, NGINX 1.16.1 is used.
    Note Go to the official NGINX 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
  3. 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 Extra Packages for Enterprise Linux (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 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. 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 comment character (#) from the front of the location ~ \.php$ line and modify the content within the braces.
      Make the following modifications:
      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 the same instance. Communication across networks is not supported.
    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 set 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 start the configuration process.
    2. Enter 2 and press the Enter key when prompted for the password strength policy.

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

    3. Enter a new password and confirm it.

      In this example, the password PASSword123! is used .

    4. Enter Y and press the Enter key to confirm your 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 other than 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 and edit the phpinfo.php file to show PHP information.
    1. Run the following command to create the phpinfo.php file:
      vim <website root directory> /phpinfo.php
      The <website root directory> is the root value enclosed inside the location ~ .php$ braces that you configured in the nginx.conf file, as shown in the following figure. Website root directoryIn this example, the website root directory is /usr/share/nginx/html. You can run the following command to create the phpinfo.php file:
      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.
    If a page similar to the one in the following figure is displayed, 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