LNMP环境部署

一.Nginx离线部署

  • 所有依赖包都可以在pkgs.org中下载.

1. 安装相关依赖

离线安装相关依赖
1
2
3
4
5
6
7
8
9
rpm -ivh https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/k/kernel-headers-4.18.0-553.el8_10.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/g/glibc-headers-2.28-251.el8_10.2.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/g/glibc-devel-2.28-251.el8_10.2.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/m/mpfr-3.1.6-1.el8.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/AppStream/x86_64/os/Packages/l/libmpc-1.1.0-9.1.el8.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/AppStream/x86_64/os/Packages/c/cpp-8.5.0-22.el8_10.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/AppStream/x86_64/os/Packages/g/gcc-8.5.0-22.el8_10.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/AppStream/x86_64/os/Packages/l/libstdc++-devel-8.5.0-22.el8_10.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/AppStream/x86_64/os/Packages/g/gcc-c++-8.5.0-22.el8_10.x86_64.rpm
离线安装pcre、openssl、zlib依赖
1
2
3
4
5
6
7
8
9
10
rpm -ivh https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/p/pcre-devel-8.42-6.el8.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/l/libkadm5-1.18.2-27.el8_10.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/z/zlib-devel-1.2.11-26.el8.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/l/libcom_err-devel-1.45.6-5.el8.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/l/libsepol-devel-2.9-3.el8.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/l/libselinux-devel-2.9-8.el8.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/l/libverto-devel-0.3.2-2.el8.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/k/keyutils-libs-devel-1.5.10-9.el8.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/k/krb5-devel-1.18.2-27.el8_10.x86_64.rpm \
https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/Packages/o/openssl-devel-1.1.1k-12.el8_9.x86_64.rpm

2.创建组和用户

1
groupadd www && useradd www -s /sbin/nologin -g www

3.源代码安装Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 下载地址:https://nginx.org/en/download.html

./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--modules-path=/usr/local/nginx/modules \
--conf-path=/usr/local/nginx/nginx.conf \
--http-log-path=/usr/local/nginx/logs/access.log \
--error-log-path=/usr/local/nginx/logs/error.log \
--pid-path=/usr/local/nginx/nginx.pid \
--lock-path=/usr/local/nginx/nginx.lock \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module

make && make install

4.配置环境变量,启动nginx服务

1
2
3
4
# vim /etc/profile,在最后一行添加如下:

export NGINX_HOME=/usr/local/nginx
export PATH=$NGINX_HOME/sbin:$PATH

二.PHP离线部署

1.安装相关依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
dnf install -y gcc \
gcc-c++ \
make \
zlib-devel \
libxml2-devel \
libcurl-devel \
libjpeg-devel \
libpng-devel \
freetype-devel \
gmp-devel \
mariadb-devel \
autoconf \
bzip2-devel \
libicu-devel \
openssl-devel \
sqlite-devel \
libzip-devel \
libmcrypt \
libmcrypt-devel \
aspell \
aspell-devel \
recode \
recode-devel \
oniguruma \
oniguruma-devel \
libxslt \
libgpg-error-devel \
libgcrypt-devel \
libxslt-devel

2.创建组和用户

1
groupadd www && useradd www -s /sbin/nologin -g www

3.三步安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
wget https://www.php.net/distributions/php-8.2.20.tar.gz && tar -xvf php-8.2.20.tar.gz

./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-curl \
--with-freetype \
--enable-gd \
--with-jpeg \
--with-gettext \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml \
--with-mysqli \
--with-openssl \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--enable-sockets \
--with-mhash \
--with-ldap-sasl \
--with-xsl \
--with-zlib \
--with-zip \
-with-bz2 \
--with-iconv \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-pdo \
--enable-bcmath \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-sysvsem \
--enable-cli \
--enable-opcache \
--enable-intl \
--enable-calendar \
--enable-static \
--enable-mysqlnd

make && make install

4.复制配置文件

1
2
3
cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

5.创建php-fpm.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# cp /usr/local/src/php-8.2.20/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
# ln -s /usr/lib/systemd/system/php-fpm.service /etc/systemd/system/php-fpm.service

[Unit]
Description=The PHP FastCGI Process Manager
After=network.target

