设为首页 收藏本站
查看: 679|回复: 0

[经验分享] Nginx源代码安装

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-6-23 10:27:19 | 显示全部楼层 |阅读模式
Linux下安装软件有三种方式,这里我以源代码编译安装为主。服务器最小化安装后,安装依赖包。

出于管理和安全的目的,我们希望使用一个指定的普通用户身份去运行我们的Web服务器。所以,我们首先增加一个普通用户用于运行我们的Nginx。

1
2
[iyunv@master ~]# groupadd nginx
[iyunv@master ~]# useradd -g nginx nginx



关闭系统防火墙,

1
2
[iyunv@master ~]# service iptables stop
[iyunv@master ~]# chkconfig iptables off




1. 下载最新稳定版本并安装Nginx
然后下载、解压并编译安装我们的Nginx, 这里使用的是最新稳定版本,

1
2
3
4
5
6
[iyunv@master ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[iyunv@master ~]# tar -xf nginx-1.8.0.tar.gz -C /usr/local/src
[iyunv@master ~]# cd /usr/local/src/nginx-1.8.0
[iyunv@master nginx-1.8.0]# ./configure --user=nginx --group=nginx
--with-http_ssl_module \
--with-http_sub_module




安装过程比较简单,./configure过程会报出一些依赖关系,这里一一解决之。首先,操作系统是最小化安装,并没有安装gcc,所以,第一步进行./configure的时候,就会报错。

当出现如下错误时,需要安装ssl的开发包,
1
2
3
4
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.




当出现如下错误时,需要安装zlib的开发包,
1
2
3
4
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.



1
2
3
4
[iyunv@master ~]# yum install -y pcre-devel
[iyunv@master ~]# yum install -y gcc
[iyunv@master ~]# yum install -y zlib-devel
[iyunv@master ~]# yum install -y openssl-devel



下面来看看./configure后面几个常用的参数:

1
2
3
4
--prefix=<dir>         指定安装主目录,默认为/usr/local/nginx
--user=<user>          指定用户身份,如果没有指定则默认使用nobody
--group=<group>        指定组身份
--with-http_ssl_module 启用https支持




2. Nginx的启动、重启与停止
安装完毕,我们就可以启动Nginx了,
1
[iyunv@master ~]# /usr/local/nginx/sbin/nginx -c /usr/loca/nginx/conf/nginx.conf




-c是用来指定Nginx的主配置文件,如果没有指定则默认为/usr/loca/nginx/conf/nginx.conf文件。启动后,可以用ps与netstat命令查看是否启动成功,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[iyunv@master ~]# ps -ef |grep nginx
root      1059     1  0 02:49 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx     1061  1059  0 02:49 ?        00:00:00 nginx: worker process                                          
root      1063  1013  0 02:49 pts/0    00:00:00 grep nginx

[iyunv@master ~]# netstat -antup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 192.168.1.151:8080          0.0.0.0:*                   LISTEN      1059/nginx         
tcp        0      0 192.168.1.150:8080          0.0.0.0:*                   LISTEN      1059/nginx         
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1059/nginx         
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      801/sshd            
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      877/master         
tcp        0      0 192.168.1.129:22            192.168.1.106:56004         ESTABLISHED 1009/sshd           
tcp        0      0 :::22                       :::*                        LISTEN      801/sshd            
tcp        0      0 ::1:25                      :::*                        LISTEN      877/master         
udp        0      0 0.0.0.0:68                  0.0.0.0:*                               1007/dhclient



启动成功,我们可以访问首页去验证一下,
wKiom1WE1hGzuAqzAAMO065f9_I122.jpg

3. Nginx启动脚本
Nginx并没有提供类似System V服务的管理脚本,如果我们希望开机时要让Nginx自动启动,可以执行如下命令:
1
2
[iyunv@master ~]# echo “/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
” >> /etc/rc.local




当然,如果我们对System V的服务管理脚本情有独钟的话,可以参考如下脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
[iyunv@master ~]# cat /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# 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 0

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 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
    killall -9 nginx
}

restart() {
    configtest || return $?
    stop
    sleep 1
    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



最后,给脚本一个可执行的权限,然后使用chkconfig命令对其进行管理,
1
2
[iyunv@master ~]# chmod 755 /etc/init.d/nginx
[iyunv@master ~]# chkconfig nginx on




当我们对Nginx的配置文件做过一些更改后,希望在不中断当前服务的情况下,进行一个平滑的重启,可以使用如下命令,
1
[iyunv@master ~]# service nginx reload



脚本中的reload函数会首先对配置文件做一个语法格式的检查,使用的是如下命令,
1
[iyunv@master ~]# /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf



当语法格式检查通过后,会对Nginx发出一个标记为1或者说是HUP的信号,Nginx收到后会关闭旧进程,打开新进程,如果有进程正在为一个用户提供服务,则会等待这次服务结束。
当然,我们也可以使用service nginx restart的方式去重启服务。停止Nginx,直接service nginx stop即可,或者kill掉所有的Nginx进程。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-79734-1-1.html 上篇帖子: nginx特性及基本配置 下篇帖子: nginx worker_processes设定 源代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表