CentOS编译安装Nginx
下载
wget
解压缩
tar zxvf nginx-1.9.6.tar.gz
进入nginx目录
cd nginx-1.9.6
设置,编译,安装(configure过程可能出现的情况,见文章最尾)
./configure --prefix=/data/server/nginx
make && make install && make clean
修改nginx配置文件
vi /data/server/nginx/conf/nginx.conf1
将根目录修改至 /data/webroot/localhost
找到
location / {
root html; # 根目录
index index.html index.htm; # 默认文档
}
改为
location / {
root /data/webroot/localhost; # 站点根目录路径
index index.html index.htm; # 目前只放静态页,所以不需要改
}
创建nginx系统服务脚本
vi /etc/init.d/nginx
填入如下内容
#!/bin/sh
#
#nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /data/webroot/logs/localhost/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/data/server/nginx/sbin/nginx" # nginx启动文件
prog=$(basename $nginx)
NGINX_CONF_FILE="/data/server/nginx/conf/nginx.conf" # 配置文件路径
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
保存
:wq对脚本添加可执行权限
chmod +x /etc/init.d/nginx
添加nginx到自启动服务
chkconfig --add nginx
设置开机启动
chkconfig nginx on
开启访问端口
vi /etc/sysconfig/iptables
添加如下
-A INPUT –m state –state NEW –m tcp –p tcp –dport 80 –j ACCEPT
重启防火墙以应用规则
/etc/init.d/iptables restart1
此后,执行命令脚本即可,无需执行原路径的执行文件
/etc/init.d/nginx start # 启动nginx
/etc/init.d/nginx stop # 停止nginx
/etc/init.d/nginx restart # 重启nginx
/etc/init.d/nginx reload # 重新加载配置文件
原操作命令