你还在为LNMP搭环境而发愁吗?CentOS7搭建LNMP环境小白教程来了!


前言

今天终于考完试了, 接下来可以把时间拿来准备拓展学习和项目实战以及竞赛了,同时我的博客也会更加频繁更新了,还愿各位大佬女神点个赞评个论!
在这里插入图片描述


搭建环境版本

L: Linux 操作系统(CentOS7)
N: ngnix 1.6
M: mysql 5.7
p: php7.0


步骤一:准备编译环境

  1. 远程连接Linux实例。

  2. 关闭防火墙。

    2.1 运行systemctl status firewalld命令查看当前防火墙的状态。
    在这里插入图片描述
    如果防火墙的状态参数是inactive,则防火墙为关闭状态。
    如果防火墙的状态参数是active,则防火墙为开启状态。本示例中防火墙为开启状态,因此需要关闭防火墙。
    关闭防火墙。如果防火墙为关闭状态可以忽略此步骤。

    2.2 关闭防火墙。如果防火墙为关闭状态可以忽略此步骤。
          如果您想临时关闭防火墙,运行命令systemctl stop firewalld。
          如果您想永久关闭防火墙,运行命令systemctl disable firewalld。

  3. 关闭SELinux。
    3.1 运行getenforce命令查看SELinux的当前状态。
    在这里插入图片描述
    如果SELinux状态参数是Disabled,则SELinux为关闭状态。
    如果SELinux状态参数是Enforcing,则SELinux为开启状态。本示例中SELinux为开启状态,因此需要关闭SELinux。

    3.2 关闭SELinux。如果SELinux为关闭状态可以忽略此步骤。

          如果您想临时关闭SELinux,运行命令setenforce 0。

          如果您想永久关闭SELinux,运行命令vim /etc/selinux/config编辑SELinux配置文件。回车后,把光标移动到SELINUX=enforcing这一行,按i键进入编辑模式,修改为SELINUX=disabled,按Esc键,然后输入:wq并按Enter键以保存并关闭SELinux配置文件。 最后重启系统使设置生效。


步骤二:安装Nginx

  1. 运行以下命令安装Nginx
yum -y install nginx
  1. 运行以下命令查看Nginx版本。
nginx -v

返回结果如下所示,表示Nginx安装成功。

nginx version: nginx/1.16.1

步骤三:安装MySQL

  1. 运行以下命令更新YUM源。
rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  1. 运行以下命令安装MySQL。
yum -y install mysql-community-server
  1. 运行以下命令查看MySQL版本号。
mysql -V
  1. 运行以下命令启动MySQL。
systemctl start mysqld
  1. 运行以下命令设置开机启动MySQL。
systemctl enable mysqld
systemctl daemon-reload

步骤四:安装PHP

  1. 更新YUM源。
    1.1 运行以下命令添加epel源。
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

1.2 运行以下命令添加Webtatic源。

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  1. 运行以下命令安装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. 运行以下命令查看PHP版本号。
php -v
  1. 返回结果如下所示,表示安装成功。
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                

步骤五:配置Nginx

  1. 运行以下命令备份Nginx配置文件。
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
  1. 修改Nginx配置文件,添加Nginx对PHP的支持。
    2.1 运行以下命令打开Nginx配置文件。
vim /etc/nginx/nginx.conf

按i进入编辑模式。
在server大括号内,修改或添加下列配置信息。

        #除下面提及的需要添加的配置信息外,其他配置保持默认值即可。
        #将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. 添加配置信息后,如下图所示。
    在这里插入图片描述
    按下Esc键后,输入:wq并回车以保存并关闭配置文件。

  2. 运行以下命令启动Nginx服务。

systemctl start nginx                
  1. 运行以下命令设置Nginx服务开机自启动。
systemctl enable nginx                

步骤六:配置MySQL

  1. 运行以下命令查看/var/log/mysqld.log文件,获取并记录root用户的初始密码。
grep 'temporary password' /var/log/mysqld.log
  1. 返回结果如下:
2021-1-8 T14:57:47.535748Z 1 [Note] A temporary password is generated for root@localhost: p0/G28g>lsHD
  1. 运行以下命令配置MySQL的安全性。
mysql_secure_installation

安全性的配置包含以下五个方面:

3.1 重置root账号密码。

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 输入Y删除匿名用户账号。

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 输入Y禁止root账号远程登录。

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

3.4 输入Y删除test库以及对test库的访问权限。

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 输入Y重新加载授权表。

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

步骤七:配置PHP

  1. 新建phpinfo.php文件,用于展示PHP信息。
    1.1 运行以下命令新建文件。
vim <网站根目录>/phpinfo.php  #将<网站根目录>替换为您配置的网站根目录。

vim <网站根目录>/phpinfo.php #将<网站根目录>替换为您配置的网站根目录。
在这里插入图片描述
本教程配置的网站根目录为/usr/share/nginx/html,因此命令为:

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

按i进入编辑模式。

输入下列内容,函数phpinfo()​会展示PHP的所有配置信息。

<?php echo phpinfo(); ?>

按Esc键后,输入:wq并回车以保存并关闭配置文件。

  1. 运行以下命令启动PHP-FPM。
systemctl start php-fpm
  1. 运行以下命令设置PHP-FPM开机自启动。
systemctl enable php-fpm

步骤八:测试访问LNMP平台

  1. 打开浏览器。
  2. 在地址栏输入http://<ECS实例公网IP地址>/phpinfo.php。
    返回结果如下图所示,表示LNMP环境部署成功。在这里插入图片描述

后续步骤

  1. 测试访问LNMP平台成功后,建议您运行以下命令将phpinfo.php文件删除,消除安全隐患。
rm -rf <网站根目录>/phpinfo.php   #将<网站根目录>替换为您在nginx.conf中配置的网站根目录
  1. 本教程配置的网站根目录为/usr/share/nginx/html,因此命令为:
rm -rf /usr/share/nginx/html/phpinfo.php

后记

即使再忙,也要记得热爱生活!


坚持分享,坚持原创,喜欢博主的靓仔靓女们可以看看博主的首页博客!
您的点赞与收藏是我分享博客的最大赞赏!
博主博客地址: https://blog.csdn.net/weixin_43967679

猜你喜欢

转载自blog.csdn.net/weixin_43967679/article/details/112384953