CentOS7源码安装Nginx
Posted xiaozhebao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7源码安装Nginx相关的知识,希望对你有一定的参考价值。
环境:
1 [root@gz01-nginx-proxy-master ~]# uname -r
2 3.10.0-693.el7.x86_64
3 [root@gz01-nginx-proxy-master ~]# cat /etc/redhat-release
4 CentOS Linux release 7.4.1708 (Core)
依赖软件包安装:
1 [root@gz01-nginx-proxy-master tools]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
2 [root@gz01-nginx-proxy-master tools]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel
创建www用户,用于启动Nginx:
1 [root@gz01-nginx-proxy-master tools]# useradd -s /sbin/nologin -M www
Nginx源码包解压编译安装:
1 [root@gz01-nginx-proxy-master local]# cd /usr/local/tools/
2 [root@gz01-nginx-proxy-master tools]# ll
3 total 816
4 -rw-r--r-- 1 root root 833473 Jan 27 2016 nginx-1.8.1.tar.gz
5 [root@gz01-nginx-proxy-master tools]# tar -zxvf nginx-1.8.1.tar.gz
6 [root@gz01-nginx-proxy-master tools]# cd nginx-1.8.1
7 [root@gz01-nginx-proxy-master nginx-1.8.1]# ./configure --prefix=/usr/local/nginx-1.8.1 8 > --user=www --group=www 9 > --with-http_ssl_module 10 > --with-http_stub_status_module --with-file-aio
11 [root@gz01-nginx-proxy-master nginx-1.8.1]# make
12 [root@gz01-nginx-proxy-master nginx-1.8.1]# make install
测试配置并启动Nginx:
1 [root@gz01-nginx-proxy-master nginx-1.8.1]#ln -s /usr/local/nginx-1.8.1/ /usr/local/nginx
2 [root@gz01-nginx-proxy-master nginx-1.8.1]# /usr/local/nginx/sbin/nginx -t
3 [root@gz01-nginx-proxy-master nginx-1.8.1]# /usr/local/nginx/sbin/nginx
查看nginx状态
1 [root@gz01-nginx-proxy-master local]# netstat -nltp|grep 80
2 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2231/nginx: master
设置nginx随开机启动:(nginx官网有模板,调试一下可以用)
1 [root@gz01-nginx-proxy-master ~]# vim /etc/init.d/nginx
2
3 #!/bin/sh
4 #
5 # nginx - this script starts and stops the nginx daemon
6 #
7 # chkconfig: - 85 15
8 # description: NGINX is an HTTP(S) server, HTTP(S) reverse 9 # proxy and IMAP/POP3 proxy server
10 # processname: nginx
11 # config: /usr/local/nginx/conf/nginx.conf
12 # config: /etc/sysconfig/nginx
13 # pidfile: /usr/local/nginx/logs/nginx.pid
14
15 # Source function library.
16 . /etc/rc.d/init.d/functions
17
18 # Source networking configuration.
19 . /etc/sysconfig/network
20
21 # Check that networking is up.
22 [ "$NETWORKING" = "no" ] && exit 0
23
24 nginx="/usr/local/nginx/sbin/nginx"
25 prog=$(basename $nginx)
26
27 NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
28
29 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
30
31 lockfile=/var/lock/subsys/nginx
32
33 make_dirs() {
34 # make required directories
35 user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed ‘s/[^*]*--user=([^ ]*).*/1/g‘ -`
36 if [ -n "$user" ]; then
37 if [ -z "`grep $user /etc/passwd`" ]; then
38 useradd -M -s /bin/nologin $user
39 fi
40 options=`$nginx -V 2>&1 | grep ‘configure arguments:‘`
41 for opt in $options; do
42 if [ `echo $opt | grep ‘.*-temp-path‘` ]; then
43 value=`echo $opt | cut -d "=" -f 2`
44 if [ ! -d "$value" ]; then
45 # echo "creating" $value
46 mkdir -p $value && chown -R $user $value
47 fi
48 fi
49 done
50 fi
51 }
52
53 start() {
54 [ -x $nginx ] || exit 5
55 [ -f $NGINX_CONF_FILE ] || exit 6
56 make_dirs
57 echo -n $"Starting $prog: "
58 daemon $nginx -c $NGINX_CONF_FILE
59 retval=$?
60 echo
61 [ $retval -eq 0 ] && touch $lockfile
62 return $retval
63 }
64
65 stop() {
66 echo -n $"Stopping $prog: "
67 killproc $prog -QUIT
68 retval=$?
69 echo
70 [ $retval -eq 0 ] && rm -f $lockfile
71 return $retval
72 }
73
74 restart() {
75 configtest || return $?
76 stop
77 sleep 1
78 start
79 }
80
81 reload() {
82 configtest || return $?
83 echo -n $"Reloading $prog: "
84 killproc $prog -HUP
85 retval=$?
86 echo
87 }
88
89 force_reload() {
90 restart
91 }
92
93 configtest() {
94 $nginx -t -c $NGINX_CONF_FILE
95 }
96
97 rh_status() {
98 status $prog
99 }
100
101 rh_status_q() {
102 rh_status >/dev/null 2>&1
103 }
104
105 case "$1" in
106 start)
107 rh_status_q && exit 0
108 $1
109 ;;
110 stop)
111 rh_status_q || exit 0
112 $1
113 ;;
114 restart|configtest)
115 $1
116 ;;
117 reload)
118 rh_status_q || exit 7
119 $1
120 ;;
121 force-reload)
122 force_reload
123 ;;
124 status)
125 rh_status
126 ;;
127 condrestart|try-restart)
128 rh_status_q || exit 0
129 ;;
130 *)
131 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
132 exit 2
133 esac
1 [root@gz01-nginx-proxy-master logs]# chmod +x /etc/init.d/nginx
2
3 [root@gz01-nginx-proxy-master logs]# chkconfig --add nginx
4
5[root@gz01-nginx-proxy-master logs]# chkconfig nginx on
以上是关于CentOS7源码安装Nginx的主要内容,如果未能解决你的问题,请参考以下文章