ubuntu下搭建nginx相关服务

code太久,回头还要研究webservice,接着就在自己的vm机上搭建了一个ubuntu系统,开始了nginx各种实验,当然,实验前必须得安装nginx了。

引用:
【“Nginx (“engine x”) 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。
Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 作为 Web 服务器的网站也越来越多,其中包括新浪博客、新浪播客、网易新闻等门户网站频道,六间房、56.com等视频分享网站,Discuz!官方论坛、水木社区 等知名论坛,豆瓣、YUPOO相册、海内SNS、迅雷在线等新兴Web 2.0网站。”】。

为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE(Perl Compatible Regular Expressions)包。您可以到 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz
tar zxvf pcre-8.12.tar.gz
解压后,在配置nginx的时候指定到这个目录即可。

目前可以确定最稳定的nginx版本为1.2.2。

shell下安装:

下载1.2.2版

wget http://nginx.org/download/nginx-1.2.2.tar.gz
tar zxvf nginx-1.2.2.tar.gz

很多人可能会碰到这样的错误

./configure: error: the HTTP rewrite module requires the PCRE library.

这是因为没有安装pcre的支持库。

apt-get install libpcre3 libpcre3-dev

然后开始编译安装

cd nginx-1.2.2
./configure --prefix=/opt/webservice/nginx --with-pcre=/hksahdow/pcre-8.12 --with-http_stub_status_module --with-http_gzip_static_module
make
make install

这样nginx就会被安装到“/opt/webservice/nginx”下。
其中参数 –with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态。至于prefix,就不用我啰嗦了。

如果你在make 的时候出现“make: *** 没有规则可以创建“default”需要的目标“build”。 停止。”,那么说明你跟我犯了同一个错误,你一定没有注意configure时的错误提示:

./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
–without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
–with-http_ssl_module –with-openssl= options.

看清楚否?没有opensll library!怎么办?装呗!

sudo apt-get install openssl
sudo apt-get install libssl-dev

装完之后出现的提示也很有用的,先记下来:

Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1 library is not used
+ using system zlib library

nginx path prefix: “/usr/local/nginx”
nginx binary file: “/usr/local/nginx/sbin/nginx”
nginx configuration prefix: “/usr/local/nginx/conf”
nginx configuration file: “/usr/local/nginx/conf/nginx.conf”
nginx pid file: “/usr/local/nginx/logs/nginx.pid”
nginx error log file: “/usr/local/nginx/logs/error.log”
nginx http access log file: “/usr/local/nginx/logs/access.log”
nginx http client request body temporary files: “client_body_temp”
nginx http proxy temporary files: “proxy_temp”
nginx http fastcgi temporary files: “fastcgi_temp”

安装成功后 /usr/local/www/nginx 目录下有四个子目录分别是:conf、html、logs、sbin 。其中 Nginx 的配置文件存放于 conf/nginx.conf,Nginx 只有一个程序文件位于 sbin 目录下的 nginx 文件。确保系统的 80 端口没被其他程序占用,运行 sbin/nginx 命令来启动 Nginx,打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

静态文件服务器搭建
在/usr/nginx/conf/nginx.conf文件中添加下列配置 location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /opt/resources; expires 1d; }

创建/opt/resources目录,将css,images等文件夹复制到下面。 假如images目录下有logo.gif图片,启动nginx后,现在可以通过下面的方式查看: http://localhost/images/logo.gif

创建系统服务:

cd /etc/init.d

创建nginx脚本,内容如下:

#!/bin/sh

### BEGIN INIT INFO
# Provides:     nginx
# Required-Start:
# Required-Stop:
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description: nginx
# Description: nginx server
### END INIT INFO

. /lib/lsb/init-functions

PROGRAM=/opt/webservice/nginx/sbin/nginx


test -x $PROGRAM || exit 0

case "$1" in
  start)
     log_begin_msg "Starting Nginx server"
     /opt/webservice/nginx/sbin/nginx
     log_end_msg 0
     ;;
  stop)
     PID=`cat /opt/webservice/nginx/logs/nginx.pid`
     log_begin_msg "Stopping Nginx server"
     if [ ! -z "$PID" ]; then
        kill -15 $PID
     fi
     log_end_msg 0
     ;;
  status)
     $0 stop
     $0 start
     ;;
  *)
     log_success_msg "Usage: service nginx {start|stop|status}"
     exit 1
esac

exit 0

然后运行下面的命令:
sudo chmod +x ./nginx
sudo update-rc.d nginx defaults
现在可以使用下面的命令了,重新启动nginx会自动启动
sudo service nginx start
sudo service nginx stop

nginx其他的配套环境陆续更新 wait install….