Help & DocumentationCloud Virtual MachineBest PracticeSetting up LNMPManually Building an LNMP Environment (CentOS 7)

Manually Building an LNMP Environment (CentOS 7)

Last updated: 2020-02-25 13:28:46

PDF

Operation Scenario

The LNMP environment is a website server architecture made up of Nginx, MySQL or MariaDB, and PHP in Linux system. This document describes how to manually build an LNMP environment on a Tencent Cloud Virtual Machine (CVM).

To manually build an LNMP environment, you need to be familiar with Linux commands (see Installing software via YUM in CentOS environment For some examples), usage and version compatibility of the software to be installed.

Software Versions

In this example, software versions used to build the LNMP environment are as follows:

  • Linux: Linux operating system. In this example, CentOS 7.6 is used.
  • Nginx: Web server. In this example, Nginx 1.17.7 is used.
  • MariaDB: Database. In this example, MariaDB 10.4.8 is used.
  • PHP: Scripting language. In this example, PHP 7.2.22 is used.

Directions

Step 1: Logging in to a Linux instance

Step 2: Installing Nginx

  1. Run the following command to create a file named nginx.repo Under /etc/yum.repos.d/ .
vi /etc/yum.repos.d/nginx.repo
  1. Press I To switch to the editing mode and enter the following.
[nginx] 
name = nginx repo 
baseurl = https://nginx.org/packages/mainline/centos/7/$basearch/ 
gpgcheck = 0 
enabled = 1
  1. Press Esc , enter : wq , save the file and return.
  2. Run the following command to install Nginx.
yum install -y nginx
  1. Run the following command to open the nginx.conf File.
vim /etc/nginx/nginx.conf
  1. Press I To switch to the editing mode, and edit the nginx.conf File.
  2. find server{...} And set the server Replace the corresponding configuration information in curly braces with the following.
    The monitoring of IPv6 addresses will be canceled and Nginx will be configured to interact with PHP.

You can use the Ctrl+F Turn the page down, Ctrl+B Turn the page up to view the file. If nginx.conf Not found in file server{...} . Please go to include /etc/nginx/conf.d/*conf; Add the following above.

server {
    listen       80;
    root   /usr/share/nginx/html;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;
    #
    location / {
          index index.php index.html index.htm;
    }
    #error_page  404              /404.html;
    #redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
    #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .php$ {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      include        fastcgi_params;
    }
}
  1. Press Esc , enter : wq , save the file and return.
  2. Run the following command to start Nginx.
systemctl start nginx
  1. Run the following command to configure the automatic startup of Nginx.
systemctl enable nginx 
  1. In a local browser, visit the following URL to verify that the Nginx service is working properly.
http://[Public IP address of the CVM instance]

If the following appears, Nginx has been successfully installed and configured.

Step 3: Installing Database

  1. Run the following command to check whether MariaDB has been installed in the system.
rpm -qa | grep -i mariadb
  • If the following results are returned, MariaDB already exists.

    To avoid conflicts between different versions, run the following command to remove the installed MariaDB.
yum -y remove [Package name]
  • If the returned result is empty, MariaDB is not installed. In this case, proceed to the next step.
  1. Run the following command to create the MariaDB.repo File under /etc/yum.repos.d/ .
vi /etc/yum.repos.d/MariaDB.repo
  1. Press " I "switch to edit mode, write the following, and add the MariaDB software library.

MariaDB software libraries vary from operating system to operating system. You can go to MariaDB website Get MariaDB software library installation information for other versions of the operating system.

# MariaDB 10.4 CentOS repository list - created 2019-11-05 11:56 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.4/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck = 0
  1. Press Esc , enter : wq , save the file and return.
  2. Run the following command to install MariaDB.
yum -y install MariaDB-client MariaDB-server
  1. Run the following command to start the MariaDB service.
systemctl start mariadb
  1. Run the following command to configure the automatic startup of MariaDB.
systemctl enable mariadb
  1. Run the following command to verify that MariaDB is successfully installed.
mysql

If the following result appears, MariaDB has been successfully installed.

9. Run the following command to exit MariaDB.

\q

Step 4: Installing and configuring PHP

  1. Run the following commands to update the software source of PHP in Yum.
rpm -Uvh https://mirrors.cloud.tencent.com/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  1. Run the following command to install the packages required for PHP 7.2.
yum -y install mod_php72w.x86_64 php72w-cli.x86_64 php72w-common.x86_64 php72w-mysqlnd php72w-fpm.x86_64
  1. Run the following command to start the PHP-FPM service.
systemctl start php-fpm
  1. Run the following command to configure the automatic startup of PHP-FPM service.
systemctl enable php-fpm

Verifying the environment configuration

After finishing the environment configuration, complete the following steps to verify that the LNMP environment has been built successfully.

  1. Run the following command to create a test file.
echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php
  1. Run the following command to restart the Nginx service.
systemctl restart nginx
  1. In a local browser, visit the following URL to check whether the environment configuration is successful.
http://[Public IP address of the CVM instance]

If the following results appear, the environment configuration is successful.

After the LNMP environment is built, you can Manually construct a WordPress website To gain a better understanding of CVM features.

FAQ

If you encounter a problem when using CVM, refer to the following documents for troubleshooting as needed: