首先致谢:本文大量参考了金山逍遥网系统架构师-张宴的Nginx 0.7.x + PHP 5.2.10(FastCGI)搭建胜过Apache十倍的Web服务器(第5版)[原创] 一文,谢谢张宴为nginx推广普及所作的贡献。
之所以要在ubuntu server 9.04(以下简称us904)下编译安装nginx,是因为us904官方源中提供的版本有些旧,为nginx0.6.35,而当前nginx官方最新稳定版为0.7.61,最新开发版本为0.8.9(详见:http://www.nginx.org/),下面以0.8.9版为例进行nginx下的编译安装:
首先安装编译nginx所需的软件包:
1.Perl 5 Compatible Regular Expression Library - development files
sudo apt-get install libpcre3-dev
2.SSL development libraries, header files and documentation
sudo apt-get install libssl-dev
3.make 工具
sudo apt-get install make
创建启用nginx所使用的用户组和用户
sudo groupadd www
sudo useradd -g www www
sudo mkdir -v -p /www/htdocs/site1
sudo chown -R www:www /www/htdocs/site1
然后下载nginx源码包
wget http://sysoev.ru/nginx/nginx-0.8.9.tar.gz
开始编译安装nginx
tar zxvf nginx-0.8.9.tar.gz
cd nginx-0.8.9
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
sudo make
sudo make install
然后建立nginx服务控制脚本程序
sudo nano /etc/init.d/nginx
输入以下内容:
![更多...](http://blog.chinadev.net/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif)
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/webserver/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
. /lib/lsb/init-functions
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid /
--exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid /
--exec $DAEMON || true
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile /
/var/run/$NAME.pid --exec $DAEMON || true
sleep 1
start-stop-daemon --start --quiet --pidfile /
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid /
--exec $DAEMON || true
echo "$NAME."
;;
status)
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
exit 1
;;
esac
exit 0
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/webserver/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
. /lib/lsb/init-functions
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid /
--exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid /
--exec $DAEMON || true
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile /
/var/run/$NAME.pid --exec $DAEMON || true
sleep 1
start-stop-daemon --start --quiet --pidfile /
/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid /
--exec $DAEMON || true
echo "$NAME."
;;
status)
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
exit 1
;;
esac
exit 0
然后就可以通过
sudo /etc/init.d/nginx start(stop|restart|status)等命令及参数来控制nginx和查看状态信息了
至于nginx的具体配置,留待下一次继续讨论学习。
在测试本次编译安装的同时,我在另一台虚拟机us904上通过sudo apt-get install nginx安装nginx来查看两种方式的不同,
发现nginx_0.6.35-0ubuntu1_i386.deb文件主要有以下文件拷贝过程
sudo dpkg -L nginx
/.
/usr
/usr/sbin
/usr/sbin/nginx
/usr/share
/usr/share/doc
/usr/share/doc/nginx
/usr/share/doc/nginx/changelog.gz
/usr/share/doc/nginx/README
/usr/share/doc/nginx/copyright
/usr/share/doc/nginx/NEWS.Debian.gz
/usr/share/doc/nginx/changelog.Debian.gz
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/nginx.1.gz
/etc
/etc/nginx
/etc/nginx/sites-available
/etc/nginx/sites-available/default
/etc/nginx/sites-enabled
/etc/nginx/conf.d
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/win-utf
/etc/init.d
/etc/init.d/nginx
/etc/logrotate.d
/etc/logrotate.d/nginx
/var
/var/log
/var/log/nginx
/var/lib
/var/lib/nginx
/var/www
/var/www/nginx-default
/var/www/nginx-default/50x.html
/var/www/nginx-default/index.html
由此猜想在生产环境中是否可以避免安装过多软件包,而直接替换/usr/sbin/nginx文件来实现,具体运行稳定性有待验证。
ps:
1.如果不想输入命令时带有sudo,可以通过下面方式切换至root
A.sudo -s -H 输入当前用户密码
B.sudo passwd root 设置root密码
su root 输入root密码
2.查看某个包具体所在的包名,可以通过sudo apt-cache search *** 来查找