部署LNMP
环境准备
主机名 | ip | 环境说明 |
---|---|---|
lnmp | 192.168.10.130 | nginx、mysql、pnode1p |
准备工作:
关闭防火墙和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
[root@node1 ~]# sed -i s/SELINUX=.*/SELINUX=disabled/ /etc/selinux/config
配置yum源
[root@node1 ~]# rm -rf /etc/yum.repos.d/*
[root@node1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo node1ttps://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 2495 100 2495 0 0 9450 0 --:--:-- --:--:-- --:--:-- 9450
[root@node1 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@node1 ~]# yum clean all
18 files removed
[root@node1 ~]# yum makecacnode1e
nginx部署
创建nginx用户
[root@node1 ~]# useradd -r -M -s /sbin/nologin nginx
[root@node1 ~]# id nginx
uid=995(nginx) gid=992(nginx) groups=992(nginx)
依赖包
[root@node1 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make --allowerasing
创建日志存放目录,给目录添加属主和属组
[root@node1 ~]# mkdir /var/log/nginx
[root@node1 ~]# chown -R nginx.nginx /var/log/nginx
[root@node1 ~]# ll -d /var/log/nginx
drwxr-xr-x 2 nginx nginx 6 Oct 22 21:33 /var/log/nginx
下载并解压nginx软件包
[root@node1 ~]# cd /usr/src/
[root@node1 src]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
[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]# rm -rf nginx-1.24.0.tar.gz
编译安装nginx
[root@node1 src]# cd 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]# make install
[root@node1 nginx-1.24.0]# ls /usr/local/nginx/
conf html logs sbin
[root@node1 nginx-1.24.0]#
nginx配置
配置环境变量
[root@node1 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@node1 ~]#
[root@node1 ~]# source /etc/profile.d/nginx.sh
[root@node1 ~]#
[root@node1 ~]# ls /usr/local/nginx/sbin/
nginx
[root@node1 ~]#
配置system功能
[root@node1 ~]# vim /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 ~]# systemctl status nginx
● nginx.service - nginx server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2023-10-22 21:42:05 CST; 3s ago
Process: 23895 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 23896 (nginx)
Tasks: 2 (limit: 23457)
Memory: 2.3M
CGroup: /system.slice/nginx.service
├─23896 nginx: master process /usr/local/nginx/sbin/nginx
└─23897 nginx: worker process
Oct 22 21:42:05 node1 systemd[1]: Starting nginx server daemon...
Oct 22 21:42:05 node1 systemd[1]: Started nginx server daemon.
[root@node1 ~]#
[root@node1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 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 ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
[root@node1 ~]#
安装mysql
下载并解压mysql二进制包
#下载网址
https://downloads.mysql.com/archives/community/
[root@node1 ~]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz
[root@node1 ~]# tar xf mysql-5.7.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
建立软链接
[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 ~]# ll /usr/local/mysql
lrwxrwxrwx 1 root root 46 Oct 22 21:51 /usr/local/mysql -> /usr/local/mysql-5.7.39-linux-glibc2.12-x86_64
[root@node1 ~]#
修改目录/usr/local/mysql的属主属组
[root@node1 ~]# chown -R mysql.mysql /usr/local/mysql
[root@node1 ~]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 46 Oct 22 21:51 /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 ~]#
[root@node1 ~]# chown -R mysql.mysql /opt/data/
[root@node1 ~]# ll /opt/
total 0
drwxr-xr-x 2 mysql mysql 6 Oct 22 21:54 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 ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@node1 ~]# ldconfig
[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 ~]# 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 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
......
2023-10-22T14:01:44.847863Z 1 [Note] A temporary password is generated for root@localhost: MFIig3rT+VJ)
启动数据库
[root@node1 ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/node1.err'.
SUCCESS!
[root@node1 ~]#
使用临时密码登录,修改数据库密码
[root@node1 ~]# mysql -uroot -p'MFIig3rT+VJ)'
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
使用修改后的密码登录
[root@node1 ~]# mysql -uroot -p123456
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> 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 ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@node1 ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
安装所需依赖包
[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]# wget https://www.php.net/distributions/php-8.2.10.tar.gz
[root@node1 src]# tar xf php-8.2.10.tar.xz
[root@node1 src]# ls
debug kernels nginx-1.24.0 php-8.2.10 php-8.2.10.tar.xz
[root@node1 src]#
编译php
[root@node1 php-8.2.10]# ./configure --prefix=/opt/php \
--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
配置php环境变量
[root@node1 ~]# echo 'export PATH=/opt/php/bin:$PATH' > /etc/profile.d/php.sh
[root@node1 ~]# source /etc/profile.d/php.sh
[root@node1 ~]# which php
/opt/php/bin/php
[root@node1 ~]#
配置php-fpm
[root@node1 ~]# cd /usr/src/php-8.2.10
[root@node1 php-8.2.10]# cp php.ini-production /etc/php.ini
[root@node1 php-8.2.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[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 2395 Oct 22 22:50 /etc/rc.d/init.d/php-fpm
[root@node1 php-8.2.10]# cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
[root@node1 php-8.2.10]# cp /opt/php/etc/php-fpm.d/www.conf.default /opt/php/etc/php-fpm.d/www.conf
[root@node1 ~]# ll /opt/php/etc/
total 20
-rw-r--r-- 1 root root 1167 Oct 22 22:41 pear.conf
-rw-r--r-- 1 root root 5439 Oct 22 22:53 php-fpm.conf
-rw-r--r-- 1 root root 5346 Oct 22 22:41 php-fpm.conf.default
drwxr-xr-x 2 root root 46 Oct 22 22:56 php-fpm.d
[root@node1 ~]#
编辑php-fpm的配置文件,在配置文件最后添加内容
[root@node1 ~]# vi /opt/php/etc/php-fpm.conf
[root@node1 ~]# tail /opt/php/etc/php-fpm.conf
; file.
; Relative path can also be used. They will be prefixed by:
; - the global prefix if it's been set (-p argument)
; - /opt/php otherwise
include=/opt/php/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 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[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; #这里添加index.php
}
...
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;
}
[root@node1 conf]#
添加php文件测试页面
[root@node1 conf]# cd /usr/local/nginx/html/
[root@node1 html]# ls
50x.html index.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 ~]# systemctl restart nginx
[root@node1 ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@node1 ~]#
}
[root@node1 conf]#
添加php文件测试页面
```shell
[root@node1 conf]# cd /usr/local/nginx/html/
[root@node1 html]# ls
50x.html index.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 ~]# systemctl restart nginx
[root@node1 ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@node1 ~]#