CENTOS7.3 64位架设使用MYSQL数据库的ASP.NET CORE网站

注:本人使用的是云服务器,具体CentOS怎么安装这里不作赘述。

网站架设效果可以查看https://www.resape.com

一、在CentOS上安装Dotnet Core环境

1、Add the dotnet product feed

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl= https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'

2、Install the .NET SDK

sudo yum update
sudo yum install libunwind libicu
sudo yum install dotnet-sdk-2.1.4

具体介绍可参考如下地址:

https://www.microsoft.com/net/learn/get-started/linuxcentos

二、使用 Apache 在 Linux 上托管 ASP.NET Core

1、安装 Apache web 服务器

sudo yum -y install httpd mod_ssl

2、配置 Apache 用于反向代理

在/etc/httpd/conf.d/路径下创建resape.conf,内容如下:

<VirtualHost *:80>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ErrorLog /var/log/httpd/resape-error.log
    CustomLog /var/log/httpd/resape-access.log common
</VirtualHost>

3、将文件保存与测试配置

sudo service httpd configtest

4、重新启动 Apache:

sudo systemctl restart httpd
sudo systemctl enable httpd

5、将站点配置为服务启动

在/etc/systemd/system/路径下创建kestrel-resape.service,内容如下:

[Unit]
Description=Example .NET Web API App running on CentOS 7

[Service]
WorkingDirectory=/var/www/html
ExecStart=/usr/share/dotnet/dotnet /var/www/html/Resape.dll
Restart=always
# Restart service after 10 seconds if dotnet service crashes
RestartSec=10
SyslogIdentifier=dotnet-example
User=apache
Environment=ASPNETCORE_ENVIRONMENT=Production 

[Install]
WantedBy=multi-user.target

6、保存并启用服务

systemctl enable kestrel-resape.service
systemctl start kestrel-resape.service
systemctl status kestrel-resape.service

7、查看日志

sudo journalctl -fu kestrel-resape.service

具体介绍可参考如下地址:

https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/linux-apache

如果一切正常,到目前为止,网站已经可以访问了。

三、安装MariaDB

1、安装

yum install mariadb-server mariadb 

2、开机启动

systemctl start mariadb
systemctl enable mariadb 

3、初始化

/usr/bin/mysql_secure_installation 

4、开启远程访问权限

use mysql;   
GRANT ALL PRIVILEGES ON *.* to 'root'@'%' identified by '123456';  
flush privileges; 

四、开启防火墙

1、启用防火墙

systemctl start firewalld.service

2、开放相应端口

sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --permanent --zone=public --add-port=3306/udp

3、重新加载防火墙

sudo firewall-cmd --reload

五、配置SSL证书

1、修改配置文件

将之前的resape.conf修改为如下内容:

<VirtualHost *:80>
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R,L]
</VirtualHost>

<VirtualHost *:443>
    ProxyPreserveHost On
    ProxyPass / http://127.0.0.1:5000/
    ProxyPassReverse / http://127.0.0.1:5000/
    ErrorLog /var/log/httpd/resape-error.log
    CustomLog /var/log/httpd/resape-access.log common
    SSLEngine on
    SSLProtocol all -SSLv2
    SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:!RC4+RSA:+HIGH:+MEDIUM:!LOW:!RC4
    SSLCertificateFile /etc/pki/tls/certs/resape.crt
    SSLCertificateKeyFile /etc/pki/tls/private/resape.key
    SSLCertificateChainFile /etc/pki/tls/certs/1_root_bundle.crt
</VirtualHost>

注意把证书放入配置文件指定的位置。

2、保存配置文件并重启Apache

sudo service httpd configtest
sudo systemctl restart httpd

至此,一切大功告成。

posted @ 2018-10-10 15:57  齐建伟  阅读(351)  评论(0编辑  收藏  举报