1.安装包准备
nginx官网https://nginx.org/en/download.html下载压缩包,一般下载最新稳定版本即可。
2.安装依赖
yum install -y gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
3.安装包解压到喜欢的目录
[root@VM-0-3-centos ~]# tar -zxvf /yzmeta/tool/nginx-1.24.0.tar.gz -C /etc
4.执行配置指令
注:--prefix=/etc/nginx 意思是将安装在/etc/nginx目录,需要事先通过命令创建nginx目录(sudo mkdir nginx)
[root@VM-0-3-centos ~]# cd /etc/nginx-1.24.0
[root@VM-0-3-centos nginx-1.24.0]# sudo ./configure --prefix=/etc/nginx --with-http_ssl_module --with-stream --with-http_stub_status_module --with-http_gzip_static_module --with-http_v2_module
# 配置configure --prefix 代表安装的路径;
--with-http_ssl_module 安装ssl;
--with-http_stub_status_module查看nginx的客户端状态;
5.执行安装指令
[root@VM-0-3-centos nginx-1.24.0]# sudo make & make install
6.创建nginx用户组
注:nginx-1.24.0目录只是解压缩包所在路径,实际安装配置路径在/etc/nginx目录下。
创建nginx用户组(为了安全添加nginx用户和组,以便以后以nginx用户的身份运行nginx程序,而非root)进入到nginx中。
[root@VM-0-3-centos nginx-1.24.0]# cd /etc/nginx
[root@VM-0-3-centos nginx]# groupadd -r nginx
[root@VM-0-3-centos nginx]# useradd -r -g nginx nginx
或[root@VM-0-3-centos nginx]# useradd nginx -s /sbin/nologin
7.配置nginx.conf配置文件
user nginx;
worker_processes 10; # 线程数,可以设置成auto自动,也可根据本机硬件线程数合理设置
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
#pid logs/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
server_tokens off;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include mime.types;
default_type application/octet-stream;
include /etc/nginx/conf/conf.d/*.conf;
server {
#listen 80 default_server;
#listen [::]:80 default_server;
#默认端口改成780
listen 780 default_server;
listen [::]:780 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/conf/default.d/*.conf;
return 301 https://$host$request_uri;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 7443 ssl http2 default_server;
listen [::]:7443 ssl http2 default_server;
#修改默认端口
#listen 7443 ssl;
#listen [::]:7443 ssl;
#server_name demo.hop.com;
server_name wx.hzfy.com.cn;
#server_name localhost;
root /usr/share/nginx/html;
client_max_body_size 500M;
#ssl_certificate 1_demo.hop.com_bundle.crt;
#ssl_certificate_key 2_demo.hop.com.key;
ssl_certificate wx.hzfy.com.cn.pem;
ssl_certificate_key wx.hzfy.com.cn.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers HIGH:!aNULL:!MD5;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
# # Load configuration files for the default server block.
include /etc/nginx/conf/default.d/*.conf;
location / {
try_files $uri $uri/ /index.html;
}
location /wx {
root /yzmeta/html/;
try_files $uri $uri/ @router;
index index.html;
}
###########################
location /haphone {
try_files $uri $uri/ @harouter;
index index.html;
}
location @harouter {
#root /yzmeta/html/;
rewrite ^.*$ /haphone/index.html last;
}
#location /haapi {
# proxy_pass http://localhost:9900/rdb;
#}
location /api {
proxy_pass http://localhost:9900/rdb;
}
location /rdb {
proxy_pass http://localhost:9900/rdb;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
8.nginx使用命令
(1)查看nginx服务及配置文件安装目录
[root@VM-0-3-centos ~]# find / -name nginx
(2)直接打开默认目录
[root@VM-0-3-centos ~]# cd /etc/nginx
[root@VM-0-3-centos nginx]# ll
(3)输入nginx,开启服务
[root@VM-0-3-centos ~]# cd /etc/nginx/sbin #进入nginx启动文件路径
[root@VM-0-3-centos ~]# sudo ./nginx #启动nginx服务
(4)查询进程
ps -aux|grep nginx
(5)查询nginx状态
nginx -t
(6)关闭nginx
nginx -s stop
(7)重启
nginx -s reload