1、Nginx下载(可更换其他版本)
wget http://nginx.org/download/nginx-1.19.10.tar.gz
2、查看gcc版本
gcc -v
3、如果没有gcc,则安装
yum install -y gcc
4、安装nginx所需要的依赖
yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel
5、解压nginx安装包
tar -zxvf nginx-1.19.10.tar.gz
6、进入安装包目录
cd nginx-1.19.10
7、配置
./configure --prefix=/usr/tools/nginx \
--pid-path=/usr/tools/nginx/log/nginx.pid \
--lock-path=/usr/tools/nginx/log/nginx.lock \
--error-log-path=/usr/tools/nginx/log/error.log \
--http-log-path=/usr/tools/nginx/log/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--without-http-cache \
--with-http_ssl_module \
--with-http_realip_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
8、编译并安装
make && make install
9、测试是否安装成功
/usr/tools/nginx/sbin/nginx -t
10、创建nginx所需要的目录
mkdir -p /var/temp/nginx
11、修改nginx.conf配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
include conf.d/*.conf;
client_max_body_size 5M;
sendfile on;
keepalive_timeout 65;
}
12、配置nginx子配置文件
server {
listen 443 ssl;
server_name ${域名 || 地址};
ssl_certificate /usr/tools/nginx/cert/证书.pem;
ssl_certificate_key /usr/tools/nginx/cert/证书.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
root /home/web/;
location / {
root /home/web/test;
index index.html index.htm;
try_files $uri $uri/ @resume-router;
}
location @resume-router {
rewrite ^.*$ /index.html last;
}
error_page 403 404 500 502 503 504 /error.html;
location = /error.html {
root html/error;
}
}
server {
listen 80;
server_name ${域名 || 地址};
return 301 https://${域名 || 地址};
}
13、配置开机自启
cd /usr/lib/systemd/system/
14、编写nginx启动项配置
vim nginx.service
15、修改配置文件
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/tools/nginx/sbin/nginx -c /usr/tools/nginx/conf/nginx.conf
ExecReload=/usr/tools/nginx/sbin/nginx -s reload -c /usr/tools/nginx/conf/nginx.conf
ExecStop=/usr/tools/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
16、设置开机自启动
systemctl enable nginx
17、基本用法
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx