最近开始折腾 Nginx, 我想自己编译它来搭建一个服务器。

我使用的系统是 CentOS, 也就是说很多风格会与 Red Hat 很像。所以安装命令是 yum 而不是 Ubuntu 那些 Debian 系统的 apt-get. 编译之前需要安装一些东西,它们是 gcc, pcre, zlib, openssl, libxml2, libxslt, gd, GeoIP. 由于我想让 Nginx 有尽量多的功能,所以才要安装 libxml2, libxslt, gd, GeoIP 之类的东西。

首先 gcc 是 GNU Compiler Collection, 编译 Nginx 必须要用这个编译器。然后 pcre 全称是 Perl Compatible Regular Expression, Nginx 解析 URL 的时候需要用到。zlib 是 Nginx 用来压缩数据的。openssl 是 Open Secure Socket Layer. gd 是一个处理图像的库。而 GeoIP 似乎可以用来查询 IP 的地理位置。

取得管理员权限后,运行:

1
2
3
4
5
6
7
yum install gcc
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
yum install libxml2 libxml2-devel libxslt libxslt-devel
yum install gd gd-devel
yum install GeoIP GeoIP-devel

之后我们为 Nginx 创建用户和组:

1
/usr/sbin/useradd --shell /sbin/nologin --home-dir /usr/local/nginx www

再创建 Nginx 的网页和日志目录:

1
2
3
4
5
mkdir /home/wwwroot
chown -R www:www /home/wwwroot

mkdir /home/wwwlogs
chown -R www:www /home/wwwlogs

然后,下载 Nginx 的源代码,并设置权限,准备编译:

1
2
3
wget http://www.nginx.org/download/nginx-0.8.54.tar.gz
tar xzvf nginx-0.8.54.tar.gz
chmod -R 777 .

然后开始配置:

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
cd nginx-0.8.54
./configure \
    --conf-path=/etc/nginx/nginx.conf \
    --error-log-path=/home/wwwlogs/nginx.error.log \
    --http-log-path=/home/wwwlogs/nginx.access.log \
    --user=www \
    --group=www \
    --with-pcre \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_xslt_module \
    --with-http_image_filter_module \
    --with-http_geoip_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-rtsig_module \
    --with-select_module \
    --with-poll_module \
    --with-ipv6

如果配置没有问题,执行编译:

1
2
make
make install

之后,我们用 vi 打开文件 /etc/init.d/nginx . 其实原来没有这个文件,所以我们打开的时候,编辑器里应该是空的。然后输入以下脚本:

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
#! /bin/sh
# chkconfig: 2345 85 15
# Description: Nginx is a World Wide Web server.
# Author: Ryan Norbauer http://norbauerinc.com
# Modified: Geoffrey Grosenbach http://topfunky.com
# Modified: Clement NEDELCU
# Modified: Euyuil http://euyuil.com
# Reproduced with express authorization from its contributors
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME

# If the daemon file is not found, terminate the script.
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 " could 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 configuration..."
        d_reload
        echo "reloaded."
    ;;
    restart)
        echo -n "Restarting $DESC: $NAME"
        d_stop
        # Sleep for two seconds before starting again, this should give the
        # Nginx daemon some time to perform a graceful stop.
        sleep 2
        d_start
        echo "."
    ;;
    *)
    echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
    echo "$1"
    exit 3
    ;;
esac

exit 0

这个脚本是我从书上弄来的,我对 Linux 的 .sh 文件不是很熟悉。原作者的脚本中有几处错误,我这里给改了以下。这个脚本的工作原理就是通过执行 /usr/local/nginx/sbin/nginx 来操纵 Nginx 的 Daemon. 请注意,这里连注释也需要一起复制,因为一是尊重原作者版权,二是有某行其实是必须的。嘿嘿,至于是哪行……

把文件保存到 /etc/init.d/nginx 之后,将其加上可执行权限:

1
chmod +x /etc/init.d/nginx

然后使用 chkconfig 将其标记为系统服务:

1
chkconfig --add nginx

之后,Nginx 在系统启动的时候应该就会自己启动了。如果想要重新启动 Nginx 而不重新启动计算机,执行:

1
service nginx restart

如果你想关掉 Nginx, 执行:

1
service nginx stop

如果你又后悔了,想打开 Nginx, 执行:

1
service nginx start

原创文章,转载请注明来源:http://euyuil.com/2188/note-about-nginx-installation/