Nginx 安装说明(安装包)
1、下载安装 Nginx
1.1、安装依赖
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel expat-devel
# 安装 gcc
yum install gcc-c++
# 安装 PCRE pcre-devel
yum install -y pcre pcre-devel
# 安装zlib
yum install -y zlib zlib-devel
# 安装Open SSL
yum install -y openssl openssl-devel1.2、下载最新的 Nginx 稳定版本( 1.28.0 版本).
官方地址:https://nginx.org/en/download.html

$ cd /usr/local/src && wget https://nginx.org/download/nginx-1.28.0.tar.gz --no-check-certificate
$ tar -xzf nginx-1.28.0.tar.gz && cd nginx-1.28.01.3、编译安装
#执行命令
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#执行make命令(要是执行不成功请检查最开始安装的四个有没有安装成功)
make
#执行make install命令
make install1.4、PCRE 编译安装
部分情况,Nginx 因为缺少 PCRE模块无法安装,且不能通过命令方式安装PCRE,则可以通过手动安装解决问题。
- PCRE 目前最新版本为 8.45,可以点这里进行下载。 https://sourceforge.net/projects/pcre/files/pcre/ ;
- 使用
tar -zxvf pcre-8.45.tar.gz进行解压。 - 运行
chmod -R 777 /pcre-8.45对当前文件夹授予全部读写权限。 - 切换到
/pcre-8.45目录下,运行./configure进行pcre初始化配置,会在控制台打印出一大堆的输出信息。 - 执行
make操作,进行编译。 - 运行
make install,进行安装,至此PCRE安装完成。
2、开机启动
创建 Nginx 开机启动 nginx.service
$ vi /lib/systemd/system/nginx.service脚本内容:
[Unit]
Description=Nginx - high performance web server
After=nginx.service
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
Execenable=/usr/local/nginx/sbin/nginx
[Install]
WantedBy=multi-user.target设置随机启动:
systemctl daemon-reload
# 设置开机启动
systemctl enable nginx
# 取消开机自启动
#systemctl disable nginx
# 查看服务当前状态
systemctl status nginx
# 启动nginx服务
systemctl start nginx
# 停止nginx服务
systemctl stop nginx
# 重启nginx服务
systemctl restart nginx3、优化 nginx.conf
创建文件目录
# 创建 Nginx 缓存目录
mkdir -p /usr/local/nginx/proxy_cache_dir;
# 创建 Nginx 代理配置目录
mkdir -p /usr/local/nginx/vhost执行下面命令,编辑 nginx.conf
vi /usr/local/nginx/conf/nginx.conf配置文件
#user nobody;
worker_processes auto; # 自动检测CPU核心数
worker_rlimit_nofile 655350; # 每个worker进程的最大文件描述符数量
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid nginx.pid;
events {
worker_connections 65535; # 每个worker进程的最大连接数
multi_accept on; # 一次接受所有新连接
use epoll; # 使用epoll事件模型
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user - $upstream_addr - $request_time/$upstream_response_time [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"';
#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 logs/access.log main;
# 连接优化
keepalive_timeout 65;
keepalive_requests 10000;
# 缓冲区优化
types_hash_max_size 4096;
variables_hash_max_size 4096;
variables_hash_bucket_size 128;
server_names_hash_max_size 2048;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
client_max_body_size 200m;
large_client_header_buffers 4 32k;
output_buffers 1 32k;
postpone_output 1460;
# 文件优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# Gzip压缩设置
gzip on;
gzip_min_length 100k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
# 限制每个IP的请求速率为10r/s
limit_conn_zone $binary_remote_addr zone=perip:10m;
# 限制连接数
limit_conn_zone $server_name zone=perserver:10m;
# include vhost
include /usr/local/nginx/vhost/*.conf;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}作者:Jeebiz 创建时间:2024-01-12 10:22
最后编辑:Jeebiz 更新时间:2025-11-04 17:26
最后编辑:Jeebiz 更新时间:2025-11-04 17:26