安装nginx
方法一、 直接下载.tar.gz安装包
https://nginx.org/en/download.html
方法二、使用wget命令下载(推荐)。确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装。
(注意:本示例使用方法一)
下载包
wget https://nginx.org/download/nginx-1.21.6.tar.gz
解压
tar xvf nginx-1.21.6.tar.gz
cd nginx-1.21.6
配置(带有https模块)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
编译和安装
编译:make
安装:make install
查看安装路径:whereis nginx
设置开机启动
编辑服务文件
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target
[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
PrivateTmp=true
[Install]
WantedBy=multi-user.target
说明:
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
保存退出
加入开机自启动
systemctl enable nginx.service
取消开机自启动
systemctl disable nginx.service
服务的启动/停止/刷新配置文件/查看状态
#启动nginx服务
systemctl start nginx.service
#停止服务
systemctl stop nginx.service
#重新启动服务
systemctl restart nginx.service
#查看所有已启动的服务
systemctl list-units --type=service
#查看服务当前状态
systemctl status nginx.service
#设置开机自启动
systemctl enable nginx.service
#停止开机自启动
systemctl disable nginx.service
nginx.conf https 配置
server {
listen 443 ssl;
server_name localhost;
ssl_certificate cert.pem;#根证书地址(默认把证书放在conf目录)
ssl_certificate_key cert.key;#证书秘钥(默认把证书放在conf目录)
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;
}
}
将 http 重定向 https
server {
listen 80;
server_name localhost;
#将请求转成https
rewrite ^(.*) https://$server_name$1 permanent;
}
查看nginx 进程命令
ps aux | grep nginx
html设置编码
html文件显示出来乱码,一般在文件头中设置网页编码即可,加入<meta charset="utf-8">
评论区