NGINX配置PHP网站

NGINX配置PHP网站

NGINX配置PHP网站

  • NGINX配置PHP网站
    • 源码安装NGINX
    • 安装PHP
    • 修改PHP参数
    • 重启PHP
    • 修改nginx配置文件
    • 重启NGINX
    • 测试
    • 解决报错问题

NGINX配置PHP网站

源码安装NGINX

脚本一键安装:
安装路径:/opt/nginx
源码路径:/root/

 #!/bin/bash
useradd -s /sbin/nologin nginx               #创建一个不能登陆系统的用户nginx
#Installation dependence
yum groupinstall -y "Development tools"
yum install -y gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel
yum install -y pcre-devel pcre zlib zlib-devel openssl openssl-devel wget gcc gcc-c++ unzip
yum -y install git
#Download source
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar -xvf nginx-1.20.1.tar.gz -C /opt
#Compile and install
cd /opt/nginx-1.20.1/
sed -i '49s/nginx/Microsoft-IIS/' src/http/ngx_http_header_filter_module.c
sed -i '50s/: /: Microsoft-IIS/' src/http/ngx_http_header_filter_module.c
sed -i '51s/: /: Microsoft-IIS/' src/http/ngx_http_header_filter_module.c
./configure \
--prefix=/opt/nginx/ \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module 
make && make install
ln -s /opt/nginx/sbin/nginx  /usr/bin/nginx
nginx
ps -aux|grep nginx


安装PHP

wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum install -y ./remi-release-7.rpm 
yum install -y php72-php-devel php72-php-fpm php72-php-mbstring php72-php-memcache php72-php-redis php72-php-mysqli php72-php-mysqlnd php72-php-pdo php72-php-bcmath php72-php-dom php72-php-gd php72-php-gmp php72-php-igbinary php72-php-imagick php72-php-mcrypt php72-php-pdo_mysql php72-php-posix php72-php-simplexml php72-php-opcache php72-php-xsl php72-php-xmlwriter php72-php-xmlreader php72-php-xml php72-php-swoole php72-php-zip php72-php-fileinfo curl curl-devel net-snmp net-snmp-devel perl-DBI ntpdate libxml2-devel libevent-devel yum-utils pcre pcre-devel openssl  openssl-devel zlib zlib-devel gcc gcc-c++


修改PHP参数

路径:/etc/opt/remi/php72/php-fpm.d/www.conf

vim /etc/opt/remi/php72/php-fpm.d/www.conf
修改参数:
max_execution_time=300 #修改为300
post_max_size=16M
upload_max_filesize=8M
max_input_time=300

date.timezone=“Asia/Shanghai” #增加
always_populate_raw_post_data=-1 #增加



重启PHP

systemctl restart php72-php-fpm



修改nginx配置文件

方法一:在默认的nginx.conf修改
方法二:添加conf.d 目录增加配置文件添加PHP配置

推荐方法2
方法一:
cp -r /opt/nginx/conf/nginx.conf /opt/nginx/conf/nginx.conf.bak
vim /opt/nginx/conf/nginx.conf +65

往下拉到 location ~ .php$位置

在这里插入图片描述

  1. 按 L或者右方向键到#号
  2. Crtl+v 可视化选项
  3. 按 j 键往下拉或按方向箭头往下键选中要取消注释的配置
  4. 再按 s 键就会把# 号去掉了
  5. 修改配置
    把 fastcgi_params 更改为fastcgi.conf ;注释掉这行 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    在这里插入图片描述

方法2:
默认文件配置文件不用修改 /opt/nginx/conf/nginx.conf
或者重新写入默认配置文件。这个是经常使用的nginx配置文件可以粘贴覆盖默认文件 /opt/nginx/conf/nginx.conf

worker_processes  auto;
error_log		logs/error.log error;
pid		logs/nginx.pid;
events {	
	worker_connections  65535;
	multi_accept on;
}
http {
	include    mime.types;
	default_type  application/octet-stream;
	log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
	access_log	 logs/access.log main;
	keepalive_timeout  60;
	add_header Access-Control-Allow-Origin '*';
        add_header Access-Control-Max-Age '3628800';
	add_header Access-Control-Allow-Credentials 'true';
	add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
	add_header Access-Control-Allow-Methods 'GET,POST,PUT,OPTIONS';
	underscores_in_headers on;	
	client_header_buffer_size	32k;
	client_body_buffer_size	20m;
	client_max_body_size	120M;
	client_header_timeout	1m;
	client_body_timeout		1m;
	proxy_connect_timeout    600;
	proxy_read_timeout       600;
	proxy_send_timeout       600;
	large_client_header_buffers	4	32k;
	fastcgi_buffers		4	128k;
	fastcgi_buffer_size		128k;
	fastcgi_busy_buffers_size	256k;
	server_tokens off;
	tcp_nopush on;
	tcp_nodelay on;
	sendfile        on;
	gzip  on; #开启gzip
	#gzip_static on;
	gzip_vary on;
	gzip_min_length 1k;
	gzip_buffers 8 32k;
	gzip_http_version 1.1;
	gzip_comp_level 6; 
	gzip_proxied any;
	gzip_types application/javascript application/json text/css image/png;
	real_ip_header		X-Real-IP;
	proxy_set_header        Host            $host:$server_port;
	proxy_set_header        X-Real-IP       $remote_addr;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;   
	include      /opt/nginx/conf.d/*.conf;
}

添加子目录
mkdir /opt/nginx/conf.d
touch /opt/nginx/conf.d/test_php.conf

添加php项目
vim /opt/nginx/conf.d/test_php.conf

server {
        listen      80 ;
        server_name  localhost;
        location / {
            root   /home/test;
            index  index.php index.htm index.html;
           }
        location ~ \.php$ {
            root          /home/test;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
       }


重启NGINX

nginx -t
nginx -s reload


测试

vim /home/test/test.php

<?php
  $i="This is a test Page";
  echo $i;
?>


解决报错问题

部署项目后:
如果出现 No input file specified.
看项目下面是不是多了 .user.ini文件
ls -la 查看项目下面



                       

点击阅读全文

上一篇 2023年 6月 7日 am10:51
下一篇 2023年 6月 7日 am10:52