Centos搭建nginx环境,编译,添加服务,开机启动。

时间:2022-03-11 15:14:55

  首先安装所需的安装库,yum -y install gcc gcc-c++ autoconf libtool* openssl openssl-devel

  编译的时候,若有提示错误,提示缺少某个库,yun search 库名 安装上此库的devel版本 例如 open-devel,pcre-devel,zlib-     devel,libpng-devel等。

  yum install libxml2 mingw32-glib2.noarch

  ./configure 不加prefix参数,默认的安装目录在/usr/local/nginx,nginx的主程序在/usr/local/nginx/sbin/nginx

  make && make install

  为nginx创建用户www ,www组Nginx日志

  groupadd www

  useradd www -g www -d /dev/null -s /sbin/nolgin

  mkdir -p /var/log/nginx

  chmod +w /var/log/nginx

  chown -R www:www /var/log/nginx

  默认centos没有把nginx添加到服务里面,下面添加服务,配置开机启动

  添加系统服务:

     新建文件nginx

      vi /etc/init.d/nginx

  加入下面的内容

  

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: -
# 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: /var/run/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 nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx start() {
[ -x $nginx ] || exit
[ -f $NGINX_CONF_FILE ] || exit
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq ] && touch $lockfile
return $retval
} stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq ] && rm -f $lockfile
return $retval
} restart() {
configtest || return $?
stop
sleep
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 >&
} case "$1" in
start)
rh_status_q && exit
$
;;
stop)
rh_status_q || exit
$
;;
restart|configtest)
$
;;
reload)
rh_status_q || exit
$
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit
esac

  为此服务添加可执行属性

  chmod +x /etc/init.d/nginx

  添加ginx为系统服务平开机启动

  chkconfig --add nginx

  chkconfig nginx on

  启动nginx

  service nginx start

  平滑变更nginx配置

  service nginx reload