Centos – nginx的搭建2

需要先搭建好系统环境

gcc编译环境

PHP

点击跳转

下载nginx安装压缩包

wget http://nginx.org/download/nginx-1.9.9.tar.gz

解压并切换到nginx目录

cd nginx-1.9.9/

tar -zxvf nginx-1.9.9.tar.gz

执行配置并编译安装

./configure

make install

默认安装目录为 /usr/local/nginx
默认配置文件为 /usr/local/nginx/conf/nginx.conf

添加nginx服务

vim /usr/lib/systemd/system/nginx.service

配置文件如下:

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存并重新载入服务

systemctl daemon-reload

控制命令:

systemctl start nginx #开启服务

systemctl stop nginx #关闭服务

systemctl status nginx #服务状态

systemctl restart nginx #重启服务

systemctl reload nginx #不关闭服务并重新载入配置

添加开机启动
创建一个脚本文件
vi /etc/init.d/nginx

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n

quot;Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
} # Stop nginx daemons functions.
stop() {
echo -n


quot;Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n


quot;Reloading $prog: "
#kill -HUP cat ${nginx_pid}
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo


quot;Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL

给脚本执行权限
chmod a+x /etc/init.d/nginx
添加开机服务
echo /etc/init.d/nginx start >>/etc/rc.local
给开机服务添加执行权限
chmod +x /etc/rc.local

为nginx启用PHP服务 并开启PATHINFO
如不开启PATHINFO 动态PHP无法打开出现404错误:
找到这段代码

pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

把下面的location ~ .php$ { 一段注释去掉
并把fastcgi_param修改成

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

重启服务即可

或直接添加以下代码

location ~ \\.php {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+?\\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
client_max_body_size 20480M; #允许客户端上传文件最大大小
include fastcgi_params;
}

在HTTP参数中添加以下代码 使php支持.htaccess
include *.htaccess;

开启文件浏览功能
在http参数中添加

autoindex on; # 开启目录文件列表
autoindex_exact_size on; # 显示出文件的确切大小,单位是bytes
autoindex_localtime on; # 显示的文件时间为文件的服务器时间
charset utf-8,gbk,gb2312; # 避免中文乱码

为某个站点添加则在location中添加
location / { }

域名转发配置

修改配置文件 添加如下代码

server
{
listen 80;
server_name xxx.cn;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://xxxxx;
}

}

server_name为域名

proxy_pass为跳转的地址

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容