• LNMP环境部署(CentOS7)


    简述


    在这里插入图片描述

    • Linux版本:CentOS7
    • Nginx版本:Nginx 1.20.1
    • MySQL版本:MySQL 5.7.36
    • PHP版本:PHP 7.0.33

    一、准备环境


    1.1 关闭防火墙

    systemctl stop firewalld
    systemctl disable firewalld
    
    • 1
    • 2

    1.2 关闭 SELinux

    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    setenforce 0
    
    • 1
    • 2

    二、安装


    1.1 安装 Nginx

    yum -y install nginx
    
    • 1

    1.2 安装 MySQL

    # 更新 yum 源
    rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
    # 安装 mysql
    yum -y install mysql-community-server --nogpgcheck
    
    • 1
    • 2
    • 3
    • 4

    1.3 安装 PHP

    # 添加 epel 源
    yum -y install https://repo.ius.io/ius-release-el7.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    # 添加 webtatic 源
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    # 安装 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
    • 2
    • 3
    • 4
    • 5
    • 6

    三、配置


    3.1 配置 Nginx

    1.1 备份

    cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
    
    • 1

    2.1 添加Nginx对PHP的支持

    # 若不添加此配置信息,后续您使用浏览器访问PHP页面时,页面将无法显示
    vim /etc/nginx/nginx.conf
            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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    效果如下:
    在这里插入图片描述

    3.2 配置 MySQL

    2.1 获取 root 用户的初始密码

    systemctl start mysqld
    grep 'temporary password' /var/log/mysqld.log
    
    • 1
    • 2

    2.2 配置 MySQL 的安全性

    # 运行以下命令配置 MySQL 的安全性
    mysql_secure_installation
    
    • 1
    • 2

    设置新密码

    Securing the MySQL server deployment.
    
    Enter password for user root: #输入上一步获取的root用户初始密码
    
    The existing password for the user account root has expired. Please set a new password.
    
    New password: #输入新密码。长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号包含()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
    
    Re-enter new password: #确认新密码。
    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 #您需要输入Y以确认使用新密码。
    
    #新密码设置完成后,需要再次验证新密码。
    New password:#再次输入新密码。
    
    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 #您需要输入Y,再次确认使用新密码。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    删除匿名用户

    Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y
    Success.
    
    • 1
    • 2

    禁止 root 远程登录

    Disallow root login remotely? (Press y|Y for Yes, any other key for No) :Y
    Success.
    
    • 1
    • 2

    删除test库以及用户对test库的访问权限

    Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y
     - Dropping test database...
    Success.
    
     - Removing privileges on test database...
    Success.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    重新加载授权表

    Reload privilege tables now? (Press y|Y for Yes, any other key for No) :Y
    Success.
    
    All done!
    
    • 1
    • 2
    • 3
    • 4

    3.3 配置 PHP

    3.1 获取网站根目录
    <网站根目录>是您在nginx.conf配置文件中location ~ .php$大括号内,配置的 root 参数值,如下图所示:
    在这里插入图片描述
    3.2 新建phpinfo.php文件,用于展示PHP信息

    # <网站根目录>/phpinfo.php
    # 由上一步获取的网站根目录拼接得 /usr/share/nginx/html/phpinfo.php
    vim /usr/share/nginx/html/phpinfo.php
    <?php echo phpinfo(); ?>
    
    • 1
    • 2
    • 3
    • 4

    四、启动服务


    systemctl restart nginx mysqld php-fpm
    systemctl enable nginx mysqld php-fpm
    
    • 1
    • 2

    五、测试验证


    5.1 验证 LNMP 是否部署成功

    浏览器的地址栏输入 http://< Nginx 的 IP 地址 >/phpinfo.php访问 LNMP 配置信息页面。
    访问结果如下图所示,表示 LNMP 环境部署成功:
    在这里插入图片描述

    5.2 验证 mysql 与 php 连通性是否正常

    # 登录数据库
    mysql -uroot -p
    # 创建库
    create database wordpress;
    # 创建用户并授权
    grant all on wordpress.*  to 'wordpress'@'%'  identified by '123456';
    grant all on wordpress.*  to 'wordpress'@'localhost'  identified by '123456';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    # 创建 php 访问页面
    cat >/usr/share/nginx/html/mysql.php<<'EOF'
    <?php
    $servername = "localhost";
    $username = "wordpress";
    $password = "123456";
    
    // 创建连接
    $conn = mysqli_connect($servername, $username, $password);
    
    // 检测连接
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    	echo "php连接MySQL数据库成功";
    ?>
    EOF
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    浏览器的地址栏输入http://< Nginx 的 IP 地址 >/mysql.php进行访问。
    访问结果如下图所示,表示 mysql 与 php 通信正常:
    在这里插入图片描述

    六、后续收尾工作


    测试访问LNMP配置信息页面后,建议您运行以下命令将 php 文件删除,消除数据泄露风险。

    rm -rf /usr/share/nginx/html/phpinfo.php
    rm -rf /usr/share/nginx/html/mysql.php
    
    • 1
    • 2

    七、扩展内容


    7.1、如何使用其它版本的 Nginx ?
    7.2、如何使用其它版本的 MySQL ?

  • 相关阅读:
    logistic回归模型—基于R
    容灾备份 | 看我使用Powershell操作FTP进行数据文件自动上传备份
    PHP代码审计--百家CMS4.1.4项目实战(下)
    构造函数继承
    基于Spring Boot应用Java的Stream流API
    MySQL——四、SQL语句(下篇)
    Leetcode.33 搜索旋转排序数组
    JS类的继承和实现原理详解
    【Lua基础 第2章】lua遍历table的方式、运算符、math库、字符串操作方法
    三国志14信息查询小程序(历史武将信息一览)制作更新过程03-主要页面的设计
  • 原文地址:https://blog.csdn.net/focus_lyh/article/details/126112531