安装 Nginx
代码语言:javascript复制yum install nginx
安装 PHP
代码语言:javascript复制# 启用 remi 源
yum install epel-release yum-utils -y
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
# 安装 php
yum-config-manager --enable remi-php72 # 安装 php7.2
yum-config-manager --enable remi-php73 # 安装 php7.3
yum-config-manager --enable remi-php74 # 安装 php7.4
yum install php php-cli php-fpm php-mysql php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis php-fileinfo
安装 mysql5.7
代码语言:javascript复制rpm -ivh https://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm
yum install mysql-server
配置 Nginx
代码语言:javascript复制vim /etc/nginx/nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php;
}
location ~ .php$ {
root /usr/share/nginx/html;
fastcgi_split_path_info ^(. .php)(/. )$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
启动服务
代码语言:javascript复制systemctl start nginx
systemctl start mysqld
systemctl php74-php-fpm
测试
代码语言:javascript复制vim /usr/share/nginx/html/index.php
<?php
phpinfo();
?>
PHP 优化配置
代码语言:javascript复制vim /etc/php-fpm.d/www.conf
# dynamic:表示php-fpm进程数是动态的,最开始是pm.start_servers指定的数量,如果请求较多,则会自动增加,保证空闲的进程不小于pm.min_spare_servers,如果进程数较多,也会进行清理,保证多余的进程数不会多于pm.max_spare_servers
# static:表示php-fpm进程数是静态的,进程数自始至终都是pm.max_children指定的数量,不再增加或减少
pm = dynamic|static|ondemand
# 慢日志配置
slowlog = /usr/local/var/log/php-fpm.log.slow
request_slowlog_timeout = 15s
配置上传文件大小限制
代码语言:javascript复制vim /etc/php.ini
# POST数据允许的最大大小,值要经upload_max_filesize稍大一点
post_max_size = 100M
# 上传文件的最大大小
upload_max_filesize = 10M
# 超时时间,如果时间到了,文件还没上传完成,也会提示失败
max_execution_time = 90
# 请求的最大传输时间,过短会导致文件还没上传完成传输就会停止了
max_input_time = 90
php日志配置
代码语言:javascript复制vim /etc/php.ini
error_reporting E_ALL & ~E_NOTICE
vim /etc/php-fpm.d/www.conf
catch_workers_output = yes
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
如果错误日志没有写入此文件,查看apache用户对/var/log/php-fpm/www-error.log是否有写入权限
www.conf的优先级高于php.ini
配置nginx用sock连接php-fpm
代码语言:javascript复制vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
listen = /dev/shm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
vim /etc/nginx/nginx.conf
fastcgi_pass unix:/dev/shm/php-fpm.sock;