CentOS 7.6 搭建LNMP环境

发布于 2022-02-21  927 次阅读


  • Linux:Linux 操作系统,本文以 CentOS 7.6 为例

  • Nginx:Web 服务器,本文以 Nginx 1.20.1/1.21.6 为例

  • MySQL:数据库,本文以 MySQL 5.7 为例
  • PHP:脚本语言,本文以 PHP 7.4 为例

1. 安装Nginx

1.1. 安装:yum install nginx


附:
- 如果要使用官方安装则创建并编辑:nano /etc/yum.repos.d/nginx.repo,加入以下内容:

[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/mainline/<OS>/<OSRELEASE>/$basearch/
gpgcheck=0
enabled=1

代表操作系统,如填入:centos
代表OS发行版本号,如:centos则填入7
例:CentOS 7为:

[nginx]
name=nginx repo
baseurl=https://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
  • yum update,更新刚才添加的仓库列表,出现如下提示(注意其中的nginx):
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
epel                                                                                                                                      | 4.7 kB  00:00:00     
extras                                                                                                                                    | 2.9 kB  00:00:00     
nginx                                                                                                                                     | 2.9 kB  00:00:00     
os                                                                                                                                        | 3.6 kB  00:00:00     
updates                                                                                                                                   | 2.9 kB  00:00:00     
nginx/x86_64/primary_db                                                                                                                   | 233 kB  00:00:01 
  • yum install nginx

1.2. 配置:

  • 首先查看 cat /etc/nginx/nginx.conf,如果末尾内容含有:include /etc/nginx/conf.d/*.conf;,则表示该版本的Nginx配置项不在/etc/nginx/nginx.conf,而是在:/etc/nginx/conf.d/default.conf

  • 本文中的示例在yum install nginx的1.20.1为:/etc/nginx/nginx.conf,如果使用文中的官方安装方式1.21.6版本为:/etc/nginx/conf.d/default.conf

1.3. 找到 server{...},并将 server 大括号中相应的配置信息替换为如下内容。用于取消对 IPv6 地址的监听,同时配置 Nginx,实现与 PHP 的联动,本文设置网页的存放路径为:/usr/share/nginx/html

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;
    }
}

解释:开启Nginx对PHP的支持(此步骤可以在安装完PHP后再做,如果你不需要PHP现在可以不做):

  • 网页根目录移动到文件中的server_name localhost;下一行:
    root /usr/share/nginx/html;

  • 使其支持.php 文件:

location / {
    index  index.php index.html index.htm;
}
  • 配置对于.php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:
location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    include        fastcgi_params;
}

1.4. 启用并启动nginx:systemctl enable --now nginx

1.5. 验证安装:curl -I 127.0.0.1,随后出现反馈信息(此步骤可以在安装完PHP后再做,如果你不需要PHP可以现在做):

HTTP/1.1 200 OK
Server: nginx/1.21.6
Date: Mon, 21 Feb 2022 05:02:53 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 25 Jan 2022 15:25:20 GMT
Connection: keep-alive
ETag: "61f01660-267"
Accept-Ranges: bytes

2. 安装MySQL

此处是参考官方

说明教程:MySQL :: A Quick Guide to Using the MySQL Yum Repository

仓库地址:MySQL :: Download MySQL Yum Repository

2.1. 下载官方仓库:rpm -Uvh ``https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm

2.2. 此链接是默认下载MySQL 8.0版本的,如果需要下载5.7的话则要禁用8.0子存储库:

  • yum-config-manager --disable mysql80-community
  • yum-config-manager --enable mysql57-community
  • repolist enabled | grep mysql检查其输出来验证是否启用和禁用了正确的子存储库

2.3. 安装:yum install mysql-community-server
2.4. 启用并启动MySQL:systemctl enable --now mysqld
2.5. 安装完成后,会默认提供一个临时密码输入:grep 'temporary password' /var/log/mysqld.log

2.6. 使用这个临时密码来进行,初装的安全配置,运行:mysql_secure_installation,随后根据提示按自身需求设置:

  • 输入 y 并按 Enter 开始相关配置。
  • 设置 MySQL 密码并按 Enter ,输入密码默认不显示。
  • 再次输入密码并按 Enter,输入 y 确认设置该密码。
  • 输入 y 并按 Enter,移除匿名用户。
  • 设置是否禁止远程连接 MySQL:
  • 禁止远程连接:输入 y 并按 Enter。
  • 允许远程连接:输入 n 并按 Enter。
  • 输入 y 并按 Enter,删除 test 库及对 test 库的访问权限。
  • 输入 y 并按 Enter,重新加载授权表。

3. 安装PHP

3.1. 下载epel源:

yum install epel-release
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install yum-utils

3.2. 安装:

yum install php74-php-gd php74-php-pdo php74-php-mbstring php74-php-cli php74-php-fpm php74-php-mysqlnd

:官方安装方法为编译安装,参考本文结尾。

3.3. 启用并启动PHP:systemctl enable --now php74-php-fpm

3.4. 运行:php74 -v ,测试是否安装成功,成功的话如会显示PHP版本号

4. 验证Nginx和PHP的环境配置

4.1. 创建测试文件:
- systemctl restar nginx

echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php

4.2. 浏览器中访问如下地址,查看环境配置是否成功。

http://服务器实例IP

4.3. 环境成功配置的话,进入后会显示一个PHP信息画面。

参考:


一沙一世界,一花一天堂。君掌盛无边,刹那成永恒。