Skip to content

安装

官网下载

源码安装

前置环境

sudo yum install openssl openssl-devel pcre pcre-devel zlib zlib-devel gcc gcc-c++

gcc: 一个开源的编译器集合,用于处理各种各样的语言,其中就包含了C语言

pcre:正则表达式库

zlib:压缩算法,gzip压缩

openssl:开放源代码的ssl软件库包

解压

tar -zxvf nginx-1.20.2.tar.gz

进入nginx-1.20.2

cd nginx-1.20.2

编译

./configure // 默认编译在/usr/local/nginx
./configure --prefix=/etc/nginx --with-http_ssl_module

#后面加上了nginx_limit_speed_module 模块
./configure --prefix=/etc/nginx --with-http_ssl_module --add-module=/home/nginx_limit_speed_module

这种方式和简单的安装配置不同的地方在第一步,通过./configure来对编译参数进行设置,需要我们手动来指定。那么都有哪些参数可以进行设置,接下来我们进行一个详细的说明。

PATH:是和路径相关的配置信息

with:是启动模块,默认是关闭的

without:是关闭模块,默认是开启的

我们先来认识一些简单的路径配置已经通过这些配置来完成一个简单的编译:

--prefix=PATH

指向Nginx的安装目录,默认值为/usr/local/nginx

--sbin-path=PATH

指向(执行)程序文件(nginx)的路径,默认值为<prefix>/sbin/nginx

--modules-path=PATH

指向Nginx动态模块安装目录,默认值为<prefix>/modules

--conf-path=PATH

指向配置文件(nginx.conf)的路径,默认值为<prefix>/conf/nginx.conf

--error-log-path=PATH

指向错误日志文件的路径,默认值为<prefix>/logs/error.log

--http-log-path=PATH

指向访问日志文件的路径,默认值为<prefix>/logs/access.log

--pid-path=PATH

指向Nginx启动后进行ID的文件路径,默认值为<prefix>/logs/nginx.pid

--lock-path=PATH

指向Nginx锁文件的存放路径,默认值为<prefix>/logs/nginx.lock

要想使用可以通过如下命令

./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--modules-path=/usr/local/nginx/modules \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/logs/nginx.lock

安装

make & make install

命令

sbin下的nginx命令默认是指定/etc/nginx/conf/nginx.conf,我的nginx.conf路径不同需要单独指定

#启动
./nginx -c /etc/nginx/nginx.conf
#重载
./nginx -s reload
./nginx -c /etc/nginx/nginx.conf -s reload
#杀掉nginx
./nginx -s quit 
./nginx -c /etc/nginx/nginx.conf -s quit
#测试
nginx -t -c /etc/nginx/nginx.conf

系统服务

vi /usr/lib/systemd/system/nginx.service

脚本内容

[Unit]
Description=Nginx HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/etc/nginx/nginx.pid
ExecStartPre=/etc/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/etc/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/etc/nginx/sbin/nginx -s reload -c /etc/nginx/nginx.conf
ExecStop=/etc/nginx/sbin/nginx -s stop
ExecQuit=/etc/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重新加载 systemd 配置文件

systemctl daemon-reload

设置开机自启动

systemctl start nginx  // 开启
systemctl stop nginx // 停止
systemctl restart nginx // 重启
systemctl reload nginx // 重新加载配置文件
systemctl status nginx // 状态

systemctl enable nginx // 开机启动
systemctl disable nginx // 关闭开机启动

系统环境

修改/etc/profile文件

在最后一行添加
export PATH=$PATH:/usr/local/nginx/sbin

生效

source /etc/profile

yum安装

安装

sudo yum update -y // 更新 yum list | grep nginx,查看安装版本

sudo yum install openssl openssl-devel pcre pcre-devel zlib zlib-devel gcc gcc-c++ // 所需扩展

sudo yum install nginx -y // 安装,nginx -v 查看版本

sudo systemctl start nginx // 启动

sudo systemctl enable nginx // 开机自启

sudo systemctl disable nginx // 开机自启

错误

/configure: error: C compiler cc is not found

yum -y install gcc gcc-c++ autoconf automake make

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

sudo yum install pcre 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= option.

sudo yum install zlib-devel

./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl= option

sudo yum install openssl-devel

nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /etc/nginx/cert/cert.conf:1

sudo yum install openssl openssl-devel pcre pcre-devel zlib zlib-devel gcc gcc-c++