CentOS环境下安装配置Nginx及系统服务化
1、准备工作
首要任务是安装GCC、PCRE(Perl Compatible Regular Expression)、zlib以及OpenSSL这四个软件组件。
由于Nginx由C语言编写,必须借助GCC进行编译;其Rewrite功能及HTTP模块依赖于PCRE库;而Gzip压缩功能则需要zlib的支持。
执行命令“# gcc”以检查环境是否已就绪;若终端返回“gcc: no input files”提示,即表明编译器已正确安装。
若未安装,则需运行“# yum install gcc”指令进行获取;过程中可能需要多次输入y以确认操作。
安装完成后,可再次使用“#gcc”测试,或执行“# gcc -v”来查看具体的版本号信息。
采用相同的方法,依次安装PCRE、zlib和OpenSSL的开发包(devel代表develop开发包):
# yum install -y pcre pcre-devel
# yum install -y zlib zlib-devel
# yum install -y openssl openssl-devel
2、下载并安装
新建nginx-src目录并进入其中;随后从官方地址http://nginx.org/获取资源,依次执行解压、配置、编译及安装操作:
# mkdir nginx-src && cd nginx-src
# wget http://nginxorg/download/nginx-targz
# tar xzf nginx-targz
# cd nginx-3
# /configure
# make
# make install
# whereis nginx
nginx: /usr/local/nginx
默认安装位置设定为/usr/local/nginx;切换至该目录下的sbin子路径,即可对服务进行启动或停止控制。
# /nginx -h
nginx version: nginx/3
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: conf/nginxconf)
-g directives : set global directives out of configuration file
启动方式:nginx
停止方式:nginx -s stop
3、添加到系统服务
利用命令“# vi /etc/init.d/nginx”调起编辑器,并录入以下脚本内容:
#!/bin/sh
# chkconfig: 2345 85 15
# Startup script for the nginx Web Server
# description: nginx is a World Wide Web server
# It is used to serve HTML files and CGI
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginxpid
# config: /usr/local/nginx/conf/nginxconf
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx deamon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/initd/$NAME
test -x $DAEMON || exit 0
d_start(){
$DAEMON || echo -n "already running"
}
d_stop(){
$DAEMON -s quit || echo -n "not running"
}
d_reload(){
$DAEMON -s reload || echo -n "can not reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo ""
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo ""
;;
reload)
echo -n "Reloading $DESC conf"
d_reload
echo "reload "
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 2
d_start
echo ""
;;
*)
echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
保存退出后,执行后续命令赋予其可执行权限;接着添加配置项并验证状态。
可通过chkconfig修改相关参数,亦可使用ntsysv工具调整开机自启动的设置。
# chmod +x /etc/initd/nginx
# chkconfig --add nginx
# chkconfig nginx on/off
# chkconfig --list nginx
nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。