nginx在CentOS中的编译安装
在安装php5.6之后,nginx莫名其妙的消失了,因此只能重装nginx了,这次我尝试用编译安装的方式来解决
准备
安装相关的依赖包
yum -y install gcc pcre pcre-devel zlib zlib-devel
下载
地址:http://nginx.org/download/nginx-1.24.0.tar.gz
wget http://nginx.org/download/nginx-1.24.0.tar.gz
编译安装
解压缩并进入目录
tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
检测安装环境是否满足安装条件
./configure --prefix=/opt/base/nginx/nginx --with-openssl=/usr/local/ssl --with-http_ssl_module --with-http_stub_status_module
这里openssl是单独配置的,由于需要https的支持,增加了 --with-http_ssl_module --with-http_stub_status_module 两项配置 否则启动会报错:[emerg] the "ssl" parameter requires ngx_http_ssl_module
这一步完成后,执行make && make install 即可完成安装。
为了更好的简化操作,可以将nginx命令做成系统服务。
编写nginx 启动配置
#! /bin/bash
# chkconfig: - 85 15
PATH=/opt/base/nginx/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
保存,命名为nginx,传到/etc/init.d/目录下
修改权限
chmod a+x nginx
注册服务
chkconfig --add nginx
设置开机启动
chkconfig nginx on
相关命令
启动nginx服务
systemctl start nginx.service
停止nginx服务
systemctl stop nginx.service
重启nginx服务
systemctl restart nginx.service
重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
systemctl reload nginx.service
由于我本机未配置成功,那么启动命令需要从程序所在位置进行定位启动
我这边的启动命令如下
/opt/base/nginx/nginx/sbin/nginx -c /opt/base/nginx/nginx/conf/nginx.conf
遇到的问题
nginx php 403 forbidden
908822#0: *2 FastCGI sent in stderr: "Primary script unknown" while reading response header
类似问题,解决办法,是在nginx配置的server节点下,增加index 和 root 配置项,完整配置(php)如下
server {
listen 443 ssl;
server_name a.test.cn;
ssl_certificate /opt/test/cert/a.test.cn.crt;
ssl_certificate_key /opt/test/cert/a.test.cn.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
root /opt/test/app/;
index index.html index.php;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
rewrite ^/news/([0-9]+)/([0-9]+).html /show_news.php?cid=$1&id=$2 last;
rewrite ^/news/list/([0-9]+)-([0-9]+).html /list_news.php?id=$1&pid=1 last;
rewrite ^/news/list-([0-9]+)/([0-9]+).html /show_news.php?cid=$1&id=$2 last;
}