Nginx简单介绍
- Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Igor Sysoev started development of Nginx in 2002, with the first public release in 2004. Nginx now hosts nearly 7.67% (35.5M) of all domains worldwide.
- Nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
- Nginx is one of a handful of servers written to address the C10K problem. Unlike traditional servers, Nginx doesn't rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.
- Even if you don't expect to handle thousands of simultaneous requests, you can still benefit from Nginx's high-performance and small memory footprint. Nginx scales in all directions: from the smallest VPS all the way up to clusters of servers.
- Nginx powers several high-visibility sites, such as WordPress, Hulu, Github, Ohloh, SourceForge, WhitePages and TorrentReactor.
需要用到的安装包:
openssl-devel
zlib-devel
pcre-devel
gcc
nginx-1.2.2.tar.gz
1、解决依赖关系
编译安装nginx需要事先需要安装开发包组"Development Tools"和 "Development Libraries"。同时,还需要专门安装pcre-devel包,安装pcre 库是为使用Nginx支持 HTTP Rewrite 模块。
# yum -y install pcre-devel
2、首先添加nginx用户和组,实现以之运行nginx服务进程
# groupadd -r nginx
# useradd -r -g nginx -s /bin/false -M nginx
# id nginx
3. 解压
# tar xf nginx-1.2.2.tar.gz
4. 接着开始编译:
- # ./configure \
- --prefix=/usr \
- --sbin-path=/usr/sbin/nginx \
- --conf-path=/etc/nginx/nginx.conf \
- --error-log-path=/var/log/nginx/error.log \
- --http-log-path=/var/log/nginx/access.log \
- --pid-path=/var/run/nginx/nginx.pid \
- --lock-path=/var/lock/nginx.lock \
- --user=nginx \
- --group=nginx \
- --with-http_ssl_module \
- --with-http_flv_module \
- --with-http_stub_status_module \
- --with-http_gzip_static_module \
- --http-client-body-temp-path=/var/tmp/nginx/client/ \
- --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
- --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
- --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
- --http-scgi-temp-path=/var/tmp/nginx/scgi \
- --with-pcre
5. 安装nginx
# make && make install
一些主要编译选项 (摘录configure)
- --prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。
- --conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。
- --pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。
- --lock-path=<path> - nginx.lock文件的路径。
- --error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。
- --http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。
- --user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。
- --group=<group> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。
- --with-http_ssl_module -开启HTTP SSL模块,使NGINX可以支持HTTPS请求。需要安装了OPENSSL
- --with-http_flv_module
- --with-http_stub_status_module - 启用 "server status" 页(可有可无)
- --without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。
- --without-http_ssi_module - 禁用 ngx_http_ssi_module
- --without-http_referer_module - 禁用 ngx_http_referer_module
- --without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。
- --without-http_proxy_module - 禁用 ngx_http_proxy_module
- --without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module
- --without-http_memcached_module - 禁用 ngx_http_memcached_module
- --without-http_browser_module - 禁用 ngx_http_browser_module
- --http-proxy-temp-path=PATH - Set path to the http proxy temporary files
- --http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files
- --without-http - 禁用 HTTP server(用作代理或反向代理)
- --with-mail - 启用 IMAP4/POP3/SMTP 代理模块
- --with-mail_ssl_module - 启用 ngx_mail_ssl_module
- --with-openssl=DIR - Set path to OpenSSL library sources
6. 为Nginx提供sysv风格的服务脚本( Red Hat Nginx Init Script Should work on RHEL, Fedora, CentOS. Tested on CentOS 5. Save this file as /etc/init.d/nginx )
# vi /etc/rc.d/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/sbin/nginx"
- prog=$(basename $nginx)
- NGINX_CONF_FILE="/etc/nginx/nginx.conf"
- [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
- lockfile=/var/lock/subsys/nginx
- make_dirs() {
- # make required directories
- user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
- options=`$nginx -V 2>&1 | grep 'configure arguments:'`
- for opt in $options; do
- if [ `echo $opt | grep '.*-temp-path'` ]; then
- value=`echo $opt | cut -d "=" -f 2`
- if [ ! -d "$value" ]; then
- # echo "creating" $value
- mkdir -p $value && chown -R $user $value
- fi
- fi
- done
- }
- start() {
- [ -x $nginx ] || exit 5
- [ -f $NGINX_CONF_FILE ] || exit 6
- make_dirs
- 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
- }
- 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
7. 给予执行权限
# chmod +x /etc/rc.d/init.d/nginx
8. 添加到服务列表中
# chkconfig --add nginx
# chkconfig nginx on
9. 启动服务
# /etc/init.d/nginx start
10. 查看启动状态
# netstat -tnlp | grep nginx
Active Internet connections (only servers)
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 29475/nginx
本文出自 “昶x月” 博客,请务必保留此出处http://xyuex.blog.51cto.com/5131937/1013414