nginx动静分离实验
目录
nginx动静分离实验
实验环境说明:
主机名字 | IP | 安装服务 | 系统 |
---|---|---|---|
nginx | 192.168.26.10 | nginx | centos8 |
lamp | 192.168.26.11 | LAMP | centos8 |
lnmp | 192.168.91.12 | LNMP | centos8 |
安装nginx服务
//创建系统用户nginx
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
//关闭防火墙和selinux
[root@nginx ~]# setenforce 0
[root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@nginx ~]# systemctl disable --now firewalld.service
//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx源码包,并解压安装
[root@nginx ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz
[root@nginx ~]# tar xf nginx-1.20.2.tar.gz
[root@nginx ~]# cd nginx-1.20.2
[root@nginx nginx-1.20.2]# ./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@nginx nginx]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//安装成功
[root@nginx nginx-1.20.2]# cd /usr/local/nginx/
[root@nginx nginx]# ls
conf html logs sbin
//配置环境变量
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh
[root@nginx ~]# which nginx
/usr/local/nginx/sbin/nginx
//启动nginx
[root@nginx ~]# nginx
[root@nginx ~]# ss -anlt
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 [::]:*
//编写service文件
[root@nginx ~]# vi /usr/lib/systemd/system/nginx.service
[root@nginx ~]# 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=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
[root@nginx ~]# systemctl daemon-reload
[root@nginx ~]# systemctl enable --now nginx.service
[root@nginx ~]# ss -anlt
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 [::]:*
web页面访问
LAMP安装
安装工具包和依赖包
[root@LAMP ~]# dnf -y install epel-release
[root@LAMP ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make ncurses-devel openssl cmake ncurses-compat-libs libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel libzip-devel http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
安装httpd
//创建apache用户
[root@LAMP ~]# useradd -rMs /sbin/nologin apache
//下载apr,apr-util,httpd源码包
[root@LAMP ~]# cd /usr/src
[root@LAMP src]# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz
[root@LAMP src]# wget https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz
[root@LAMP src]# wget https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
//编译apr
[root@LAMP src]# tar xf apr-1.7.0.tar.gz
[root@LAMP src]# cd apr-1.7.0/
[root@LAMP apr-1.7.0]# vim configure
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
#$RM "$cfgfile" 将这行注释掉或者删掉
[root@LAMP apr-1.7.0]# ./configure --prefix=/usr/local/apr
[root@LAMP apr-1.7.0]# make && make install
//编译apr-util
[root@LAMP apr-1.7.0]# cd /usr/src
[root@LAMP src]# tar xf apr-util-1.6.1.tar.gz
[root@LAMP src]# cd apr-util-1.6.1/
[root@LAMP apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@LAMP apr-util-1.6.1]# make && make install
//编译httpd
[root@LAMP apr-util-1.6.1]# cd /usr/src/
[root@LAMP src]# tar xf httpd-2.4.54.tar.gz
[root@LAMP src]# cd httpd-2.4.54/
[root@LAMP httpd-2.4.5]# ./configure --prefix=/usr/local/apache \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@LAMP httpd-2.4.5]# make && make install
[root@LAMP httpd-2.4.54]# cd /usr/local/apache/
[root@LAMP apache]# ls
bin build cgi-bin conf error htdocs icons include logs man manual modules
//配置apache全局环境变量
[root@LAMP apache]# echo "export PATH=\$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh
[root@LAMP apache]# source /etc/profile.d/httpd.sh
[root@LAMP apache]# which apachectl
/usr/local/apache/bin/apachectl
//配置头文件
[root@LAMP apache]# ln -s /usr/local/apache/include /usr/include/httpd
[root@LAMP apache]# ll /usr/include/httpd
lrwxrwxrwx 1 root root 25 Oct 19 20:33 /usr/include/httpd -> /usr/local/apache/include
//配置man 文档
[root@LAMP apache]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/apache/man
//将主配置文件里的'#ServerName'行的注释去掉
[root@LAMP apache]# sed -i '/#ServerName/s/#//g' /usr/local/apache/conf/httpd.conf
//使用systemd风格管理apache
[root@LAMP apache]# vim /usr/lib/systemd/system/httpd.service
[Unit]
Description=web server daemon
Documentation=man:httpd(5)
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/apache/bin/apachectl start
ExecStop=/usr/local/apache/bin/apachectl stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@LAMP apache]# systemctl daemon-reload
[root@LAMP apache]# systemctl enable --now httpd.service
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
安装mysql
//下载mysql安装包
[root@LAMP apache]# cd /usr/src/
[root@LAMP src]#wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
//创建mysql用户
[root@LAMP src]# useradd -rMs /sbin/nologin mysql
//创建数据存放目录
[root@LAMP src]# mkdir /opt/data
[root@LAMP src]# chown -R mysql.mysql /opt/data
//安装mysql
[root@LAMP src]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@LAMP src]# cd /usr/local/
[root@LAMP local]# ln -s mysql-8.0.28-linux-glibc2.12-x86_64 mysql
[root@LAMP local]# chown -R mysql.mysql mysql*
[root@LAMP local]# ll -d mysql*
lrwxrwxrwx 1 mysql mysql 35 Oct 19 20:41 mysql -> mysql-8.0.28-linux-glibc2.12-x86_64
drwxr-xr-x 9 mysql mysql 129 Oct 19 20:41 mysql-8.0.28-linux-glibc2.12-x86_64
[root@LAMP local]# cd mysql
[root@LAMP mysql]# ls
bin docs include lib LICENSE man README share support-files
//配置全局环境变量
[root@LAMP mysql]# echo "export PATH=\$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
[root@LAMP mysql]# source /etc/profile.d/mysql.sh
[root@LAMP mysql]# which mysql
/usr/local/mysql/bin/mysql
//配置mysql库文件
[root@LAMP mysql]# echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf
[root@LAMP mysql]# ldconfig
//配置mysql头文件
[root@LAMP mysql]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@LAMP mysql]# ll /usr/include/mysql
lrwxrwxrwx. 1 root root 24 Sep 7 15:21 /usr/include/mysql -> /usr/local/mysql/include
//配置mysql man文档
[root@LAMP mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
//初始化mysql
[root@LAMP mysql]# /usr/local/mysql/bin/mysqld --initialize --user mysql --datadir /opt/data/
2022-10-19T23:55:07.587268Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: b-%2GDtGkWcW
//root@localhost: 后面的就是mysql初始化密码,要保存好
[root@LAMP mysql]# echo 'b-%2GDtGkWcW' > /tmp/pass
//生成mysql配置文件
[root@LAMP mysql]# vim /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
//使用systemd风格管理mysql服务
[root@LAMP mysql]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@LAMP mysql]# systemctl daemon-reload
[root@LAMP mysql]# systemctl enable --now mysqld
//初始化mysql密码
[root@LAMP mysql]# mysql -uroot -p'b-%2GDtGkWcW'
mysql> alter user root@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
安装php
//下载php源码包
[root@LAMP mysql]# cd /usr/src
[root@LAMP src]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
//解压php
[root@LAMP src]# tar xf php-8.1.11.tar.gz
[root@LAMP src]# cd php-8.1.11/
//编译php
[root@LAMP php-8.1.11]# ./configure --prefix=/usr/local/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
…………
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
[root@LAMP php-8.1.11]# make && make install
[root@LAMP php-8.1.11]# cd /usr/local/php8/
[root@LAMP php8]# ls
bin etc include lib php sbin var
//配置php全局环境变量
[root@LAMP php8]# echo "export PATH=\$PATH:/usr/local/php8/bin" > /etc/profile.d/php.sh
[root@LAMP php8]# source /etc/profile.d/php.sh
//配置php头文件
[root@LAMP php8]# ln -s /usr/local/php8/include /usr/include/php
[root@LAMP php8]# ll /usr/include/php
lrwxrwxrwx 1 root root 23 Oct 19 21:02 /usr/include/php -> /usr/local/php8/include
//配置php库文件
[root@LAMP php8]# echo "/usr/local/php8/lib" > /etc/ld.so.conf.d/php.conf
[root@LAMP php8]# ldconfig
//配置php-fpm
[root@LAMP php8]# cp /usr/src/php-8.1.11/php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@LAMP php8]# cp /usr/src/php-8.1.11/sapi/fpm/init.d.php-fpm /usr/local/php8/bin/php-fpm
[root@LAMP php8]# chmod +x /usr/local/php8/bin/php-fpm
[root@LAMP php8]# cd /usr/local/php8/etc/
[root@LAMP etc]# cp php-fpm.conf.default php-fpm.conf
[root@LAMP etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
//使用systemd风格管理php-fpm
[root@LAMP etc]# vim /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/php8/bin/php-fpm start
ExecStop=/usr/local/php8/bin/php-fpm stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@LAMP etc]# systemctl daemon-reload
[root@LAMP etc]# systemctl enable --now php-fpm.service
配置httpd
//启动代理模块
[root@LAMP etc]# cd /usr/local/apache/
[root@LAMP apache]# vim conf/httpd.conf
#取消如下内容的注释
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
//配置虚拟主机,让php页面监听在82端口
[root@LAMP ~]# cd /usr/local/apache/conf/extra/
[root@LAMP extra]# vi httpd-vhosts.conf
listen 82
<VirtualHost *:82>
DocumentRoot "/usr/local/apache/htdocs/php"
ServerName php.example.com
ErrorLog "logs/php.example.com-error_log"
CustomLog "logs/php.example.com-access_log" common
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/php/$1
<Directory "/usr/local/apache/htdocs/php">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>、
//创建虚拟主机的目录并生成php测试页面
[root@LAMP apache]# mkdir /usr/local/apache/htdocs/php
[root@LAMP apache]# cd /usr/local/apache/htdocs/php
[root@LAMP php]# cat > index.php <<EOF
<?php
phpinfo();
?>
EOF
[root@LAMP php]# chown -R apache.apache /usr/local/apache/htdocs/
[root@LAMP apache]# vim /usr/local/apache/conf/httpd.conf
#取消此行注释
Include conf/extra/httpd-vhosts.conf
#搜索AddType,加上以下内容
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php #添加此行
AddType application/x-httpd-php-source .phps #添加此行
#在'DirectoryIndex index.html'后面添加index.php
DirectoryIndex index.html index.php
//重启httpd
[root@LAMP php]# systemctl restart httpd.service
访问测试
LNMP安装
安装nginx
//创建系统用户nginx
[root@lnmp ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@lnmp ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
//关闭防火墙和selinux
[root@lnmp ~]# setenforce 0
[root@lnmp ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@lnmp ~]# systemctl disable --now firewalld.service
//创建日志存放目录
[root@lnmp ~]# mkdir -p /var/log/nginx
[root@lnmp ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx源码包,并解压安装
[root@lnmp ~]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
[root@lnmp ~]# tar xf nginx-1.22.0.tar.gz
[root@lnmp ~]# cd nginx-1.22.0
[root@lnmp nginx-1.22.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@lnmp nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
//安装成功
[root@lnmp nginx-1.22.0]# cd /usr/local/nginx/
[root@lnmp nginx]# ls
conf html logs sbin
//配置环境变量
[root@lnmp nginx]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp nginx]# source /etc/profile.d/nginx.sh
[root@lnmp nginx]# which nginx
/usr/local/nginx/sbin/nginx
//启动nginx
[root@lnmp nginx]# nginx
[root@lnmp nginx]# 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 [::]:*
//编写service文件
[root@lnmp nginx]# vi /usr/lib/systemd/system/nginx.service
[root@lnmp nginx]# 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=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
[root@lnmp nginx]# systemctl daemon-reload
[root@lnmp nginx]# systemctl enable --now nginx.service
[root@lnmp nginx]# 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 [::]:*
安装mysql
//安装依赖包
[root@lnmp ~]# dnf -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs
//创建用户和组
[root@lnmp ~]# useradd -r -M -s /sbin/nologin mysql
//下载二进制格式的mysql软件包
[root@lnmp ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
//解压软件至/usr/local/
[root@lnmp ~]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@lnmp ~]# cd /usr/local/
[root@lnmp local]# ls
bin games lib libexec nginx share
etc include lib64 mysql-8.0.28-linux-glibc2.12-x86_64 sbin src
[root@lnmp local]# mv mysql-8.0.28-linux-glibc2.12-x86_64/ mysql
[root@lnmp local]# ls
bin games lib libexec nginx share
etc include lib64 mysql sbin src
//修改目录/usr/local/mysql的属主属组
[root@lnmp local]# chown -R mysql.mysql mysql
//配置mysql
[root@lnmp local]# ls /usr/local/mysql/
bin docs include lib LICENSE man README share support-files
[root@lnmp local]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@lnmp local]# echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf
[root@lnmp local]# vim /etc/man_db.conf
bash: vim: command not found
[root@lnmp local]# vi /etc/man_db.conf
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man //添加这行
//添加环境变量
[root@lnmp local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp local]# source /etc/profile.d/mysql.sh
[root@lnmp local]# which mysql
/usr/local/mysql/bin/mysql
//建立数据存放目录
[root@lnmp local]# mkdir -p /opt/data
[root@lnmp local]# chown -R mysql.mysql /opt/data/
[root@lnmp local]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Oct 20 00:01 data
//初始化数据库
[root@lnmp ~]# mysqld --initialize --user=mysql --datadir=/opt/data/
2022-10-19T16:02:17.849376Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: udb*9QO3<tjA
[root@lnmp ~]# echo 'udb*9QO3<tjA' > pass
//卸载mariadb的所有包
[root@lnmp ~]# dnf -y remove mariadb*
//生成配置文件
[root@lnmp ~]# vi /etc/my.cnf
[root@lnmp ~]# 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@lnmp ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lnmp ~]# vi /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/opt/data
[root@lnmp ~]# chmod +x /etc/init.d/mysqld
//启动mysql 并设置开机自启
[root@lnmp ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/lnmp.err'.
. SUCCESS!
[root@lnmp ~]# 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 [::]:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
[root@lnmp ~]# chkconfig --add mysqld
[root@lnmp ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
//修改密码为123456
//使用临时密码登录
[root@lnmp ~]# cat pass
udb*9QO3<tjA
[root@lnmp ~]# mysql -uroot -p'udb*9QO3<tjA'
......
mysql> ALTER user 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.02 sec)
mysql> exit
Bye
安装php
//安装依赖包
[root@lnmp ~]# yum -y install epel-release
[root@lnmp ~]# dnf -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make vim wget libsqlite3x-devel libzip-devel
[root@lnmp ~]# dnf -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
//下载php
[root@lnmp ~]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
//编译安装php
[root@lnmp ~]# tar xf php-8.1.11.tar.gz
[root@lnmp ~]# cd php-8.1.11
[root@lnmp php-8.1.11]# ./configure --prefix=/usr/local/php8 --with-config-file-path=/etc --enable-fpm --enable-inline-optimization --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-json --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@lnmp php-8.1.11]# make
[root@lnmp php-8.1.11]# make install
//安装后配置
[root@lnmp php-8.1.11]# echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php8.sh
[root@lnmp php-8.1.11]# source /etc/profile.d/php8.sh
[root@lnmp php-8.1.11]# which php
/usr/local/php8/bin/php
[root@lnmp php-8.1.11]# php -v
PHP 8.1.11 (cli) (built: Oct 20 2022 00:42:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.11, Copyright (c) Zend Technologies
//配置php-fpm
[root@lnmp php-8.1.11]# cp php.ini-production /etc/php.ini
cp: overwrite '/etc/php.ini'? y
[root@lnmp php-8.1.11]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@lnmp php-8.1.11]# chmod +x /etc/init.d/php-fpm
[root@lnmp php-8.1.11]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@lnmp php-8.1.11]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
//启动php-fpm
[root@lnmp php-8.1.11]# service php-fpm start
Starting php-fpm done
[root@lnmp php-8.1.11]# 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 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
//设置开机自启
[root@lnmp php-8.1.11]# chkconfig --add php-fpm
[root@lnmp php-8.1.11]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nginx配置
// 配置网页文件
[root@lnmp ~]# cd /usr/local/nginx/html/
[root@lnmp html]# vim index.php
[root@lnmp html]# cat index.php
<?php
phpinfo();
?>
[root@lnmp html]# chown -R nginx.nginx index.php
//修改配置文件
[root@lnmp nginx]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
// 重新加载nginx
[root@lnmp nginx]# nginx -s reload
web 页面访问
以上页面都访问成功之后就可以开始配置动静分离了
在nginx主机上配置动静分离
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
upstream test1 {
server 192.168.26.12;
server 192.168.26.11;
}
upstream test2 {
server 192.168.26.12;
server 192.168.26.11:82;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://test1;
}
location ~ \.php$ {
proxy_pass http://test2;
}
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s reload
验证
访问nginx主机的IP
此时我们访问到的是静态网页,并且nginx和apache做轮询
刷新,重新加载
访问nginx主机的IP+index.php
刷新,重新加载
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!