[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

PrivateTmp=true
ProtectSystem=false
PrivateDevices=true
ProtectKernelModules=true
ProtectKernelTunables=true
ProtectControlGroups=true
RestrictRealtime=true

RestrictAddressFamilies=AF_INET AF_INET6 AF_NETLINK AF_UNIX
RestrictNamespaces=true

[Install]
WantedBy=multi-user.target
  • nginx添加php-fpm
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    # nginx conf配置php支持
    server {
    listen 80;
    server_name 192.168.64.10;

    root /home/www/html;

    location / {
    index index.php index.html index.htm;
    }

    error_page 404 /404.html;
    location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }

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

三.MySQL 8 RPM包部署

1.安装依赖

1
dnf -y install compat-openssl10 ncurses-compat-libs openssl-devel perl-Getopt-Long perl-libs

2.下载rpm安装包

1
2
3
4
5
6
7
8
9
10
wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-client-8.0.37-1.el7.x86_64.rpm \
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-client-plugins-8.0.37-1.el7.x86_64.rpm \
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-common-8.0.37-1.el7.x86_64.rpm \
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-devel-8.0.37-1.el7.x86_64.rpm \
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-embedded-compat-8.0.37-1.el7.x86_64.rpm \
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-icu-data-files-8.0.37-1.el7.x86_64.rpm \
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-libs-8.0.37-1.el7.x86_64.rpm \
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-libs-compat-8.0.37-1.el7.x86_64.rpm \
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-server-8.0.37-1.el7.x86_64.rpm \
https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-8.0-community-el7-x86_64/mysql-community-server-debug-8.0.37-1.el7.x86_64.rpm

3.安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@iZ0jlh32uzw0d35tst4rdaZ src]# rpm -ivh mysql-community-common-8.0.37-1.el7.x86_64.rpm \
mysql-community-libs-compat-8.0.37-1.el7.x86_64.rpm \
mysql-community-libs-8.0.37-1.el7.x86_64.rpm \
mysql-community-client-plugins-8.0.37-1.el7.x86_64.rpm \
mysql-community-client-8.0.37-1.el7.x86_64.rpm \
mysql-community-icu-data-files-8.0.37-1.el7.x86_64.rpm \
mysql-community-server-8.0.37-1.el7.x86_64.rpm \
mysql-community-server-debug-8.0.37-1.el7.x86_64.rpm \
mysql-community-devel-8.0.37-1.el7.x86_64.rpm \
mysql-community-embedded-compat-8.0.37-1.el7.x86_64.rpm
warning: mysql-community-common-8.0.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID a8d3785c: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-common-8.0.37-1.e################################# [ 10%]
2:mysql-community-client-plugins-8.################################# [ 20%]
3:mysql-community-libs-8.0.37-1.el7################################# [ 30%]
4:mysql-community-client-8.0.37-1.e################################# [ 40%]
5:mysql-community-icu-data-files-8.################################# [ 50%]
6:mysql-community-server-8.0.37-1.e################################# [ 60%]
7:mysql-community-server-debug-8.0.################################# [ 70%]
8:mysql-community-libs-compat-8.0.3################################# [ 80%]
9:mysql-community-devel-8.0.37-1.el################################# [ 90%]
10:mysql-community-embedded-compat-8################################# [100%]

4.修改my.cnf配置文件中数据存储路径datadir和日志存放路径log-error

1
2
3
4
5
6
7
8
9
[mysqld]
datadir=/data/mysql/data
socket=/var/lib/mysql/mysql.sock

log-error=/data/mysql/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

lower_case_table_names=1
default-time-zone='+08:00'

5.创建mysqld.log文件,修改datadirlog-error路径的mysql权限

1
2
3
mkdir -p /data/mysql/{data,log}
touch /data/mysql/log/mysqld.log
chown -R mysql:mysql /data/mysql

6.启动mysqld服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@iZ0jlh32uzw0d35tst4rdaZ etc]# systemctl start mysqld
[root@iZ0jlh32uzw0d35tst4rdaZ etc]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2024-03-31 22:03:56 CST; 2min 54s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 31528 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 31602 (mysqld)
Status: "Server is operational"
Tasks: 37 (limit: 10548)
Memory: 474.9M
CGroup: /system.slice/mysqld.service
└─31602 /usr/sbin/mysqld

Mar 31 22:03:47 iZ0jlh32uzw0d35tst4rdaZ systemd[1]: Starting MySQL Server...
Mar 31 22:03:56 iZ0jlh32uzw0d35tst4rdaZ systemd[1]: Started MySQL Server.

7.查看初始化密码并登录

1
2
[root@iZ0jlh32uzw0d35tst4rdaZ etc]# grep 'temporary password' /data/mysql/log/mysqld.log
2024-03-31T14:03:52.435924Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: /HZszQik,3ur

8.SQL语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 初始化完成登录后需要修改root密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '6Ziaiyuepao!';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

# 创建数据库
mysql> CREATE DATABASE IF NOT EXISTS wpdb DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
Query OK, 1 row affected (0.02 sec)

# 创建用户和密码
mysql> CREATE USER 'wp'@'%' IDENTIFIED BY 'Wp!@#2024';
Query OK, 0 rows affected (0.02 sec)

# 授权
mysql> GRANT ALL ON wpdb.* TO 'wp'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> ALTER USER 'wp'@'%' IDENTIFIED WITH mysql_native_password BY 'Wp!@#2024';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)