2019/03/14

ASP.Net Core Nginx on CentOS 7

本篇參考至「ASP.NET Core + Nginx on CentOS 安裝筆記


執行結果:




# Install .Net Core 2.2
Install .Net Core 2.2
 
# Instanll NGINX
yum install -y epel-release
yum update -y
yum install -y nginx

# Add firewall rules
firewall-cmd --permanent --zone=public --add-service=http 
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

# Add SELinux HTTP permission
setsebool -P httpd_can_network_connect on

# Create certificate
mkdir /etc/ssl/private
chmod 700 /etc/ssl/private
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt
openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

# Add nginx.conf
vi /etc/nginx/conf.d/asp.conf
server {    
 listen 443 http2 ssl;
 listen [::]:443 http2 ssl;

 server_name localhost;
 ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
 ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
 ssl_dhparam /etc/ssl/certs/dhparam.pem;

 location / {
  proxy_pass         http://localhost:5000;
  proxy_http_version 1.1;
  proxy_set_header   Upgrade $http_upgrade;
  proxy_set_header   Connection keep-alive;
  proxy_set_header   Host $host;
  proxy_cache_bypass $http_upgrade;
  proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header   X-Forwarded-Proto $scheme;
    }    
}

# Add www-data user and group
groupadd www-data
useradd -g www-data www-data
usermod -a -G www-data jeffrey
chmod g+w -R /var/www/WebOnlineEditor

# Add Web Service
vi /etc/systemd/system/kestrel-woe.service

[Unit]
Description=WebOnlineEditor

[Service]
WorkingDirectory=/var/www/WebOnlineEditor
ExecStart=/usr/bin/dotnet /var/www/WebOnlineEditor/WebOnlineEditorApplication.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=WebOnlineEditor
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

# Start service
systemctl enable kestrel-woe.service
systemctl start kestrel-woe.service


參考資料:
https://blog.darkthread.net/blog/aspnetcore-with-nginx/
https://blog.csdn.net/lwwl12/article/details/78293216
https://blog.johnwu.cc/article/ironman-day30-asp-net-core-kestrel-web-server.html
https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.2
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-2.2
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/httpsys?view=aspnetcore-2.2