CentOS 7 安装 nginx-1.3.15.tar.gz 详细步骤(从源码编译到启动配置)

0 阅读2分钟

一、准备编译环境

Nginx 1.3.15 是老版本,需要从源码编译,得先装好编译工具和依赖库。直接一条命令搞定:

sudo yum install gcc make pcre-devel zlib-devel openssl-devel -y

  • gcc:C 语言编译器,必须装。
  • make:编译工具,用来执行 Makefile。
  • pcre-devel:支持正则表达式,Nginx 的 rewrite 模块要用。
  • zlib-devel:支持 gzip 压缩。
  • openssl-devel:支持 HTTPS,如果不需要 HTTPS 可以不装,但建议装上。

二、下载并解压 Nginx 包

安装包下载:pan.quark.cn/s/4fcdf033f…

下载完解压:

tar -zxvf nginx-1.3.15.tar.gz

解压后会多一个 nginx-1.3.15文件夹,进去:

cd nginx-1.3.15

三、配置编译参数

在 nginx-1.3.15目录下执行 configure命令,设置安装路径和功能模块:

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre --with-zlib

  • --prefix=/usr/local/nginx:指定安装目录,可改。
  • --with-http_ssl_module:启用 HTTPS 支持(需要 openssl-devel)。
  • --with-pcre:启用 PCRE 支持(需要 pcre-devel)。
  • --with-zlib:启用 zlib 支持(需要 zlib-devel)。

配置过程中如果提示缺依赖,就按提示把缺的包装上,再重新执行 ./configure

四、编译并安装

配置成功后,执行编译:

make

编译完成后,安装到指定目录:

sudo make install

安装完会在 /usr/local/nginx下生成 sbinconfhtml等目录。

五、启动 Nginx

进入安装目录的 sbin文件夹:

cd /usr/local/nginx/sbin

执行启动命令:

sudo ./nginx

启动后可以用 ps命令看看进程在不在:

ps -ef | grep nginx

能看到 nginx: master process和 nginx: worker process就说明启动成功了。

六、测试访问

Nginx 默认监听 80 端口,打开浏览器访问:

http://服务器IP

如果是本机就访问 http://localhost,能看到 Nginx 的欢迎页面(“Welcome to nginx!”)就成功了。

七、常用命令

  • 停止 Nginx:

    sudo ./nginx -s stop
    

  • 重载配置(改了 conf 文件后用):

    sudo ./nginx -s reload
    

  • 检查配置文件语法:

    sudo ./nginx -t
    

八、常见问题

  1. 80 端口被占用

    用 netstat -tlnp | grep 80找到占用进程杀掉,或者改 conf/nginx.conf里的 listen端口。

  2. 启动时报 “error while loading shared libraries: libpcre.so.1”

    可能是 pcre 库路径没找到,执行 sudo ldconfig刷新动态链接库缓存。

  3. 远程访问不了

    检查防火墙有没有开 80 端口:

    sudo firewall-cmd --add-port=80/tcp --permanent
    sudo firewall-cmd --reload
    

这样就完成了 Nginx 1.3.15 的安装和启动,适合老项目或者需要特定版本的场景。