文章目录
1. nginx简介
nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。
nginx由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler使用。
第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
nginx的特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
2. nginx的特性与优点
2.1 nginx的特性
nginx是一个很牛的高性能Web和反向代理服务器,它具有很多非常优越的特性:
在高连接并发的情况下,nginx是Apache服务器不错的替代品,能够支持高达50000个并发连接数的响应
使用epoll and kqueue作为开发模型
nginx作为负载均衡服务器:nginx既可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好很多
2.2 nginx的优点
- 高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2-3万并发连接数
- 内存消耗少:在3万并发连接下,开启的10个nginx进程才消耗150M内存(15M*10=150M)
- 配置文件非常简单:风格跟程序一样通俗易懂
- 成本低廉:nginx为开源软件,可以免费使用。而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则需要十多万至几十万人民币
- 支持Rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组
- 内置的健康检查功能:如果Nginx Proxy后端的某台Web服务器宕机了,不会影响前端访问
- 节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头
- 稳定性高:用于反向代理,宕机的概率微乎其微
- 模块化设计:模块可以动态编译
- 外围支持好:文档全,二次开发和模块较多
- 支持热部署:可以不停机重载配置文件
- 支持事件驱动、AIO(AsyncIO,异步IO)、mmap(Memory Map,内存映射)等性能优化
3. nginx的安装与配置
***java
3.1 nginx的安装
nginx下载地址: 推荐下载稳定版
//创建系统用户
[root@cxr ~]# useradd -r -M -s /sbin/nologin nginx
//安装依赖环境
[root@cxr ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
[root@cxr ~]# yum -y groups mark install 'Development Tools' //安装过程以省略
//创建日志存放目录
[root@cxr ~]# mkdir -p /var/log/nginx
[root@cxr ~]# chown -R nginx.nginx /var/log/nginx
//下载nginx
[root@cxr ~]# cd /usr/src/
[root@cxr src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
//编译安装
[root@cxr src]# ls
debug kernels nginx-1.22.0.tar.gz
[root@cxr src]# tar -xf nginx-1.22.0.tar.gz
[root@cxr src]# cd nginx-1.22.0/
[root@cxr 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@cxr nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install //编译过程省略
//配置环境变量
[root@cxr nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@cxr nginx-1.22.0]# source /etc/profile.d/nginx.sh
//服务控制方式,使用nginx命令
-t //检查配置文件语法
-v //输出nginx的版本
-c //指定配置文件的路径
-s //发送服务控制信号,可选值有{stop|quit|reopen|reload}
//启动nginx
[root@cxr nginx-1.22.0]# nginx
[root@cxr nginx-1.22.0]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
4 nginx的配置文件详解
主配置文件:/usr/local/nginx/conf/nginx.conf
- 默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
- 可以在启动nginx时通过-c选项来指定要读取的配置文件
nginx常见的配置文件及其作用
配置文件 | 作用 |
---|---|
nginx.conf | nginx的基本配置文件 |
mime.types | MIME类型关联的扩展文件 |
fastcgi.conf | 与fastcgi相关的配置 |
proxy.conf | 与proxy相关的配置 |
sites.conf | 配置nginx提供的网站,包括虚拟主机 |
4.1 nginx.conf配置详解
nginx.conf的内容分为以下几段:
- main配置段:全局配置段。其中main配置段中可能包含event配置段
- event {}:定义event模型工作特性
- http {}:定义http协议相关的配置
配置指令:要以分号结尾,语法格式如下:
derective value1 [value2 ...];
支持使用变量:
- 内置变量:模块会提供内建变量定义
- 自定义变量:set var_name value
4.2 用于调试、定位问题的配置参数
daemon {on|off}; //是否以守护进程方式运行nginx,调试时应设置为off
master_process {on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别; //配置错误日志
error_log里的位置和级别能有以下可选项:
位置 | 级别 |
---|---|
file stderr memory:size syslog:server=address[,parameter=value] | debug:若要使用debug级别,需要在编译nginx时使用–with-debug选项 info notice warn error crit alert emerg |
4.3 正常运行必备的配置参数
user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组
pid /path/to/pid_file; //指定nginx守护进程的pid文件
worker_rlimit_nofile number; //设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_core size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
4.4 优化性能的配置参数
worker_processes n; //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...; //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用8位二进制表示cpu核心,如:
0000 0001 //第一颗cpu核心
0000 0010 //第二颗cpu核心
0000 0100 //第三颗cpu核心
0000 1000 //第四颗cpu核心
0001 0000 //第五颗cpu核心
0010 0000 //第六颗cpu核心
0100 0000 //第七颗cpu核心
1000 0000 //第八颗cpu核心
timer_resolution interval; //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
worker_priority number; //指明worker进程的nice值
4.5 事件相关的配置:event{}段中的配置参数
accept_mutex {off|on}; //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求
lock_file file; //accept_mutex用到的互斥锁锁文件路径
use [epoll | rtsig | select | poll]; //指明使用的事件模型,建议让nginx自行选择
worker_connections #; //每个进程能够接受的最大连接数
4.6 网络连接相关的配置参数
keepalive_timeout number; //长连接的超时时长,默认为65s
keepalive_requests number; //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none]; //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off; //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number; //读取http请求报文首部的超时时长
client_body_timeout number; //读取http请求报文body部分的超时时长
send_timeout number; //发送响应报文的超时时长
4.7 fastcgi的相关配置参数
LNMP:php要启用fpm模型
配置示例如下:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000; //定义反向代理
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
4.8 常需要进行调整的参数
- worker_processes
- worker_connections
- worker_cpu_affinity
- worker_priority
5编译安装lnmp
下载最新版的mysql
5.1编译安装MySQL
//将安装包放到/usr/src目录下
[root@localhost nginx]# cd /usr/src/
[root@localhost src]# ls
debug mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz nginx-1.22.0.tar.gz
//创建系统用户
[root@localhost src]# useradd -r -M -s /sbin/nologin mysql
[root@localhost src]# id mysql
uid=974(mysql) gid=972(mysql) 组=972(mysql)
//解压安装包
[root@localhost src]# tar -xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
//重命名
root@localhost src]# cd /usr/local/
[root@localhost local]# ls
bin games lib libexec nginx share
etc include lib64 mysql-8.0.28-linux-glibc2.12-x86_64 sbin src
[root@localhost local]# mv mysql-8.0.28-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ls
bin etc games include lib lib64 libexec mysql nginx sbin share src
//修改属组
[root@localhost local]# chown -R mysql.mysql mysql
[root@localhost local]# ll
总用量 0
drwxr-xr-x. 2 root root 6 8月 12 2018 bin
drwxr-xr-x. 2 root root 6 8月 12 2018 etc
drwxr-xr-x. 2 root root 6 8月 12 2018 games
drwxr-xr-x. 2 root root 6 8月 12 2018 include
drwxr-xr-x. 2 root root 6 8月 12 2018 lib
drwxr-xr-x. 2 root root 6 8月 12 2018 lib64
drwxr-xr-x. 2 root root 6 8月 12 2018 libexec
drwxr-xr-x. 9 mysql mysql 129 9月 3 04:04 mysql
drwxr-xr-x. 11 root root 151 9月 3 03:21 nginx
drwxr-xr-x. 2 root root 6 8月 12 2018 sbin
drwxr-xr-x. 5 root root 49 4月 11 13:04 share
drwxr-xr-x. 2 root root 6 8月 12 2018 src
/添加环境变量
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh //重新读取下
[root@localhost local]#
//将MySQL目录下面的include目录软连接到/usr/include/mysql
[root@localhost local]# ln -s /usr/local/mysql/include /usr/include/mysql
//告知lib库的路径
[root@localhost local]# vim /etc/ld.so.conf.d/mysql.conf
[root@localhost local]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@localhost local]# ldconfig //重新读取配置文件
//配置man文档
[root@localhost local]# vim /etc/man_db.conf
#---------------------------------------------------------
# every automatically generated MANPATH includes these fields
#
#MANDATORY_MANPATH /usr/src/pvm3/man
#
MANDATORY_MANPATH /usr/man
MANDATORY_MANPATH /usr/share/man
MANDATORY_MANPATH /usr/local/share/man
MANDATORY_MANPATH /usr/local/mysql/man //在文档中添加这一行
#---------------------------------------------------------
//创建数据存放目录
[root@localhost local]# mkdir /opt/data
[root@localhost local]# chown -R mysql.mysql /opt/data
[root@localhost local]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 9月 3 04:12 data
//初始化
[root@localhost local]# mysqld --initialize --user mysql --datadir /opt/data
2022-09-03T08:14:19.581866Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.28) initializing of server in progress as process 206752
2022-09-03T08:14:19.594074Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-09-03T08:14:20.183698Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-09-03T08:14:21.627841Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: U.vw8*yQffvV //这个密码是随机生成的临时密码用来登录到MySQL修改密码。此处密码是:U.vw8*yQffvV 每个人都是不一样的
//生成配置文件
[root@localhost local]# vim /etc/my.cnf
[root@localhost local]# 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@localhost local]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@localhost support-files]# cp mysql.server mysqld
[root@localhost support-files]# chown -R mysql.mysql mysqld
[root@localhost support-files]# vim mysqld
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.
basedir=/usr/local/mysql //修改这一行
datadir=/opt/data //修改这一行
# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
//启动MySQL
[root@localhost support-files]# /usr/local/mysql/support-files/mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
[root@localhost support-files]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
//下载库
[root@localhost ~]# yum -y install ncurses-compat-libs //安装过程已省略
//修改密码
[root@localhost support-files]# mysql -uroot -p'U.vw8*yQffvV'
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 8
Server version: 8.0.28
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> SET PASSWORD = 'hello 123!'; //安装需求来改密码
Query OK, 0 rows affected (0.00 sec)
mysql> quit //退出
Bye
//测试密码
[root@localhost support-files]# mysql -uroot -p'hello 123!' //能够登录就说明修改成功
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 9
Server version: 8.0.28 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
//设置MySQL开机自启
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/mysqld.service //复制一份sshd.service文件
[root@localhost ~]# cd /usr/lib/systemd/system
[root@localhost system]# vim mysqld.service
[root@localhost system]# cat mysqld.service //配置文件
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
[root@localhost system]#
//关闭selinux
[root@localhost system]# vim /etc/selinux/config
SELINUX=disabled //修改成禁用模式
[root@localhost system]# vim /etc/selinux/config
[root@localhost system]# reboot //重启一下
连接断开
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl status mysqld //查看MySQL状态
● mysqld.service - mysql server daemon
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: >
Active: active (running) since Sat 2022-09-03 04:32:46 EDT; 1min 11s ago
Process: 244508 ExecStart=/usr/local/mysql/support-files/mysqld start (code=exited>
Main PID: 244521 (mysqld_safe)
Tasks: 38 (limit: 23648)
Memory: 359.7M
CGroup: /system.slice/mysqld.service
├─244521 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --p>
└─244711 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir>
9月 03 04:32:45 localhost systemd[1]: Starting mysql server daemon...
9月 03 04:32:46 localhost mysqld[244508]: Starting MySQL. SUCCESS!
9月 03 04:32:46 localhost systemd[1]: Started mysql server daemon
6编译安装php
右键复制连接下载到虚拟机中的/usr/src/目录下
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget https://www.php.net/distributions/php-8.1.10.tar.gz
--2022-09-03 04:38:18-- https://www.php.net/distributions/php-8.1.10.tar.gz
正在解析主机 www.php.net (www.php.net)... 185.85.0.29, 2a02:cb40:200::1ad
正在连接 www.php.net (www.php.net)|185.85.0.29|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:19745520 (19M) [application/octet-stream]
正在保存至: “php-8.1.10.tar.gz”
php-8.1.10.tar.gz 100%[======================>] 18.83M 32.5KB/s 用时 7m 49s
2022-09-03 04:46:09 (41.1 KB/s) - 已保存 “php-8.1.10.tar.gz” [19745520/19745520])
[root@localhost src]# ls
debug mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz nginx-1.22.0.tar.gz
kernels nginx-1.22.0 php-8.1.10.tar.gz
[root@localhost src]# wget https://github.com/kkos/oniguruma/archive/refs/tags/v6.9.7.1.tar.gz -O oniguruma-6.9.7.1.tar.gz //下载一个依赖包
--2022-09-03 05:05:03-- https://github.com/kkos/oniguruma/archive/refs/tags/v6.9.7.1.tar.gz
正在解析主机 github.com (github.com)... 20.205.243.166
正在连接 github.com (github.com)|20.205.243.166|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://codeload.github.com/kkos/oniguruma/tar.gz/refs/tags/v6.9.7.1 [跟随至新的 URL]
--2022-09-03 05:05:04-- https://codeload.github.com/kkos/oniguruma/tar.gz/refs/tags/v6.9.7.1
正在解析主机 codeload.github.com (codeload.github.com)... 20.205.243.165
正在连接 codeload.github.com (codeload.github.com)|20.205.243.165|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:未指定 [application/x-gzip]
正在保存至: “oniguruma-6.9.7.1.tar.gz”
oniguruma-6.9.7.1.tar [ <=> ] 616.30K 501KB/s 用时 1.2s
2022-09-03 05:05:06 (501 KB/s) - “oniguruma-6.9.7.1.tar.gz” 已保存 [631096]
//解压编译安装 oniguruma
[root@localhost src]# tar -xf oniguruma-6.9.7.1.tar.gz
[root@localhost src]# tar -xf php-8.1.10.tar.gz
[root@localhost src]# cd oniguruma-6.9.7.1/
[root@localhost oniguruma-6.9.7.1]#
[root@localhost oniguruma-6.9.7.1]# yum install autoconf automake libtool make gcc gcc-c++
[root@localhost oniguruma-6.9.7.1]# ./autogen.sh && ./configure --prefix=/usr
[root@localhost oniguruma-6.9.7.1]# make && make install //过程以省略
//编译安装php
[root@localhost ~]# cd /usr/src/php-8.1.10/
[root@localhost php-8.1.10]# ./configure --prefix=/usr/local/php8 --exec-prefix=/usr/local/php8 --bindir=/usr/local/php8/bin --sbindir=/usr/local/php8/sbin --includedir=/usr/local/php8/include --libdir=/usr/local/php8/lib/php --mandir=/usr/local/php8/php/man --with-config-file-path=/usr/local/php8/etc --with-openssl --enable-mbstring --enable-fpm
+--------------------------------------------------------------------+
| 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@localhost php-8.1.10]# make && make install //构架过程省略
//配置php-fpm
[root@localhost php-8.1.10]# cp php.ini-production /etc/php.ini
[root@localhost php-8.1.10]# cp /usr/local/php8/etc/php-fpm.conf.default /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.1.10]# cp /usr/local/php8/etc/php-fpm.d/www.conf.default /usr/local/php8/etc/php-fpm.d/www.conf
//修改属组
[root@localhost php-8.1.10]# sed -i '/^user = nobody/c user = nginx' /usr/local/php8/etc/php-fpm.d/www.conf
[root@localhost php-8.1.10]# sed -i '/^group = nobody/c group = nginx' /usr/local/php8/etc/php-fpm.d/www.conf
//编辑php-fpm的配置文件(/usr/local/php8/etc/php-fpm.conf):
//配置fpm的相关选项为你所需要的值:
[root@localhost php-8.1.10]# vim /usr/local/php8/etc/php-fpm.conf
[root@localhost php-8.1.10]# tail /usr/local/php8/etc/php-fpm.conf
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
; - the global prefix if it's been set (-p argument)
; - /usr/local/php8 otherwise
include=/usr/local/php8/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
//编写php-fpm脚本
[root@localhost init.d]# cd
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# cat php-fpm
#! /bin/sh
# Comments to support chkconfig on CentOS
# chkconfig: 2345 65 37
#
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="php-fpm daemon"
NAME=php-fpm
DAEMON=/usr/local/php8/sbin/$NAME
CONFIGFILE=/usr/local/php8/etc/php-fpm.conf
PIDFILE=/usr/local/php8/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
d_start() {
$DAEMON -y $CONFIGFILE || echo -n " already running"
}
d_stop() {
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
d_reload() {
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC is success"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC is success"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC is success"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
//添加执行权限
[root@localhost init.d]# chmod +x php-fpm
//开启php服务
[root@localhost init.d]# service php-fpm start
[root@localhost init.d]# service php-fpm start
Starting php-fpm daemon is success.
[root@localhost init.d]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 70 *:33060 *:*
LISTEN 0 128 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
[root@localhost init.d]# ps -ef|grep php
root 724088 1 0 06:42 ? 00:00:00 php-fpm: master process (/usr/local/php8/etc/php-fpm.conf)
nginx 724089 724088 0 06:42 ? 00:00:00 php-fpm: pool www
nginx 724090 724088 0 06:42 ? 00:00:00 php-fpm: pool www
nginx 724091 724088 0 06:42 ? 00:00:00 php-fpm: pool www
nginx 724092 724088 0 06:42 ? 00:00:00 php-fpm: pool www
nginx 724093 724088 0 06:42 ? 00:00:00 php-fpm: pool www
root 725088 198267 0 06:43 pts/2 00:00:00 grep --color=auto php
//下载依赖包
[root@localhost ~]# yum -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 sqlite-devel libzip-devel libtool
[root@localhost ~]# wget https://github.com/kkos/oniguruma/archive/v6.9.4.tar.gz -O oniguruma-6.9.4.tar.gz //下载此包 后续编译php需要
[root@localhost ~]# tar -zxf oniguruma-6.9.4.tar.gz //解压
[root@localhost ~]# cd oniguruma-6.9.4
[root@localhost oniguruma-6.9.4]# dnf -y install automake
//编译安装
[root@localhost oniguruma-6.9.4]# ./autogen.sh && ./configure --prefix=/usr
Generating autotools files.
libtoolize: putting auxiliary files in '.'.
libtoolize: copying file './ltmain.sh'
libtoolize: putting macros in AC_CONFIG_MACRO_DIRS, 'm4'.
[root@localhost oniguruma-6.9.4]# make && make install ///省略过程
//下载PHP软件包
[root@localhost ~]# wget https://www.php.net/distributions/php-8.1.10.tar.gz
[root@localhost ~]# tar xf php-8.1.10.tar.gz
[root@localhost ~]# cd php-8.1.10/
./configure --prefix=/usr/local/php7 \
--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@localhost php-8.1.10]# make
[root@localhost php-8.1.10]# make install //make编译
[root@localhost php-8.1.10]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh //使php命令可以使用
[root@localhost php-8.1.10]# source /etc/profile.d/php7.sh //读取 让其生效
[root@localhost ~]# which php
/usr/local/php7/bin/php
[root@localhost ~]# cd php-8.1.10/
[root@localhost php-8.1.10]# cp php.ini-production /etc/php.ini //将生产环境文件 复制到etc下
[root@localhost php-8.1.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost fpm]# chmod +x /etc/rc.d/init.d/php-fpm //此文件需要执行权限所以复制过去要看是否有执行(x)权限
[root@localhost php-8.1.10]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf //将php-fpm.conf.default 复制一份名为php-fpm.conf
[root@localhost php-8.1.10]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf //将www.conf.default 复制一份名为www.conf
[root@localhost ~]# service php-fpm start
Starting php-fpm done
[root@localhost ~]# ss -antl //php-fpm 的默认端口为9000
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
[root@localhost ~]# ps -ef |grep php //查看php的进程
root 681716 1 0 19:57 ? 00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody 681717 681716 0 19:57 ? 00:00:00 php-fpm: pool www
nobody 681718 681716 0 19:57 ? 00:00:00 php-fpm: pool www
root 689388 362392 0 19:59 pts/1 00:00:00 grep --color=auto php
[root@localhost nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@localhost nginx]# cd /usr/local/nginx/html/
[root@localhost html]# ls
index.html
[root@localhost html]# chown -R nginx.nginx /usr/local/nginx/ //设置apache下的文件目录属组属主都为nginx
[root@localhost html]# ll
总用量 4
-rw-r--r-- 1 nginx nginx 12 9月 3 20:02 index.html
[root@localhost html]# vim index.php //创建一个index.php的测试文件
[root@localhost nginx]# cat html/test.com/index.php
<?php
phpinfo();
?>
[root@localhost test.com]# cd
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf koi-win scgi_params
fastcgi.conf.default mime.types scgi_params.default
fastcgi_params mime.types.default uwsgi_params
fastcgi_params.default nginx.conf uwsgi_params.default
koi-utf nginx.conf.default win-utf
[root@localhost conf]# vim 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;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
[root@localhost conf]# nginx -s stop
[root@localhost conf]# nginx
[root@localhost conf]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631