liunx配置 Nginx 从粘贴 到 运行

nginx arm 服务器 安装

1、 下载

下载地址

Mainline version # 抢先版
Stable version # 稳定版
Legacy versions # 历史版本

日志 1Liunx 版本win版本
CHANGES-1.28nginx-1.28.0 pgpnginx/Windows-1.28.0 pgp

2、 安装编译环境

sudo apt update
sudo apt install -y build-essential libpcre3-dev zlib1g-dev libssl-dev

#或
yum makecache
yum install -y gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel

3、 新建一个文件夹 nginx cd进入目录copy paste并解压

mkdir /usr/local/nginx           # 新建文件夹
cd /usr/local/nginx/              # 进入文件夹
# 将源码压缩包 复制到此,并解压缩
tar -zxvf nginx-1.24.0.tar.gz  # 解压源码包
# 再新建一个用于装 编译后的文件的文件夹
mkdir nginx
# 此时 /root/nginx/ 文件下下有   源码压缩包nginx-1.24.0.tar.gz  源码解压后的包nginx-1.24.0  空的文件夹nginx   
cd nginx-1.24.0                # 进入源码目录

4、配置编译选项

指定安装路径为 /usr/local/nginx/nginx,并启用常用模块(如 HTTPS 支持):

# prefix 指定的路径为 刚才新建的空的文件夹
./configure --prefix=/usr/local/nginx/nginx \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_realip_module

关键参数说明:

  • –prefix=/root/nginx/nginx:指定安装目录。
  • –with-http_ssl_module:启用 HTTPS 支持。
  • –with-http_v2_module:启用 HTTP/2 支持。

4.1 安装完成之后会出现这些东西,这只是 configure 脚本给出的“安装蓝图”——它告诉你编译完成后,nginx 的所有文件将被放到哪里。

Configuration summary

  + using system PCRE2 library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx/nginx"
  nginx binary file: "/usr/local/nginx/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/nginx/conf"
  nginx configuration file: "/usr/local/nginx/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

5、 编译并安装

cd /usr/local/nginx/nginx-1.24.0 # 进入你的源码目录
make          # 编译源码
make install  # 安装到指定目录

6、验证安装

/root/nginx/nginx/sbin/nginx -v  # 查看版本
# 出现这个就对 nginx version: nginx/1.24.0

/root/nginx/nginx/sbin/nginx -t  # 测试配置文件
# nginx: the configuration file /usr/local/nginx/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /usr/local/nginx/nginx/conf/nginx.conf test is successful

7、 添加到系统环境变量

# 先查看环境变量内容 
echo $PATH  #记住结果
# 添加到环境变量里 这里的路径为你的nginx 安装的sbin 路径
echo 'export PATH=$PATH:/usr/local/nginx/nginx/sbin/' >> ~/.bashrc
source ~/.bashrc

8、检查是否操作成功

echo $PATH 
# 对比之下之前执行的命令,如果多出了 //usr/local/nginx/nginx/sbin/  即可

which nginx 
# 出现路径地址即可

nginx -v 
# 出现版本号即可

9、定义启动命令

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

将下面内容复制到 nginx.service中,其中/usr/local/nginx/nginx/ 这段是nginx的安装目录,可根据4.1 出现的安装蓝图 对应填写

[Unit]
Description=The Nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

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

[Install]
WantedBy=multi-user.target

10、重新加载一下系统服务

systemctl daemon-reload

11、 现在可以直接运行以下命令使用nginx

# 操作命令

systemctl start nginx    # 启动 Nginx 服务
systemctl restart nginx  # 重启 Nginx 服务
systemctl reload nginx   # 重新加载配置(不重启服务)
systemctl stop nginx     # 停止 Nginx	服务 
systemctl status nginx   # 查看 Nginx 服务状态

systemctl enable nginx   # 设置开机 自启
systemctl disable nginx  # 禁止开机 自启

# 打开日志文件
tail -n  /usr/local/nginx/nginx/logs/error.log 
tail -n  /usr/local/nginx/nginx/logs/success.log	

12、配置nginx

# 一个基础的 Nginx 配置,根据需求改成自己项目的。

# 全局配置段
user  nginx;                     # 运行nginx的用户
worker_processes  auto;          # 自动根据CPU核心数设置工作进程数
error_log  /var/log/nginx/error.log warn;  # 错误日志路径和级别
pid        /var/run/nginx.pid;   # 进程ID文件位置

# 事件模块配置
events {
    worker_connections  1024;     # 每个工作进程的最大连接数
    use epoll;                   # 使用epoll高效模式(Linux)
    multi_accept on;             # 同时接受多个新连接
}

# HTTP模块配置
http {
    # 基础设置
    include       /etc/nginx/mime.types;  # MIME类型映射文件
    default_type  application/octet-stream; # 默认MIME类型
    
    # 日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log  /var/log/nginx/access.log  main; # 访问日志路径和格式
    
    # 性能调优
    sendfile        on;          # 启用高效文件传输模式
    tcp_nopush     on;          # 仅在sendfile为on时有效,优化数据包发送
    tcp_nodelay    on;          # 禁用Nagle算法,提高实时性
    keepalive_timeout  65;      # 保持连接的超时时间
    types_hash_max_size 2048;   # types哈希表大小
    
    # Gzip压缩配置
    gzip  on;                   # 启用gzip压缩
    gzip_min_length 1k;         # 最小压缩文件大小
    gzip_comp_level 2;          # 压缩级别(1-9)
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    
    # 包含其他配置文件
    include /etc/nginx/conf.d/*.conf;  # 包含其他配置文件
    
    # 示例server配置(也可以放在单独的conf.d文件中)
    server {
        listen       80;        # 监听端口
        server_name  localhost; # 域名
        
        # 静态文件服务配置
        # 如果webpack/vite中配置了 publicPath , location /`这里的路径地址要与 publicPath的值相同`
        location / {
            alias   /usr/share/nginx/html; # 网站根目录
            index  index.html index.htm;  # 默认索引文件
            try_files $uri $uri/ /index.html; # 尝试查找文件规则
        }
		# 后台接口代理
		location /api/ {
		    proxy_pass http://localhost/api/;
		    proxy_set_header Host $host;
		    proxy_set_header X-Real-IP $remote_addr;
		    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		    
		    # 超时设置
		    proxy_connect_timeout 60s;
		    proxy_read_timeout 60s;
		}
        
        # 错误页面配置
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
        
        # 禁止访问隐藏文件
        location ~ /\. {
            deny all;
        }
    }
    
    # 可以添加更多server块来配置虚拟主机
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值