一、服务安装
- 首先安装前置服务
[root@localhost ~]# yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
zlib:Nginx提供gzip模块,需要zlib库支持。
openssl:Nginx提供SSL功能
pcre:支持地址重写rewrite功能 - 创建服务使用的用户和组并锁定无法登录
[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -r -g nginx nginx
[root@localhost ~]# usermod -s /sbin/nologin nginx
-编译安装文件
编译部分配置项说明
--with-xxx 代表默认没有打开的功能
--without-xxx 代表默认打开的功能
--prefix=path 代表安装路径
--sbin-path=path sbin路径
--conf-path 配置文件
--pid-path 代表进程号保存文件
--error-log-path错误日志
--lock-path 锁文件
--user ps看到的启动进程用户
--group ps看到的启动进程用户所在组
--with-http_ssl_module 支持SSL模块
--with-http_flv_module 为flv伪流媒体服务端提供支持
[root@localhost nginx-1.12.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/usr/local/nginx/log/error.log --pid-path=/usr/local/nginx/nginx.pid --lock-path=/usr/local/nginx/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
二、配置开机自启动及设置为系统服务
源码安装的启动方式
#检查安装参数
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --error-log-path=/usr/local/nginx/log/error.log --pid-path=/usr/local/nginx/nginx.pid --lock-path=/usr/local/nginx/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module
#启动
[root@localhost nginx]# /usr/local/nginx/sbin/nginx
[root@localhost nginx]# ps -ef|grep nginx
root 3947 1 0 13:09 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 3948 3947 0 13:09 ? 00:00:00 nginx: worker process
root 3950 1291 0 13:09 pts/0 00:00:00 grep nginx
[root@localhost nginx]# netstat -tulnp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3963/nginx
#停止
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s stop
#使运行中的Nginx 服务重新加载nginx.conf 文件并生效
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s reload
#优雅停止服务,关闭监听端口,停止接收新的连接,然后把当前正在处理的连接全部处理完,最后再退出进程。
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s quit
修改配置文件
[root@localhost nginx]# vim /usr/local/nginx/nginx.conf
1 ######nginx.conf########
2 user nginx nginx; #修改服务的用户和组
3 worker_processes 1; #启动工作进程数量,一般等于CPU的核心数
设置开机启动脚本
[root@localhost init.d]# vim nginx
1 #! /bin/bash
2 # Description: Startup script for webserver on CentOS. cp it in /etc/init.d and
3 # chkconfig --add nginx && chkconfig nginx on
4 # then you can use server command control nginx
5 #
6 # chkconfig: 2345 08 99
7 # description: Starts, stops nginx
8
9 set -e
10 PATH=$PATH:/usr/local/nginx/sbin/
11 DESC="nginx daemon"
12 NAME=nginx
13 DAEMON=/usr/local/nginx/sbin/$NAME
14 CONFIGFILE=/usr/local/nginx/nginx.conf
15 PIDFILE=/usr/local/nginx/nginx.pid
16 SCRIPTNAME=/etc/init.d/$NAME
17
18 # Gracefully exit if the package has been removed.
19 test -x $DAEMON || exit 0
20
21 d_start() {
22 $DAEMON -c $CONFIGFILE || echo -n " already running"
23 }
24
25 d_stop() {
26 kill -QUIT `cat $PIDFILE` || echo -n " not running"
27 }
28
29 d_reload() {
30 kill -HUP `cat $PIDFILE` || echo -n " can't reload"
31 }
32
33 case "$1" in
34 start)
35 echo -n "Starting $DESC: $NAME"
36 d_start
37 echo "."
38 ;;
39 stop)
40 echo -n "Stopping $DESC: $NAME"
41 d_stop
42 echo "."
43 ;;
44 reload)
45 echo -n "Reloading $DESC configuration..."
46 d_reload
47 echo "reloaded."
48 ;;
49 restart)
50 echo -n "Restarting $DESC: $NAME"
51 d_stop
52 sleep 1
53 d_start
54 echo "."
55 ;;
56 *)
57 echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
58 exit 3
59 ;;
60 esac
61 exit 0
[root@localhost init.d]# chmod 700 /etc/init.d/nginx
添加开机自启动
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 2345 nginx on
[root@localhost init.d]# chkconfig |grep nginx
nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
服务相关命令/etc/init.d/nginx start|stop|restart|reload
启动配置参考文章
日志切割脚本
vi /usr/local/nginx/sbin/cut_nginx_log.sh
#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path="/usr/local/nginx/logs/"
logs_bak_path="/data/logs/nginx/"
mkdir -p ${logs_bak_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
cp ${logs_path}access.log ${logs_bak_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
rm -rf ${logs_path}*.log
kill -USR1 `cat /var/run/nginx.pid`
网友评论