Are you still worrying about setting up the environment for LNMP? CentOS7 builds LNMP environment white tutorial is here!


Preface

Today I finally finished the exam, and then I can spend time preparing for further learning and project actual combat and competition. At the same time, my blog will be updated more frequently. I also hope that all the big goddesses would like to comment!
Insert picture description here


Build environment version

L: Linux operating system (CentOS7)
N: ngnix 1.6
M: mysql 5.7
p: php7.0


Step 1: Prepare the compilation environment

  1. Remotely connect to the Linux instance.

  2. Turn off the firewall.

    2.1 Run the systemctl status firewalld command to view the current firewall status.
    Insert picture description here
    If the state parameter of the firewall is inactive, the firewall is in the off state.
    If the state parameter of the firewall is active, the firewall is in the open state. In this example, the firewall is turned on, so you need to turn off the firewall.
    Turn off the firewall. If the firewall is turned off, you can ignore this step.

    2.2 Turn off the firewall. If the firewall is turned off, you can ignore this step.
          If you want to temporarily turn off the firewall, run the command systemctl stop firewalld.
          If you want to turn off the firewall permanently, run the command systemctl disable firewalld.

  3. Close SELinux.
    3.1 Run the getenforce command to view the current status of SELinux.
    Insert picture description here
    If the SELinux status parameter is Disabled, then SELinux is disabled.
    If the SELinux status parameter is Enforcing, then SELinux is on. In this example, SELinux is turned on, so SELinux needs to be turned off.

    3.2 Close SELinux. If SELinux is turned off, you can ignore this step.

          If you want to temporarily disable SELinux, run the command setenforce 0.

          If you want to disable SELinux permanently, run the command vim /etc/selinux/config to edit the SELinux configuration file. After pressing Enter, move the cursor to the line SELINUX=enforcing, press the i key to enter the editing mode, modify it to SELINUX=disabled, press the Esc key, then type: wq and press the Enter key to save and close the SELinux configuration file. Finally, restart the system to make the settings take effect.


Step 2: Install Nginx

  1. Run the following command to install Nginx
yum -y install nginx
  1. Run the following command to check the Nginx version.
nginx -v

The returned result is as follows, indicating that the Nginx installation is successful.

nginx version: nginx/1.16.1

Step 3: Install MySQL

  1. Run the following command to update the YUM source.
rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  1. Run the following command to install MySQL.
yum -y install mysql-community-server
  1. Run the following command to view the MySQL version number.
mysql -V
  1. Run the following command to start MySQL.
systemctl start mysqld
  1. Run the following command to set up MySQL to start on boot.
systemctl enable mysqld
systemctl daemon-reload

Step 4: Install PHP

  1. Update YUM source.
    1.1 Run the following command to add epel source.
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

1.2 Run the following command to add the Webtatic source.

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  1. Run the following command to install PHP.
yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64  php70w-pdo.x86_64   php70w-mysqlnd  php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
  1. Run the following command to view the PHP version number.
php -v
  1. The returned result is as follows, indicating that the installation was successful.
PHP 7.0.33 (cli) (built: Dec  6 2018 22:30:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies                

Step Five: Configure Nginx

  1. Run the following command to back up the Nginx configuration file.
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
  1. Modify the Nginx configuration file to add Nginx support for PHP.
    2.1 Run the following command to open the Nginx configuration file.
vim /etc/nginx/nginx.conf

Press i to enter edit mode.
Inside the server braces, modify or add the following configuration information.

        #除下面提及的需要添加的配置信息外,其他配置保持默认值即可。
        #将location / 大括号内的信息修改为以下所示,配置网站被访问时的默认首页。
        location / {
    
    
            index index.php index.html index.htm;
        }
                #添加下列信息,配置Nginx通过fastcgi方式处理您的PHP请求。
        location ~ .php$ {
    
    
            root /usr/share/nginx/html;    #将/usr/share/nginx/html替换为您的网站根目录,本教程使用/usr/share/nginx/html作为网站根目录。
            fastcgi_pass 127.0.0.1:9000;   #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理。
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;   #Nginx调用fastcgi接口处理PHP请求。
        }                
  1. After adding the configuration information, as shown in the figure below.
    Insert picture description here
    After pressing the Esc key, type: wq and press Enter to save and close the configuration file.

  2. Run the following command to start the Nginx service.

systemctl start nginx                
  1. Run the following command to set the Nginx service to start automatically.
systemctl enable nginx                

Step 6: Configure MySQL

  1. Run the following command to view the /var/log/mysqld.log file, obtain and record the initial password of the root user.
grep 'temporary password' /var/log/mysqld.log
  1. The returned results are as follows:
2021-1-8 T14:57:47.535748Z 1 [Note] A temporary password is generated for root@localhost: p0/G28g>lsHD
  1. Run the following command to configure MySQL security.
mysql_secure_installation

The security configuration includes the following five aspects:

3.1 Reset the root account password.

Enter password for user root: #输入上一步获取的root用户初始密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration of the plugin.
Using existing password for root.
Estimated strength of the password: 100 
Change the password for root ? (Press y|Y for Yes, any other key for No) : Y #是否更改root用户密码,输入Y
New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
Re-enter new password: #再次输入新密码
Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y

3.2 Enter Y to delete the anonymous user account.

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y  #是否删除匿名用户,输入Y
Success.

3.3 Enter Y to prohibit remote login of the root account.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
Success.

3.4 Enter Y to delete the test library and access permissions to the test library.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
- Dropping test database...
Success.

3.5 Enter Y to reload the authorization form.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
Success.
All done!

Step 7: Configure PHP

  1. Create a new phpinfo.php file to display PHP information.
    1.1 Run the following command to create a new file.
vim <网站根目录>/phpinfo.php  #将<网站根目录>替换为您配置的网站根目录。

vim <website root directory>/phpinfo.php #Replace <website root directory> with the website root directory you configured.
Insert picture description here
The root directory of the website configured in this tutorial is /usr/share/nginx/html, so the command is:

vim /usr/share/nginx/html/phpinfo.php

Press i to enter edit mode.

Enter the following content, the function phpinfo()​ will display all the configuration information of PHP.

<?php echo phpinfo(); ?>

After pressing the Esc key, type: wq and press Enter to save and close the configuration file.

  1. Run the following command to start PHP-FPM.
systemctl start php-fpm
  1. Run the following command to set PHP-FPM to start automatically.
systemctl enable php-fpm

Step 8: Test access to the LNMP platform

  1. Open the browser.
  2. Enter http://<ECS instance public network IP address>/phpinfo.php in the address bar.
    The returned result is shown in the figure below, indicating that the LNMP environment has been successfully deployed.Insert picture description here

Next steps

  1. After the test access to the LNMP platform is successful, it is recommended that you run the following command to delete the phpinfo.php file to eliminate security risks.
rm -rf <网站根目录>/phpinfo.php   #将<网站根目录>替换为您在nginx.conf中配置的网站根目录
  1. The root directory of the website configured in this tutorial is /usr/share/nginx/html, so the command is:
rm -rf /usr/share/nginx/html/phpinfo.php

postscript

Even if you are busy, remember to love life!


Insist on sharing, insist on originality, pretty girls who like bloggers can check out the blogger's homepage blog!
Your likes and favorites are my greatest appreciation for sharing blogs!
Blogger blog address: https://blog.csdn.net/weixin_43967679

Guess you like

Origin blog.csdn.net/weixin_43967679/article/details/112384953