linux安装nginx

背景

操作系统:centOS

1. 安装 GCC

yum install gcc-c++ -y

可使用命令查看安装的 gcc 版本

 2. PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。

yum install -y pcre pcre-devel

3. zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

4. OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

5. 安装 nginx

去官网下载 .tar.gz 安装包,地址:https://nginx.org/en/download.html

我用的是 nginx-1.18.0.tar.gz,使用命令解压

tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0

进行配置,可以使用默认配置,但是建议使用以下自定义配置

默认配置:

./configure

自定义配置:

./configure --prefix=/usr/local/nginx  
--conf-path=/usr/local/nginx/nginx.conf 
--error-log-path=/usr/local/nginx/nginxlog/error.log 
--http-log-path=/usr/local/nginx/nginxlog/access.log 
--pid-path=/usr/local/nginx/pids/nginx.pid 
--lock-path=/usr/local/nginx/locks/nginx.lock 
--with-http_ssl_module 
--with-http_stub_status_module  
--with-http_gzip_static_module  
--http-client-body-temp-path=/usr/local/nginx/tmp/client  
--http-proxy-temp-path=/usr/local/nginx/tmp/proxy  
--http-fastcgi-temp-path=/usr/local/nginx/tmp/fastcgi  
--http-uwsgi-temp-path=/usr/local/nginx/tmp/uwsgi  
--http-scgi-temp-path=/usr/local/nginx/tmp/scgi

自定义配置中涉及到的临时文件目录需要手动创建

mkdir -p /usr/local/nginx/tmp

编译安装

make
make install

查找安装路径

[root@gpap-prod-2 nginx-1.18.0]# whereis nginx
nginx: /usr/local/nginx

启动、停止nginx

cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload

查询nginx进程

ps -ef | grep nginx

6. 设置nginx为开机自启动(在rc.local中增加启动代码即可)

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx

设置执行权限

chmod 755 rc.local

rc.local文件内容如下

[root@gpap-prod-2 nginx-1.18.0]# cat /etc/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.


touch /var/lock/subsys/local
/usr/local/nginx/sbin/nginx
原文地址:https://www.cnblogs.com/miaoying/p/15134231.html