Nginx与PHP之间的关系:Nginx与PHP相对而言都是独立的,只不过在应
用过程中,解析动态脚本时,Nginx会自动源代码发送给PHP-FPM程序。
Nginx在处理静态页面时,效率较高。
安装PHP-FPM
安装依赖
# yum -y install libxml2-devel libjpeg-devel libpng-devel
freetype-devel curl-devel openssl-devel
php.sh配置安装
# vim php.sh
tar php-7.4.0.tar.gz
cd php-7.4.0
./configure --prefix=/usr/local/php \
--with-config-filepath=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysqli=mysqlnd \
--with-pdomysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--with-libzip \
--enable-soap \
--without-pear \
--with-gettext \
--disable-fileinfo \
--enable-maintainer-zts \
make && make install
–prefix:安装目录
–with-config-file-path:配置文件目录
–enable-fpm:开启php-fpm功能
–with-fpm-user:代表以哪个用户运行php-fpm
–with-fpm-group:代表以哪个用户组运行php-fpm
–with-mysqli:php-mysqli扩展
–with-pdo-mysql:php-pdo-mysql扩展
–with-*:项目所需的额外的扩展库(需要运维工程师安装,但是由程序猿提
供文档)
php7.2:从php5.6开始,php链接mysql必须使用mysqli或pdo扩展
执行脚本
# source php.sh
PHP-FPM配置文件
把php.in移动到/usr/local/php/etc目录
# cp /root/php-7.4.0/php.ini-development /usr/local/php/etc/php.ini
加载php-fpm.conf主配置文件
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
加载php-fpm子配置文件www.conf文件
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
PHP-FPM启动 => php.ini => php-fpm.conf => www.conf文件
添加php-fpm启动项
把phpp-fpm添加到/etc/init.d目录下,开机可自启
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
给php-fpm执行权限
# chmod +x /etc/init.d/php-fpm
启动php
# service php-fpm start
添加到环境变量
创建php脚本
# cd /usr/local/nginx/html
# vim demo.php
<?php
echo 'hello php!';
?>
让nginx识别php
# cd /usr/local/nginx
去除#号与空白行
# grep -Ev '#|^$' conf/nginx.conf > nginx.conf
把nginx.conf备份
# mv conf/nginx.conf conf/nginx.conf.bak
移动nginx.conf到conf目录
# mv nginx.conf conf/
# vim /usr/local/nginx/conf/nginx.conf
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 server {
# 监听端口
11 listen 80;
#域名,localhost表示本机
12 server_name localhost;
#项目目录
root html;
#任意请求
13 location / {
#项目目录
14 root html;
#默认首页(如果在html目录发现默认首页,直接跳转)
15 index index.html index.htm;
16 }
#添加php文件支持
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;
}
#错误时,跳转到/50.html页面
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html {
19 root html;
20 }
21 }
22 }
一个nginx.conf对应一个http,代表http请求,所以一个页面只有一个http
一个http可以包含多个server区块,每个区块就是一个项目(网站)=> 虚拟
主机
重载nginx
# systemctl reload nginx