下载路径 nginx: download 下载Stable version(稳定版本)
安装依赖
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
如果安装的nginx无法使用外网源和内网源 可以使用
yum -y install --downloadonly --downloaddir=/aa gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
随后把rpm 包下载下来并上传执行 rpm -ivh ./* --nodeps --force
安装nginx
tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
./configure \
--prefix=/opt/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \ #启用文件修改支持
--with-http_stub_status_module \ #启用状态统计
--with-http_gzip_static_module \ #启用 gzip静态压缩
--with-http_flv_module \ #启用 flv模块,提供对 flv 视频的伪流支持
--with-http_ssl_module #启用 SSL模块,提供SSL加密功能
make && make install
echo $?
设置nginx 安装目录用户
Useradd -s /sbin/nologin nginx
Chown -R nginx:nginx /opt/nginx
优化
Vim conf/nginx.conf
worker_processes 1; # 设置cpu的核数也可以设置为worker_processes auto;根据cpu核数自动设置
events {
worker_rlimit_nofile 65536; # 针对Nginx进程
worker_connections 1024; #(worker_connections 是不能超过
worker_rlimit_nofile的)
}
nginx 有一个 master,有四个 woker,每个 woker 支持最大的连接数 1024,支持的最大并发数是多少?
普通的静态访问最大并发数是: worker_connections * worker_processes /2,
而如果是 HTTP 作 为反向代理来说,最大并发数量应该是 worker_connections *worker_processes/4。
vim /etc/security/limits.conf #在配置文件最后添加
* soft nofile 65536
* hard nofile 65536
vim /etc/sysctl.conf
net.ipv4.tcp_tw_reuse = 1 # 让占用的端口复用
net.ipv4.tcp_timestamps = 1 # 时间戳
设置开机自启
首先,在系统的/lib/systemd/system/目录下创建 nginx.service文件:
vi /lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -t
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx -s reload
ExecStop=/opt/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl daemon-reload #重新加载某个服务的配置文件
重启服务器后nginx 开机自启