抛弃CentOS7,拥抱Rocky Linux,为什么不用Rocky Linux 10?因为服务器只提供了 Rocky Linux 9 的版本。
1、安装nginx
nginx版本:nginx-1.28.0.tar.gz
1.1、安装依赖
dnf -y install gcc gcc-c++ pcre-devel openssl-devel zlib-devel
1.2、编译安装
# 解压压缩包
tar zxvf nginx-1.28.0.tar.gz
# 进入源文件目录
cd nginx-1.28.0
# 执行configure
./configure --prefix=/sangfor/server/nginx --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module
# make && make install
make && make install
1.3、配置 nginx.service 脚本文件
文件位置在 /etc/systemd/system/nginx.service,脚本内容见:systemd unit 与 nginx.service
1.4、管理 nginx
| 操作 | 命令 | 说明 |
|---|---|---|
| 重新加载配置 | sudo systemctl daemon-reload |
修改service文件后执行 |
| 启动服务 | sudo systemctl start nginx |
立即启动服务 |
| 停止服务 | sudo systemctl stop nginx |
立即停止服务 |
| 重启服务 | sudo systemctl restart nginx |
强制重启服务 |
| 查看状态 | sudo systemctl status nginx |
显示服务运行状态 |
| 开机自启 | sudo systemctl enable nginx |
设置系统启动时自动运行 |
备注,CentOS、Rocky Linux、AlmaLinux 系列不需要加 sudo。
2、安装PHP
php版本:php-8.1.34.tar.gz
2.1、安装依赖
dnf -y install gcc gcc-c++ make cmake libxml2 libxml2-devel pcre pcre-devel zlib zlib-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libxml2-devel autoconf
dnf -y install sqlite-devel libtool autoconf automake oniguruma
2.2、需要手动安装的依赖
2.2.1、安装libzip
#卸载老版本的libzip
yum remove libzip
#下载安装libzip-1.2.0
wget https://libzip.org/download/libzip-1.2.0.tar.gz
tar zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure --prefix=/usr
make && make install
2.2.2、安装oniguruma
# 安装oniguruma
# 执行autogen.sh的时候需要authmake,而automake又需要libtool
tar zxvf src/oniguruma-6.9.5.tar.gz
cd oniguruma-6.9.5
./autogen.sh
.configure --prefix=/usr
make
make install
2.3、编译安装php
tar zxvf php-8.1.34.tar.gz
cd php-8.1.34
./configure --prefix=/sangfor/server/php \
--with-config-file-path=/sangfor/server/php/etc \
--enable-fpm \
--enable-mbstring \
--with-zip \
--enable-ftp \
--enable-soap \
--enable-pcntl \
--enable-sockets \
--with-zlib \
--with-mhash \
--with-gettext \
--enable-gd \
--with-mysqli \
--with-pdo-mysql \
--with-curl \
--with-openssl \
--enable-intl
make
make install
2.4、配置
# php.ini 配置文件
cp php.ini-production /sangfor/server/php/etc/php.ini
# php-fpm.conf 配置文件
cp /sangfor/server/php/etc/php-fpm.conf.default /sangfor/server/php/etc/php-fpm.conf
# www.conf 配置文件
cp /sangfor/server/php/etc/php-fpm.d/www.conf.default /sangfor/server/php/etc/php-fpm.d/www.conf
2.5、php-fpm管理
配置php-fpm.service脚本
# php-fpm.service
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/
php-fpm.service管理脚本
# 启动
systemctl start php-fpm
# 重新加载配置文件
systemctl reload php-fpm
3、nginx和php通信
nginx和php通信有两种方式,tcp 端口和 unix domain socket
3.1、tcp 端口方式
3.1.1、tcp socket 解释
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
3.1.2、tcp 端口 实战
listen = 127.0.0.1:9000
;listen = /dev/shm/php-cgi.sock
3.1.3、nginx对应的配置
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;
}
3.2、unix domain socket 方式
3.2.1、unix domain socket 解释
; '/path/to/unix/socket' - to listen on a unix socket.
3.2.2、unix domaini socket 实战
修改www.conf
;listen = 127.0.0.1:9000
listen = /dev/shm/php-cgi.sock
修改nginx.conf
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
3.2.3、设置用户和权限
在 /sangfor/server/php/etc/php-fpm.d/www.conf 文件中去掉下面三行的注释
listen.owner = nobody
listen.group = nobody
listen.mode = 0660