Nginx安装、默认虚拟主机、用户认证、nginx中PHP解析
准备工作安装包
# cd /usr/local/src/
下载安装包:
# wget http://nginx.org/download/nginx-1.12.1.tar.gz
解压:
# tar zxvf nginx-1.12.1.tar.gz
安装
环境配置
# cd nginx-1.12.1/
# ./configure --prefix=/usr/local/nginx
#如果需要支持某模块,可以在此添加,如HTTPS、SSL等
编译&安装
# make && make install
# echo $?
0
# cd /usr/local/nginx/
# ls
confhtmllogssbin
配置
添加&启动服务
创建启动脚本:
# vim /etc/init.d/nginx
把下面整段复制进去:
#!/bin/bash
#chkconfig: - 30 21
#description: http service.
#Source Function Library
. /etc/init.d/functions
#Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
检查脚本语法:
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
更改权限:
# chmod 755 /etc/init.d/nginx
添加到系统服务:
# chkconfig --add nginx
# chkconfig nginx on
更改配置文件
# cd /usr/local/nginx/conf/
注释掉Nginx自带脚本,创建自己的脚本:
# mv nginx.conf nginx.conf.bak
# vim nginx.conf
user nobody nobody;
#定义启动Nginx的用户
worker_processes 2;
#定义子进程数目
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
#指定Nginx最多可打开的文件数目
events
{
use epoll;
worker_connections 6000;
#进程最大连接数
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
#虚拟主机
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ .php$
#配置PHP解析
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
检测语法:
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动Nginx服务:
# /etc/init.d/nginx start
Starting nginx (via systemctl): [确定]
至此,Nginx基础配置完成!
检测
# curl localhost
Welcome to nginx!
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.com.
Thank you for using nginx.
使用浏览器检测:
先将虚拟主机IP添加到本地hosts,然后访问:
检测PHP解析
# vim /usr/local/nginx/html/1.php
# curl localhost/1.php
welcom to akai-nginx text.
常见的502问题解决
对于LNMP来说,最常见的就是502问题,LNMP环境搭建完成后,一访问网站直接提示“502 Bad Gateway”。主要原因大致分为两种:
(1)配置错误
在Nginx配置中有这么一段:
location ~ .php$
#配置PHP解析
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
如果把fastcgi_pass(这是用来通信的)后面指定的路径配置错了,那么就会出现502错误,因为Nginx找不到php-fpm了,fastcgi _pass后面可以跟socket也可以跟IP:port,默认监听地址为:127.0.0.1:9000。
注意: 这里用两种形式都可以,但是两个配置文件(Nginx和php-fpm)中的形式一定要统一,不然绝对502;如果用套接字形式的话,socket文件的路径一定要对,不然也还是502。
(2)资源耗尽
LNMP架构处理PHP时,是Nginx直接调取后端的php-fpm服务,如果Nginx的请求量偏高,而我们又没给php-fpm配置足够的子进程数,那么总有php-fpm资源耗尽的时候,一旦耗尽Nginx找不到php-fpm,此时也会导致502错误出现。解决办法就是调整php-fpm.conf中的pm.max_children数值,使其增加。但也不能无限制增加,因为服务器的资源有限。4G内存机器如果只跑php-fpm和Nginx,不跑MySQL服务,pm.max _children可以设置为150,尽量不要超过该数值,8G内存设置为300,以此类推。
(3)listen.mode
在php-fpm配置文件中有参数listen.mode,该参数时指定php-fpm所监听的socket文件listen = /tmp/php-fcgi.sock的权限,如果在此不指定权限,默认权限为440(只允许root用户及root组读取),之后在Nginx中监听该文件时就会提示502错误,解决办法就是给予socket文件读写权限666。
如果遇到其它的较为少见的错误,我们可以修改nginx的错误日志(/usr/local/nginx/logs/nginx_error.log)的级别,在配置文件/usr/local/nginx/conf/nginx.conf中将crit改为debug,使其记录最多的日志内容,这样方便我们排查错误,但是配置更改完成后要记得将级别改回crit,避免日志文件占用太多磁盘空间。
页:
[1]