Nginx安装与配置(包括解决403错误,站点配置,

[复制链接]
查看11 | 回复1 | 2021-1-27 06:28:52 | 显示全部楼层 |阅读模式
1.yum安装nginx
[html]viewplaincopy
nginx-v#安装前检查
yuminstallnginx-y#yum安装nginx
2.启动nginx并设置开机自启动
[html]viewplaincopy
systemctlstartnginx.service#启动nginx
systemctlstopnginx.service#关闭nginx
systemctlenablenginx.service#设置nginx开机自启
systemctlstatusnginx.service#查看nginx运行状态
3.安装完成后即可输入ip进行访问,若出现403,检查是否关闭防火墙
[html]viewplaincopy
systemctlstopfirewalld#关闭防火墙
4.修改nginx配置文件使其支持php解析,并配置相关参数
[html]viewplaincopy
#Formoreinformationonconfiguration,see:
#*OfficialEnglishDocumentation:http://nginx.org/en/docs/
#*OfficialRussianDocumentation:http://nginx.org/ru/docs/
userroot;#运行用户
worker_processesauto;##启动进程,通常设置成和cpu的数量相等
error_log/var/log/nginx/error.log;#错误日志位置
pid/run/nginx.pid;#全局错误日志及PID文件
#Loaddynamicmodules.See/usr/share/nginx/README.dynamic.
include/usr/share/nginx/modules/*.conf;
worker_rlimit_nofile51200;#单个后台workerprocess进程的最大并发链接数
events{
useepoll;#仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections51200;
multi_accepton;
}
http{
log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
access_log/var/log/nginx/access.logmain;
charsetutf-8;
sendfileon;
tcp_nopushon;
tcp_nodelayon;
keepalive_timeout65;
types_hash_max_size2048;
include/etc/nginx/mime.types;
default_typeapplication/octet-stream;
#Loadmodularconfigurationfilesfromthe/etc/nginx/conf.ddirectory.
#Seehttp://nginx.org/en/docs/ngx_core_module.html#include
#formoreinformation.
include/etc/nginx/conf.d/*.conf;
fastcgi_connect_timeout300;
fastcgi_send_timeout300;
fastcgi_read_timeout300;
fastcgi_send_timeout300;
fastcgi_read_timeout300;
fastcgi_buffer_size64k;
fastcgi_buffers464k;
fastcgi_busy_buffers_size128k;
fastcgi_temp_file_write_size256k;
server{
listen80default_server;
listen[::]:80default_server;
server_name_;
indexindex.htmlindex.htmindex.php;
#root/usr/share/nginx/html;#默认网站根目录
root/home/www;#自定义网站根目录需要给足读写权限
#Loadconfigurationfilesforthedefaultserverblock.
include/etc/nginx/default.d/*.conf;
location/{
indexindex.htmlindex.htmindex.php;
autoindexon;
}
location~\.php${
#root/usr/share/nginx/html;#指定php的根目录
root/home/www;#自定义网站根目录需要给足读写权限
fastcgi_pass127.0.0.1:9000;#php-fpm的默认端口是9000
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
includefastcgi_params;
}
error_page404/404.html;
location=/40x.html{
}
error_page500502503504/50x.html;
location=/50x.html{
}
}
#SettingsforaTLSenabledserver.
#
#server{
#listen443sslhttp2default_server;
#listen[::]:443sslhttp2default_server;
#listen443sslhttp2default_server;
#listen[::]:443sslhttp2default_server;
#server_name_;
#root/usr/share/nginx/html;
#
#ssl_certificate"/etc/pki/nginx/server.crt";
#ssl_certificate_key"/etc/pki/nginx/private/server.key";
#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout10m;
#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;
#
##Loadconfigurationfilesforthedefaultserverblock.
#include/etc/nginx/default.d/*.conf;
#
#location/{
#}
#
#error_page404/404.html;
#location=/40x.html{
#}
#
#error_page500502503504/50x.html;
#location=/50x.html{
#}
#}
includevhosts/*.conf;
}
5.启动php-fpm(有两种启动方式,1.手动切换到目录下启动,2.设置开机自启动)
a.手动启动:切换到php-fpm目录执行./php-fpm
b.开机自启:systemctlenablephp-fpm.service
6.关闭selinux(selinux开启可能会导致访问时出现403)
a.临时关闭
[html]viewplaincopy
setenforce0
b.永久关闭
[html]viewplaincopy
vi/etc/selinux/config(find/-nameselinux)
将SELINUX=enforcing改为SELINUX=disabled
设置后需要重启才能生效
7.配置虚拟站点
a.find/-namenginx.conf#找到nginx.conf目录路径
b.在nginx目录下新建文件夹vhosts
c.切换到vhosts目录,新建web1.conf,web1.conf配置如下
[html]viewplaincopy
server{
listen80;
server_nameweb1.com;
root/home/www/web/;
indexindex.phpindex.html;

location/{
indexindex.phpindex.html;
}
location~.*\.(php|php5)?${
fastcgi_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
fastcgi_paramAPPLICATION_ENVdevelopment;
includefastcgi_params;
}
}
d.并在nginx.conf的server最后添加includevhosts/*.conf;
e.配置解析,find/-namehosts添加域名及ip,还需在本机hosts中也添加ip地址
8.如果nginx安装或运行过程中出现403错误首先检查是否具有读写权限,如果出现其他错误,可使用cat命令查看错误日志进行问题排查
9.部署tp框架在Nginx中(或者出现Noinputfile)
使Nginx支持tp框架支持:common中加入以下配置
[html]viewplaincopy
returnarray(
'URL_MODEL'=>'2',//URL模式或者'URL_MODEL'=>3(URL兼容模式)
'URL_PATHINFO_FETCH'=>':get_path_info',//加入此项配置
);
[html]viewplaincopy
Nginx.conf:
location/{//…..省略部分代码
if(!-e$request_filename){
rewrite^(.*)$/index.php?s=$1last;
break;
}
}
如出现_STORAGE_WRITE_ERROR_,请检查Runtime目录是否具有读写权限

分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:28:52 | 显示全部楼层
建议重启重新安装,善用Google搜索引擎
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行