网站首页 > 技术教程 正文
本文基于Centos 7下安装配置Nginx操作实践记录整理。
一、配置 EPEL源
sudo yum install -y epel-release sudo yum -y update
二、安装Nginx
sudo yum install -y nginx
安装成功后,默认的网站目录为: /usr/share/nginx/html
默认的配置文件为:/etc/nginx/nginx.conf
自定义配置文件目录为: /etc/nginx/conf.d/
三、开启端口80和443
如果你的服务器打开了防火墙,你需要运行下面的命令,打开80和443端口。
sudo firewall-cmd --permanent --zone=public --add-service=http sudo firewall-cmd --permanent --zone=public --add-service=https sudo firewall-cmd --reload
如果你的服务器是阿里云ECS,你还可以通过控制台安全组,打开80和443端口,或者其他自定义端口。
具体操作路径: 阿里云ECS服务器 -》 安全组 -》 配置规则 -》 安全组规则 -》 入方向 -》 添加安全组规则
端口范围: 比如你要打开80端口,这里就填写 80/80 。
优先级: 优先级可选范围为1-100,默认值为1,即最高优先级。
示例图如下:
{{< figure src="/img/aliyun-ecs-80.png" link="https://promotion.aliyun.com/ntms/act/qwbk.html?userCode=dbgo15cy" target="blank" >}}
{{< figure src="/img/aliyun-ecs-443.png" link="https://promotion.aliyun.com/ntms/act/qwbk.html?userCode=dbgo15cy" target="blank" >}}
四、操作Nginx
1.启动 Nginx
systemctl start nginx
2.停止Nginx
systemctl stop nginx
3.重启Nginx
systemctl restart nginx
4.查看Nginx状态
systemctl status nginx
5.启用开机启动Nginx
systemctl enable nginx
6.禁用开机启动Nginx
systemctl disable nginx
{{< figure src="/img/nginx-preview.png">}}
五、配置Nginx
1. 安装Https免费证书(以阿里云域名为例)
一键安装acme.sh
curl https://get.acme.sh | sh echo 'alias acme.sh=~/.acme.sh/acme.sh' >> ~/.bashrc source ~/.bashrc
生成Https证书
export Ali_Key="**********" export Ali_Secret="**********" acme.sh --issue --dns dns_ali -d domain
其中:
点击阿里云后台,右上角用户头像,菜单中选择accesskeys。
查看AccessKey ID 和 Access Key Secret,即对应上面的Ali_Key 和 Ali_Secret。
domain 对应该账户下购买的阿里云域名。
拷贝证书到nginx
mkdir -p /etc/nginx/ssl acme.sh --install-cert -d domain \ --key-file /etc/nginx/ssl/domain.key \ --fullchain-file /etc/nginx/ssl/domain.cer \ --reloadcmd "service nginx force-reload"
https证书拷贝成功。
目前https证书在 60 天以后会自动更新, 你无需任何操作. 今后有可能会缩短这个时间, 不过都是自动的, 你不用关心.
2. 配置nginx
删除/etc/nginx/nginx.conf中的server部分代码。
server { ... }
在/etc/nginx/conf.d 创建自定义配置文件default.conf
server { listen 80; listen 443 ssl; server_name domain www.domain; location / { root /usr/share/nginx/html; index index.html index.htm; } ssl on; ssl_certificate /etc/nginx/ssl/domain.crt; ssl_certificate_key /etc/nginx/ssl/domain.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4:!DH:!DHE; ssl_prefer_server_ciphers on; error_page 497 https://$host$uri?$args; }
其中:
root /usr/share/nginx/html; 表示网站文件目录,后面的分号不可省略。
ssl_certificate和ssl_certificate_key 指向https证书。
error_page 497 https://hosturi?$args; 这句的作用是,强制http跳转到https。
猜你喜欢
- 2024-10-14 Centos7常用命令总结(centos7.5命令大全)
- 2024-09-30 centos7使用nginx+uwsgi+daphne+supervisor+redis部署Django
- 2024-09-30 阿里云CentOS7.6下安装Nginx(阿里云搭建nginx)
- 2024-09-30 Centos7利用gz包安装Nginx(centos7.5安装gcc)
- 2024-09-30 CentOS7常用软件的安装(JDK+Tomcat+Nginx+Redis+MySQL)
- 2024-09-30 centos7系统下Nginx配置搭建(centos7 搭建部署conflunce)
- 2024-09-30 一文读懂服务器centos7.0安装指导指南(详细)
- 2024-09-30 为何还有人在百度centos7环境下安装nginx?
- 2024-09-30 CentOS7 - 安装NGINX作为负载平衡器
- 2024-09-30 Kali与编程:Centos7上搭建web服务器
你 发表评论:
欢迎- 08-06linux 和 windows文件格式互相转换
- 08-06谷歌 ChromeOS 已支持 7z、iso、tar 文件格式
- 08-06Linux下比较文件内容的6种方法
- 08-06文件格式及功能汇总
- 08-0610个Linux文件内容查看命令的实用示例
- 08-06Linux-如何区分不同文件类型
- 08-06Zabbix技术分享——监控windows进程资源使用情况
- 08-06Linux系统卡顿?学会ps命令这三招,轻松定位问题进程
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)