安装Nginx(From Source Code)

从源代码编译Nginx
本文介绍如何从源代码编译安装Nginx Web服务器。步骤包括安装必要的依赖包、下载Nginx源代码、配置编译选项、编译及安装Nginx等。此外还提供了一个用于管理Nginx服务的init脚本。

Setting up Pre-requisites to compile from source

yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel
# mkdir -p src && cd src    

Compiling the Nginx source

Downloading the source code

nginxVersion="1.5.5"
wget http://nginx.org/download/nginx-$nginxVersion.tar.gz
tar -xzf nginx-$nginxVersion.tar.gz 
ln -sf nginx-$nginxVersion nginxwget http://nginx.org/download/nginx-

Preparing the nginx source

cd nginx

./configure \       
--user=nginx                          \
--group=nginx                         \
--prefix=/etc/nginx                   \
--sbin-path=/usr/sbin/nginx           \
--conf-path=/etc/nginx/nginx.conf     \
--pid-path=/var/run/nginx.pid         \
--lock-path=/var/run/nginx.lock       \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module        \
--with-http_stub_status_module        \
--with-http_ssl_module                \
--with-pcre                           \
--with-file-aio                       \
--with-http_realip_module             \
--without-http_scgi_module            \
--without-http_uwsgi_module           \

Compiling the nginx source

make
make install

Running the VPS

1 useradd -r nginx
2 setup the file /etc/init.d/nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# 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
# pidfile:     /var/run/nginx.pid
# user:        nginx

# 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"

lockfile=/var/run/nginx.lock

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
    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

3 Set the service to start whenever the system boots:

chkconfig --add nginx
chkconfig --level 345 nginx on

4 adjust /etc/nginx/nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    # add the below 2 lines under http around line 20
    types_hash_bucket_size 64;
    server_names_hash_bucket_size 128;

5 start the service

service nginx start
<think>好的,我现在需要帮助用户解决在CentOS下源码编译安装Nginx时遇到的缺少PCRE库导致的错误。根据用户提供的引用,特别是引用[1]和引用[2],解决方案应该是安装PCRE开发库。 首先,用户的问题明确指出是在CentOS系统下,所以需要对应CentOS的安装命令。引用[1]中提到在CentOS/RHEL系统上使用sudo yum install pcre-devel,而引用[2]中也提到安装pcre库的步骤。这说明用户需要执行yum命令来安装pcre-devel包。 接下来,用户可能已经运行了Nginx的configure脚本,但提示缺少PCRE库。安装完pcre-devel后,可能需要重新运行configure命令,并确保指定PCRE的路径,尤其是如果PCRE不是通过包管理器安装的情况。例如,使用--with-pcre=选项指定路径,但大多数情况下,系统默认安装的位置会被自动检测到,所以可能不需要额外参数。 另外,引用[3]和引用[4]提到了OpenSSL相关的错误,但用户当前的问题是关于PCRE的,所以暂时不需要处理SSL的问题。不过,可能需要提醒用户如果后续遇到其他依赖问题,比如OpenSSL,可以参考这些引用中的解决方法。 用户可能还想知道如何验证安装是否成功,以及后续的配置步骤。比如启动Nginx、设置开机启动等,这些虽然不是直接解决PCRE错误,但可以作为补充信息帮助用户完成整个安装过程。 总结步骤应该是:1. 安装pcre-devel;2. 重新配置和编译Nginx;3. 验证安装。同时需要给出具体的命令示例,确保用户能够复制粘贴执行。此外,可能还需要提醒用户如果PCRE是通过源码安装的,configure时需要指定路径,但通常情况下使用yum安装的库会被自动找到。 最后,生成相关问题时要围绕PCRE的作用、其他常见依赖错误、验证安装的方法以及优化Nginx编译的选项,这样可以覆盖用户可能遇到的后续问题。</think>### 解决方案:解决CentOS下Nginx源码编译缺少PCRE库的错误 #### 1. 安装PCRE开发库 **PCRE库是Nginx HTTP重写模块(rewrite module)的核心依赖**。在CentOS系统中,需通过以下命令安装开发包: ```bash sudo yum install pcre-devel ``` 此命令会安装PCRE的头文件和静态库,支持Nginx在编译时正确调用正则表达式功能[^1][^2]。 #### 2. 重新配置Nginx编译环境 安装完成后,返回Nginx源码目录,重新运行`configure`脚本: ```bash cd /path/to/nginx-source-code ./configure --with-http_ssl_module # 若需要SSL功能可保留此参数 ``` 若PCRE库未安装到默认路径,需通过`--with-pcre=`指定自定义路径: ```bash ./configure --with-pcre=/usr/local/pcre ``` #### 3. 编译并安装Nginx ```bash make && sudo make install ``` #### 4. 验证安装 检查Nginx是否包含`http_rewrite_module`: ```bash nginx -V 2>&1 | grep -o http_rewrite_module ``` 若输出`http_rewrite_module`,则表明依赖已解决[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值