> 文档中心 > Nginx安装、配置

Nginx安装、配置

目录

  • 前言
  • 1、安装依赖库
  • 2、下载、编译、配置nginx
    • 2.1、下载
    • 2.2、解压文件
    • 2.3、源码编译
    • 2.4、源码安装
    • 2.5、配置文件软连接
  • 3、启动/停止nginx
  • 4、测试访问
  • 5、配置自启

前言

本文讲解nginx,源码安装,Docker安装见:《Docker部署安装Nginx》

1、安装依赖库

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

2、下载、编译、配置nginx

注意:以下操作目录为 /usr/local

2.1、下载

下载地址:http://nginx.org/en/download.html,下载后传到服务器中;
在这里插入图片描述
或者通过wget直接下载

wget http://nginx.org/download/nginx-1.20.2.tar.gz 

2.2、解压文件

tar -zxvf  nginx-1.20.2.tar.gz

2.3、源码编译

进入解压目录:

cd nginx-1.20.2

基本编译:

./configure

在这里插入图片描述
自定义编译:

./configure \--prefix=/usr/local/nginx \ 指向安装目录--sbin-path=/usr/local/nginx/sbin/nginx \指向(执行)程序文件(nginx)--conf-path=/usr/local/nginx/conf/nginx.conf \指向配置文件--error-log-path=/usr/local/nginx/logs/error.log \指向log--http-log-path=/usr/local/nginx/logs/access.log \     指向http-log# 如果指定了用户启动,需要加入用户,命令:useradd -s /sbin/nologin -M nginx--user=nginx \   --group=nginx \--with-http_ssl_module \ 启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)--with-http_flv_module \  启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)--with-http_stub_status_module \     启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)--with-http_gzip_static_module \   启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)--http-client-body-temp-path=/var/tmp/nginx/client/ \ 设定http客户端请求临时文件路径--http-proxy-temp-path=/usr/local/nginx/proxy_temp \ 设定http代理临时文件路径--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp/ \ 设定http fastcgi临时文件路径--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \ 设定http uwsgi临时文件路径--http-scgi-temp-path=/usr/local/nginx/scgi_temp \ 设定http scgi临时文件路径--with-pcre \ 启用pcre库--add-module= #第三方模块路径

2.4、源码安装

make && make install

在这里插入图片描述

注意:如果采用的是默认'基本编译(./configure)那么安装后nginx目录和解压的nginx-1.20.2包同级'

在这里插入图片描述

2.5、配置文件软连接

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

3、启动/停止nginx

操作目录:/usr/local/nginx/sbin

cd /usr/local/nginx/sbin# 启动:./nginx# 关闭:./nginx -s stop# 重启:./nginx -s reload

4、测试访问

在这里插入图片描述

5、配置自启

进入系统目录:

cd /usr/lib/systemd/system/

编写nginx.service:

vim nginx.service

内容如下:

Description=nginx - high performance web serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stop[Install]WantedBy=multi-user.target

ExecStart、ExecReload、ExecStop需要指定到nginx的实际安装目录

执行命令:

systemctl daemon-reload# 开启自启systemctl enable nginx.service# 启动、重启、停止systemctl start|reload|stop nginx