Linux安装nginx

2012-4-6 王健宇 Linux

今天在虚拟机安装了个centos6.2以及ubuntu server,然后搭建好了vsftpd,就准备安装nginx

简单说下安装过程w3m nginx.org进入nginx官网下载nginx.tar.gz压缩包

然后tar xzvf nginx.tar.gz解压,cd切换到nginx源码目录

按./configure 结果提示错误了

错误就是checking for C compiler default output file name... configure: error: C compiler cannot create executables.
然后百度找了一下资料说是要安装g++

centos使用yum安装g++方法yum install gcc-c++

在这里Ubuntu需要安装下列几个包.
sudo apt-get install libpcre3
sudo apt-get install zlib1g
sudo apt-get install libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install libssl (这个可能会找不到源,可以通过新立德软件包管理器中安装)
sudo apt-get install libssl-dev
如果在build过程中发现缺少其他包可以继续通过apt-get来安装.
g++安装方法apt-get install g++

有时候系统没有make就需要安装

apt-get install make

yum install make

下载pcre-8.30编译安装,make时报错:

source='pcrecpp.cc' object='pcrecpp.lo' libtool=yes
        DEPDIR=.deps depmode=none /bin/sh ./depcomp
        /bin/sh ./libtool --tag=CXX   --mode=compile  -DHAVE_CONFIG_H -I.   -I/usr/kerberos/include   -c -o pcrecpp.lo pcrecpp.cc
libtool: ignoring unknown tag CXX
libtool: unrecognized option `-DHAVE_CONFIG_H'
Try `libtool --help' for more information.
make[1]: *** [pcrecpp.lo] Error 1

这个错误,基本可以断定是缺少系统包造成的,但是缺少哪个包却不明确,这里记录下,这个错误是缺少安装gcc-c++,只需yum -y install gcc-c++,重新configure,make && make install通过。

安装Nginx时报错

错误提示:./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=<path> options.

解决办法:

yum -y install openssl openssl-devel

安装过程中出现的错误及解决办法
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

解决办法:yum -y install pcre-devel

./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.

解决办法:yum install -y zlib-devel
总结在一起就是

yum install gcc openssl-devel pcre-devel zlib-devel

Nginx的启动、停止

启动:
/usr/local/nginx/sbin/nginx  -c /usr/local/nginx/conf/nginx.conf
参数"-c"指定了配置文件的路径,如果没有"-c",nginx默认会加载其安装目录的conf子目录中的nginx.conf文件。所以默认情况下可以不加"-c"启动。

停止:
通过ps -ef|grep nginx查看nginx的进程。在进程列表里面找master进程,它的编号就是主进程号了。
(1)从容停止Nginx:
kill -QUIT 主进程号
kill -QUIT /usr/local/nginx/logs/nginx.pid
(2)快速停止Nginx:
kill -TERM 主进程号
kill -TERM /usr/local/nginx/logs/nginx.pid
(3)强制停止Nginx:
pkill -9 nginx

Nginx的configure脚本支持选项说明:
./configure --help

–prefix=<path> - 安装路径,如果没有指定,默认为/usr/local/nginx。

–sbin-path=<path> - nginx可执行命令的文件,如果没有指定,默认为<prefix>/sbin/nginx。

–conf-path=<path> - 在没有使用-c参数指定的情况下nginx.conf的默认位置,如果没有指定,默认为<prefix>/conf/nginx.conf。

–pid-path=<path> - nginx.pid的路径,如果没有在nginx.conf中通过“pid”指令指定,默认为<prefix>/logs/nginx.pid。

–lock-path=<path> - nginx.lock文件路径,如果没有指定,默认为<prefix>/logs/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”。

–builddir=DIR - 设置构建目录。

–with-rtsig_module - 启用rtsig模块。

–with-select_module –without-select_module - 如果在configure的时候没有发现kqueue, epoll, rtsig或/dev/poll其中之一,select模块始终为启用状态。

–with-poll_module –without-poll_module - 如果在configure的时候没有发现kqueue, epoll, rtsig或/dev/poll其中之一,poll模块始终为启用状态。

–with-http_ssl_module - 启用ngx_http_ssl_module,启用SSL支持并且能够处理HTTPS请求。需要OpenSSL,在Debian系统中,对应的包为libssl-dev。

–with-http_realip_module - 启用ngx_http_realip_module

–with-http_addition_module - 启用ngx_http_addition_module

–with-http_sub_module - 启用ngx_http_sub_module

–with-http_dav_module - 启用ngx_http_dav_module

–with-http_flv_module - 启用ngx_http_flv_module

–with-http_stub_status_module - 启用”server status”(服务状态)页

–without-http_charset_module - 禁用ngx_http_charset_module

–without-http_gzip_module - 禁用ngx_http_gzip_module,如果启用,需要zlib包。

–without-http_ssi_module - 禁用ngx_http_ssi_module

–without-http_userid_module - 禁用ngx_http_userid_module

–without-http_access_module - 禁用ngx_http_access_module

–without-http_auth_basic_module - 禁用ngx_http_auth_basic_module

–without-http_autoindex_module - 禁用ngx_http_autoindex_module

–without-http_geo_module - 禁用ngx_http_geo_module

–without-http_map_module - 禁用ngx_http_map_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_limit_zone_module - 禁用ngx_http_limit_zone_module

–without-http_empty_gif_module - 禁用ngx_http_empty_gif_module

–without-http_browser_module - 禁用ngx_http_browser_module

–without-http_upstream_ip_hash_module - 禁用ngx_http_upstream_ip_hash_module

–with-http_perl_module - 启用ngx_http_perl_module

