一、安装asp.net
1. 安装Microsoft 源
将 Microsoft 包签名密钥添加到受信任密钥列表,并添加 Microsoft 包存储库:
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
2. 安装运行时
安装 ASP.NET Core 运行时:
sudo yum install -y aspnetcore-runtime-3.1
安装其他版本是只需更改版本号,如:sudo yum install -y aspnetcore-runtime-6.0
二、将asp.net项目注册成服务
1. 创建服务文件
创建服务定义文件:
vi /usr/lib/systemd/system/myweb.service
服务文件名称:myweb.service,不允许重复。
2. 以下示例是应用的一个 myweb服务文件:
[Unit]
Description="myweb Service"
[Service]
Type=simple
GuessMainPID=true
# 项目路径
WorkingDirectory=/opt/www/
Restart=always
# 如果 dotnet 服务崩溃,10 秒后重启服务
RestartSec=10
# 启动命令
ExecStart=/usr/bin/dotnet /opt/www/myweb.dll
# 关闭进程
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
4. 操作myweb
启动 myweb
systemctl start myweb
停止myweb
systemctl stop myweb
重启myweb
systemctl restart myweb
查看myweb状态
systemctl status myweb
启用开机启动myweb
systemctl enable myweb
禁用开机启动myweb
systemctl disable myweb
三、安装Nginx
1. 首先更新系统软件
sudo yum update -y
2. 安装nginx源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
3. 安装nginx
sudo yum install -y nginx
4. 操作Nginx
启动 Nginx
systemctl start nginx
停止Nginx
systemctl stop nginx
重启Nginx
systemctl restart nginx
重新加载配置文件
nginx -s reload
查看Nginx状态
systemctl status nginx
启用开机启动Nginx
systemctl enable nginx
禁用开机启动Nginx
systemctl disable nginx
5.Nginx反向代理报502错误,处理方法
setsebool -P httpd_can_network_connect 1 #httpd可以连接到网络
四、设置防火墙
开启80端口
sudo firewall-cmd --permanent --zone=public --add-service=http
开启443端口
sudo firewall-cmd --permanent --zone=public --add-service=https
开启指定端口(8080)
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
关闭指定端口(8080)
sudo firewall-cmd --remove-port=8080/tcp --permanent
重新加载配置文件
sudo firewall-cmd --reload