安装方式

  1. 下载开源文件手动编译安装:
    2. nginx: download
    3. nginx安装及其配置详细教程 - 知乎 (zhihu.com)

  2. 使用 yum 源安装

手动安装

mkdir -p /usr/local/nginx/logs
mkdir -p /usr/local/nginx/lock
mkdir -p /usr/local/nginx/client
mkdir -p /usr/local/nginx/proxy
mkdir -p /usr/local/nginx/fastcgi
mkdir -p /usr/local/nginx/uwsgi
mkdir -p /usr/local/nginx/scgi



./configure \
--prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/lock/nginx.lock \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--http-client-body-temp-path=/usr/local/nginx/client \
--http-proxy-temp-path=/usr/local/nginx/proxy \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi \
--http-scgi-temp-path=/usr/local/nginx/scgi \
--with-stream \
--with-file-aio \
--with-http_v2_module \
--with-http_dav_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_stub_status_module \
--with-http_gzip_static_module


make & make install

手动安装自定义配置 Nginx 服务

在目录 /lib/systemd/system 下创建 nginx.service 文件

[Unit]                                                                               
Description=nginx - web server                                                      
After=network.target remote-fs.target nss-lookup.target

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

[Install]                                                                            
WantedBy=multi-user.target

对应命令

# 开机自动启动
systemctl enable nginx.service
# 启动redis服务
systemctl start nginx.service
# 查看服务状态
systemctl status nginx.service
# 停止服务
systemctl stop nginx.service
# 取消开机自动启动(卸载服务)
systemctl disabled nginx.service
# 重新加载配置文件
systemctl reload nginx.service

==注意:==

image-20230813232822217|500

Nginx常用命令

# 立即停止
nginx -s stop
# 执行完当前请求再停止
nginx -s quit
# 重新加载配置文件,相当于restart
nginx -s reload
# 将日志写入一个新的文件
nginx -s reopen
# 测试配置文件
nginx -t

日志

Yum 安装

  • /var/log/nginx

手动安装

  • [安装目录]/logs
    • 比如:/usr/local/nginx/logs/

配置文件

配置文件位于 /usr/local/nginx/conf/nginx.conf, 下列命令会引用 /usr/local/nginx/conf.d 目录下所有的 .conf 文件,这样可以保持主配置文件的简洁,同时配个多个 .conf 文件方便区分,增加可读性。

    include       /usr/local/nginx/conf.d/*.conf

image-20230813232317364

使用 VSCode 编辑 Nginx 配置文件

下载插件

远程连接插件
image-20230813225236251

image-20230813225250656

Nginx 配置文件高亮插件
image-20230813225303702

创建远程连接配置文件

image-20230813225400994

连接到服务器

image-20230813225421757

image-20230813225440003

image-20230813225453807

然后就会打开一个新的 VSCode 窗口,注意需要选择连接的操作系统类型,并输入密码

最后这个界面就是连接的远程服务器了,点击打开文件夹就可以打开服务器上的文件夹了(==注意:==此时可能还会让你输入密码)
image-20230813225722651

打开文件夹后的界面

image-20230813231136613