–with-perl_modules_path=PATH - 为perl模块设置路径

–with-perl=PATH - 为perl库设置路径

–http-client-body-temp-path=PATH - 为http连接的请求实体临时文件设置路径,如果没有指定,默认为<prefix>/client_body_temp

–http-proxy-temp-path=PATH - 为http代理临时文件设置路径,如果没有指定,默认为<prefix>/proxy_temp

–http-fastcgi-temp-path=PATH - 为http fastcgi临时文件设置路径,如果没有指定,默认为<prefix>/fastcgi_temp

–without-http - 禁用HTTP服务

–with-mail - 启用IMAP4/POP3/SMTP代理模块

–with-mail_ssl_module - 启用ngx_mail_ssl_module

–with-cc=PATH - 设置C编译器路径

–with-cpp=PATH - 设置C预处理器路径

–with-cc-opt=OPTIONS - 变量CFLAGS中附加的参数,用于FreeBSD中的PCRE库,同样需要指定–with-cc-opt=”-I /usr/local/include”,如果我们使用select()函数则需要同时增加文件描述符数量,可以通过–with-cc-opt=”-D FD_SETSIZE=2048”指定。

–with-ld-opt=OPTIONS - 通过连接器的附加参数,用于FreeBSD中的PCRE库,同样需要指定–with-ld-opt=”-L /usr/local/lib”。

–with-cpu-opt=CPU - 指定编译的CPU,可用的值为: pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

–without-pcre - 禁用PCRE库文件,同时将禁用HTTP rewrite 模块,如果要在”location”指令中使用正则表达式,同样需要PCRE库。

–with-pcre=DIR - 设置PCRE库源文件路径。

–with-pcre-opt=OPTIONS - 在编译时为PCRE设置附加参数。

–with-md5=DIR - 设置md5库源文件路径。

–with-md5-opt=OPTIONS - 在编译时为md5设置附加参数。

–with-md5-asm - 使用md5汇编源。

–with-sha1=DIR - 设置sha1库源文件路径。

–with-sha1-opt=OPTIONS - 在编译时为sha1设置附加参数。

–with-sha1-asm - 使用sha1汇编源。

–with-zlib=DIR - 设置zlib库源文件路径。

–with-zlib-opt=OPTIONS - 在编译时为zlib设置附加参数。

–with-zlib-asm=CPU - 为指定的CPU使用zlib汇编源进行优化,可用值为: pentium, pentiumpro。

–with-openssl=DIR - 设置openssl库源文件路径。

–with-openssl-opt=OPTIONS - 在编译时为openssl设置附加参数。

–with-debug - 启用debug记录。

–add-module=PATH - 增加一个在PATH中的第三方模块。

安装依赖:

pcre: http://www.pcre.org/
zlib: http://iweb.dl.sourceforge.net/project/libpng/zlib/1.2.5/zlib-1.2.5.tar.gz
sudo apt-get install libssl-dev

# groupadd -r nginx
# useradd -r -g nginx -s /bin/false -M nginx 建立nginx用户和nginx组

开始编译:

./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

make
make install
给nginx脚本
vim /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

给权限:
chmod +x /etc/rc.d/init.d/nginx
添加到服务列表:
chkconfig --add nginx
服务自动启动:
chkconfig nginx on

 

location
=表示精确匹配,所请求的必须是一个特定文件
~表示做正则表达式匹配 ,
^~不使用正则表达式,只做字符匹配
@转向其他代理服务器
=优先级最高
^~次之

访问日志:
access_log  /var/log/nginx/access.log  main;

监听端口
形式如下:
listen 127.0.0.1:8000;
listen 127.0.0.1;
listen 8000;
listen *:8000;
listen localhost:8000;

alias 别名
在server中
例如:
location /bbs {
         alias /www/bbs;
}
虚拟主机:
如:
 server {
      listen   80;
      server_name www.magedu.com;
      root /www/magedu;
      index index.html;
}
      server {
      listen   80;
      server_name dz.magedu.com;
      root /www/discuz;
      index index.html;
}
      server {
      listen  80 default;
      server_name _;
      root /www/default;
      index index.html;
      location /status {
         stub_status on;
         access_log off;
}

定义默认虚拟主机:
server {
      listen  80 default;
      server_name _;
      root /www/default;
      index index.html;

}

开启nginx状态监控的功能:
location /status {
         stub_status on;
         access_log off;
}

启用基于用户的认证:
 auth_basic "Restricted";
         auth_basic_user_file /etc/nginx/.htpasswd;
如:
 server {
      listen  80 default;
      server_name _;
      root /www/default;
      index index.html;
      location /status {
         stub_status on;
         access_log off;
         auth_basic "Restricted";
         auth_basic_user_file /etc/nginx/.htpasswd;
}
}

访问控制:
如:
location / {
  deny    192.168.1.1;
  allow   192.168.1.0/24;
  allow   10.1.0.0/16;
  deny    all;
}

最后一个yum直接安装nginx方法su -c 'yum -y install nginx'
EL5 based systems

su -c 'rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm'
...
su -c 'yum -y install nginx'

EL4 based systems

su -c 'rpm -Uvh http://download.fedora.redhat.com/pub/epel/4/i386/epel-release-4-9.noarch.rpm'
...
su -c 'yum -y install nginx'
如果编译安装成功

cd cd /usr/local/nginx/sbin

./nginx

却不能启动nginx提示

./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
将libpcre.so.1从/usr/local/lib拷贝或链接到/usr/lib下

还不行cd /lib

然后ln -s libpcre.so.0.0.1 libpcre.so.1
应该就可以了!

标签: Linux安装nginx详解

发表评论:

Powered by emlog sitemap