lnmp部署
环境准备
主机名 | ip地址 | 环境说明 |
---|---|---|
node1 | 192.168.200.20 | nginx、mysql、php |
准备工作:
配置yum源
[root@node1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2318 100 2318 0 0 12010 0 --:--:-- --:--:-- --:--:-- 12010
[root@node1 ~]#
[root@node1 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo
[root@node1 ~]#
关闭防火墙和selinux
[root@node1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node1 ~]# setenforce 0
nginx部署
nginx的安装与配置
创建nginx用户
[root@node1 ~]# useradd -r -M -s /sbin/nologin nginx
[root@node1 ~]#
[root@node1 ~]# id nginx
uid=994(nginx) gid=991(nginx) groups=991(nginx)
[root@node1 ~]#
安装依赖包
[root@node1 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
Last metadata expiration check: 0:04:46 ago on Wed 18 Oct 2023 02:40:06 PM CST.
...
libxcb-devel-1.13.1-1.el8.x86_64 openssl-devel-1:1.1.1k-5.el8_5.x86_64 pcre2-devel-10.32-2.el8.x86_64
pcre2-utf16-10.32-2.el8.x86_64 pcre2-utf32-10.32-2.el8.x86_64 xorg-x11-proto-devel-2020.1-3.el8.noarch
zlib-devel-1.2.11-17.el8.x86_64
Complete!
[root@node1 ~]#
创建日志存放目录,给目录添加属主和属组
[root@node1 ~]# mkdir /var/log/nginx
[root@node1 ~]# chown -R nginx.nginx /var/log/nginx
[root@node1 ~]# ll /var/log/
total 2816
...
-rw-------. 1 root root 1417506 Sep 24 17:32 messages-20230924
drwxr-xr-x 2 nginx nginx 6 Oct 18 14:47 nginx
drwx------. 2 root root 6 Jul 28 10:38 private
...
[root@node1 ~]#
下载并解压nginx软件包
[root@node1 ~]# cd /usr/src/
[root@node1 src]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
--2023-10-18 15:06:38-- http://nginx.org/download/nginx-1.24.0.tar.gz
...
Saving to: ‘nginx-1.24.0.tar.gz’
nginx-1.24.0.tar.gz 100%[===========================================================>] 1.06M 968KB/s in 1.1s
2023-10-18 15:06:40 (968 KB/s) - ‘nginx-1.24.0.tar.gz’ saved [1112471/1112471]
[root@node1 src]#
[root@node1 src]# tar xf nginx-1.24.0.tar.gz
[root@node1 src]# ls
debug kernels nginx-1.24.0 nginx-1.24.0.tar.gz
[root@node1 src]#
编译安装nginx
[root@node1 src]# cd nginx-1.24.0
[root@node1 nginx-1.24.0]#
[root@node1 nginx-1.24.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@node1 nginx-1.24.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@node1 nginx-1.24.0]#
[root@node1 nginx-1.24.0]# make install
[root@node1 ~]# ls /usr/local/nginx/
conf html logs sbin
[root@node1 ~]#
nginx配置
配置环境变量
[root@node1 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@node1 ~]# source /etc/profile.d/nginx.sh
[root@node1 ~]#
[root@node1 ~]# ls /usr/local/nginx/sbin/
nginx
[root@node1 ~]#
配置system功能
[root@node1 ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service
[root@node1 ~]# vi /usr/lib/systemd/system/nginx.service
[root@node1 ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
[Install]
WantedBy=multi-user.target
[root@node1 ~]#
启动nginx
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@node1 ~]#
[root@node1 ~]# systemctl status nginx
● nginx.service - nginx server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2023-10-18 15:42:23 CST; 4s ago
Process: 32802 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
...
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@node1 ~]#
网页访问测试
mysql部署
安装所需的依赖包
[root@node1 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
创建mysql用户和组
[root@node1 ~]# groupadd -r -g 306 mysql
[root@node1 ~]# useradd -r -M -s /sbin/nologin -g 306 -u 306 mysql
[root@node1 ~]#
[root@node1 ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
[root@node1 ~]#
安装mysql
下载并解压mysql二进制包
下载地址:Download MySQL
[root@node1 ~]# tar xf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@node1 ~]#
[root@node1 ~]# ls /usr/local/
bin etc games include lib lib64 libexec mysql-5.7.39-linux-glibc2.12-x86_64 nginx sbin share src
[root@node1 ~]#
建立软链接
[root@node1 ~]# ln -sv /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64 /usr/local/mysql
'/usr/local/mysql' -> '/usr/local/mysql-5.7.39-linux-glibc2.12-x86_64'
[root@node1 ~]#
[root@node1 ~]# ll /usr/local/
total 0
drwxr-xr-x. 2 root root 45 Sep 23 14:50 bin
drwxr-xr-x. 3 root root 60 Sep 28 10:02 etc
drwxr-xr-x. 2 root root 6 Jun 22 2021 games
drwxr-xr-x. 2 root root 6 Jun 22 2021 include
drwxr-xr-x. 3 root root 21 Sep 23 14:50 lib
drwxr-xr-x. 3 root root 17 Jul 28 10:38 lib64
drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec
lrwxrwxrwx 1 root root 46 Oct 18 16:14 mysql -> /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x 9 root root 129 Oct 18 16:14 mysql-5.7.39-linux-glibc2.12-x86_64
drwxr-xr-x 11 root root 151 Oct 18 15:26 nginx
drwxr-xr-x. 2 root root 27 Sep 23 14:50 sbin
drwxr-xr-x. 5 root root 49 Jul 28 10:38 share
drwxr-xr-x. 2 root root 6 Jun 22 2021 src
[root@node1 ~]#
修改目录/usr/local/mysql的属主属组
[root@node1 ~]# chown -R mysql.mysql /usr/local/mysql
[root@node1 ~]#
[root@node1 ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 46 Oct 18 16:14 /usr/local/mysql -> /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64
[root@node1 ~]#
添加环境变量
[root@node1 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@node1 ~]#
[root@node1 ~]# source /etc/profile.d/mysql.sh
[root@node1 ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@node1 ~]#
创建存放数据的目
[root@node1 ~]# mkdir /opt/data
[root@node1 ~]# chown -R mysql.mysql /opt/data/
[root@node1 ~]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Oct 18 16:17 data
[root@node1 ~]#
配置mysql软链接
[root@node1 ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@node1 ~]#
[root@node1 ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@node1 ~]#
[root@node1 ~]# ldconfig
[root@node1 ~]#
[root@node1 ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@node1 ~]#
生成my.cnf配置文件
[root@node1 ~]# vi /etc/my.cnf
[root@node1 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
[root@node1 ~]#
配置服务启动脚本
[root@node1 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@node1 ~]#
[root@node1 ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@node1 ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
[root@node1 ~]#
添加软链接
[root@node1 ~]# ln -s libncurses.so.6.1 /usr/lib64/libncurses.so.5
[root@node1 ~]#
[root@node1 ~]# ln -s libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
[root@node1 ~]#
初始化数据库
[root@node1 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
启动数据库
[root@node1 ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/node1.example.com.err'.
SUCCESS!
[root@node1 ~]#
使用临时密码登录,修改数据库密码
[root@node1 ~]# mysql -uroot -p'7,?fHoqfHuyd'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
...
mysql>
mysql> set password = password('redhat123!');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql>
mysql> quit;
Bye
[root@node1 ~]#
使用修改后的密码登录
[root@node1 ~]# mysql -uroot -predhat123!
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> quit;
Bye
[root@node1 ~]#
php部署
配置epel源
[root@node1 ~]# yum -y install https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@node1 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo epel-modular.repo epel.repo epel-testing-modular.repo epel-testing.repo
[root@node1 ~]#
[root@node1 ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@node1 ~]#
[root@node1 ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@node1 ~]#
安装所需依赖包
[root@node1 ~]# wget http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
[root@node1 ~]# yum -y install libxml2-devel bzip2 bzip2-devel libcurl-devel libjpeg-turbo-devel libpng-devel freetype-devel readline-devel sqlite-devel libzip-devel oniguruma-devel-6.8.2-2.el8.x86_64.rpm
下载并解压php软件包
[root@node1 ~]# cd /usr/src/
[root@node1 src]#
[root@node1 src]# wget https://www.php.net/distributions/php-8.2.10.tar.gz
...
Saving to: ‘php-8.2.10.tar.gz’
php-8.2.10.tar.gz 100%[===========================================================>] 18.22M 781KB/s in 14s
2023-10-18 16:47:56 (1.31 MB/s) - ‘php-8.2.10.tar.gz’ saved [19104774/19104774]
[root@node1 src]#
[root@node1 src]# tar xf php-8.2.10.tar.gz
[root@node1 src]#
[root@node1 src]# ls
debug kernels nginx-1.24.0 nginx-1.24.0.tar.gz php-8.2.10 php-8.2.10.tar.gz
[root@node1 src]#
编译安装php
[root@node1 src]# cd php-8.2.10
[root@node1 php-8.2.10]#
[root@node1 php-8.2.10]# ./configure --prefix=/opt/php8 \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@node1 php-8.2.10]# make && make install
[root@node1 php-8.2.10]#
[root@node1 php-8.2.10]# ls /opt/
data php8
[root@node1 php-8.2.10]#
配置php环境变量
[root@node1 ~]# echo 'export PATH=/opt/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@node1 ~]#
[root@node1 ~]# source /etc/profile.d/php8.sh
[root@node1 ~]# which php
/opt/php8/bin/php
[root@node1 ~]#
配置php-fpm
[root@node1 ~]# cd /usr/src/php-8.2.10
[root@node1 php-8.2.10]#
[root@node1 php-8.2.10]# cp php.ini-production /etc/php.ini
[root@node1 php-8.2.10]#
[root@node1 php-8.2.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@node1 php-8.2.10]#
[root@node1 php-8.2.10]# chmod +x /etc/rc.d/init.d/php-fpm
[root@node1 php-8.2.10]# ll /etc/rc.d/init.d/php-fpm
-rwxr-xr-x 1 root root 2396 Oct 18 17:16 /etc/rc.d/init.d/php-fpm
[root@node1 php-8.2.10]#
[root@node1 php-8.2.10]# cp /opt/php8/etc/php-fpm.conf.default /opt/php8/etc/php-fpm.conf
[root@node1 php-8.2.10]#
[root@node1 php-8.2.10]# cp /opt/php8/etc/php-fpm.d/www.conf.default /opt/php8/etc/php-fpm.d/www.conf
[root@node1 php-8.2.10]#
[root@node1 php-8.2.10]# ll /opt/php8/etc/
total 20
-rw-r--r-- 1 root root 1178 Oct 18 17:08 pear.conf
-rw-r--r-- 1 root root 5351 Oct 18 17:16 php-fpm.conf
-rw-r--r-- 1 root root 5351 Oct 18 17:08 php-fpm.conf.default
drwxr-xr-x 2 root root 46 Oct 18 17:17 php-fpm.d
[root@node1 php-8.2.10]#
编辑php-fpm的配置文件,在配置文件最后添加内容
[root@node1 ~]# vi /opt/php8/etc/php-fpm.conf
[root@node1 ~]# tail /opt/php8/etc/php-fpm.conf
...
include=/opt/php8/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[root@node1 ~]#
启动php服务,查看端口
[root@node1 ~]# service php-fpm start
Starting php-fpm done
[root@node1 ~]#
[root@node1 ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
[root@node1 ~]#
nginx配置支持php
取消以下内容的注释,添加修改内容
[root@node1 ~]# cd /usr/local/nginx/conf/
[root@node1 conf]# vi nginx.conf
...
location / {
root html;
index index.html index.php index.htm;
}
...
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $Document_Root$fastcgi_script_name;
include fastcgi_params;
}
...
"nginx.conf" 117L, 2664C written
[root@node1 conf]#
添加php文件
[root@node1 conf]# cd /usr/local/nginx/html/
[root@node1 html]# ls
50x.html index.html
[root@node1 html]#
[root@node1 html]# vi index.php
[root@node1 html]# cat index.php
<?php
phpinfo();
?>
[root@node1 html]#
重启nginx和php服务
[root@node1 ~]# systemctl daemon-reload
[root@node1 ~]#
[root@node1 ~]# systemctl restart nginx
[root@node1 ~]#
[root@node1 ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@node1 ~]#