下面是CentOS上搭建Nginx+Mono运行asp.net环境的配置方法的完整攻略:
准备环境
- 安装必要的依赖:
sudo yum install epel-release yum-utils curl httpd-tools
sudo yum install centos-release-scl
sudo yum install rh-dotnet20
sudo yum install rh-dotnet20-aspnetcore-runtime rh-dotnet20-runtime
sudo yum install nginx
- 创建日志目录:
sudo mkdir /var/log/nginx/demo_app
sudo touch /var/log/nginx/demo_app/access.log
sudo touch /var/log/nginx/demo_app/error.log
sudo chgrp -R nginx /var/log/nginx/demo_app
sudo chmod -R g+rwx /var/log/nginx/demo_app
sudo chmod -R g+s /var/log/nginx/demo_app
- 创建demo_app.conf配置文件:
sudo nano /etc/nginx/conf.d/demo_app.conf
然后输入以下内容:
upstream demo_app {
server unix:/var/www/demo_app/www/demo_app.sock;
}
server {
listen 80;
server_name demo_app.com www.demo_app.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name demo_app.com www.demo_app.com;
ssl_certificate /etc/nginx/certs/demo_app.com.crt;
ssl_certificate_key /etc/nginx/certs/demo_app.com.key;
client_max_body_size 50M;
server_tokens off;
access_log /var/log/nginx/demo_app/access.log;
error_log /var/log/nginx/demo_app/error.log;
location / {
try_files $uri $uri/ @demo_app;
}
location /xmlrpc.php {
deny all;
}
location /wp-admin/install.php {
deny all;
}
location /wp-admin/includes {
deny all;
}
location /wp-includes {
deny all;
}
location @demo_app {
include /etc/nginx/fastcgi_params;
fastcgi_pass demo_app;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME /var/www/demo_app/www$fastcgi_script_name;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param HTTP_X_REAL_IP $remote_addr;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param ASPNETCORE_ENVIRONMENT Production;
}
}
- 运行Nginx和Mono:
sudo service nginx start
sudo systemctl enable nginx
sudo systemctl start rh-dotnet20-aspnetcore-runtime
部署应用程序
- 下载应用程序:
sudo mkdir /var/www/demo_app
sudo chown -R nginx /var/www/demo_app
cd /var/www/demo_app
sudo git clone https://github.com/aspnet/AspNetCore.Docs.git -b master
- 修改应用程序配置:
cd /var/www/demo_app/AspNetCore.Docs/aspnetcore/tutorials/first-web-api
sudo nano appsettings.json
修改appsettings.json的内容:
{
"AllowedHosts": "*",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://0.0.0.0:8050"
},
"Https": {
"Url": "https://0.0.0.0:8051",
"Certificate": {
"Path": "/etc/ssl/certs/mycert.pem",
"Password": ""
}
}
},
"AllowedHosts": "*"
}
}
- 修改应用程序启动脚本:
cd /var/www/demo_app/AspNetCore.Docs/aspnetcore/tutorials/first-web-api
sudo nano start.sh
修改start.sh的内容:
#!/bin/bash
cd /var/www/demo_app/AspNetCore.Docs/aspnetcore/tutorials/first-web-api
source scl_source enable rh-dotnet20
/dotnet publish -c Release -o $HOME/www
cd $HOME/www
/dotnet aspnetcore-tutorials.dll
- 运行应用程序:
chmod +x start.sh
./start.sh
示例1:
假设我们的域名为www.example.com,我们需要配置Nginx和Mono来部署asp.net Web API应用程序。首先,我们在Nginx的配置文件中添加一条新的server block,像这样:
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://127.0.0.1:5002;
proxy_set_header Host $host;
}
}
然后,我们在应用程序的启动脚本中添加下面的环境变量:
export ASPNETCORE_URLS=http://127.0.0.1:5002
最后,我们启动Nginx和Mono,并运行我们的应用程序:
sudo service nginx start
sudo systemctl enable nginx
sudo systemctl start rh-dotnet20-aspnetcore-runtime
/snap/dotnet --version # 检查.NET Core版本
git clone https://github.com/aspnet/AspNetCore.Docs -b master
cd AspNetCore.Docs/aspnetcore/tutorials/web-api
export ASPNETCORE_URLS=http://127.0.0.1:5002
chmod +x start.sh
./start.sh
示例2:
假设我们需要在CentOS上部署一个asp.net应用程序,该应用程序需要使用特定的端口号和定制SSL证书来进行加密通信。我们需要在Nginx的配置文件中添加以下内容:
server {
listen 80;
server_name www.example.com;
location / {
proxy_pass http://127.0.0.1:5002;
proxy_set_header Host $host;
}
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /path/to/ssl/certificate;
ssl_certificate_key /path/to/ssl/private/key;
location / {
proxy_pass http://127.0.0.1:5003;
proxy_set_header Host $host;
}
}
然后,我们在应用程序的启动脚本中添加下面的环境变量:
export ASPNETCORE_URLS=https://127.0.0.1:5003
最后,我们启动Nginx和Mono,并运行我们的应用程序:
sudo service nginx start
sudo systemctl enable nginx
sudo systemctl start rh-dotnet20-aspnetcore-runtime
/snap/dotnet --version # 检查.NET Core版本
git clone https://github.com/aspnet/AspNetCore.Docs -b master
cd AspNetCore.Docs/aspnetcore/tutorials/web-api
export ASPNETCORE_URLS=https://127.0.0.1:5003
chmod +x start.sh
./start.sh