Centos7 部署NetCore6.0 Web Api
一、安装dotnet环境
第一步
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
第二步
sudo yum install dotnet-sdk-6.0
第一次:Is this ok [y/d/N] y(确认下载安装包)
第二次:Is this ok [y/d/N] y(下载完成,确认安装)
第三步
sudo yum install dotnet-runtime-6.0
安装完成
通过dotnet --info命令查看是否安装完成,下面截图的是安装成功的显示
二、打包发布NetCore WEB API程序
1.vs中右键项目选择"发布"
2.如下图
在服务器创建文件夹,如dotnet_api,使用ftp将打包的文件发布到服务器创建的文件夹里
三、跑起来发布的WEB API
dotnet xxxx.dll --urls http://*:5000 然后再浏览器输入服务器IP:5000就可以访问发布的WEB API了
这样虽然跑起来了,但是Shell一关闭就不行了,这肯定不行,所以我们得给发布的WEB API做成自启动。
四、自启动,类似windows服务
1.创建自启动文件(再 /etc/systemd/system/目录里)
vim /etc/systemd/system/xxx.service
配置文件的内容
[Unit] Description=xx.service[Service]
WorkingDirectory=/site/xx/xx/www
ExecStart=/usr/share/dotnet/dotnet /site/xx/xx.dll --urls http://*:5000
Restart=always
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=AspnetCore
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false[Install]
WantedBy=muti-user.target
2.设置为自启动
systemctl enable xxx.service
3.启动服务
systemctl start xxx.service
4.查看服务状态
systemctl status xxx.service
这个时候就可以访问WEB API了
五、使用nginx做代理转发域名到5000端口
1.安装nginx
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm sudo yum install -y nginx
2.启动nginx
sudo systemctl start nginx.service
3.配置nginx域名转发(打开/etc/nginx/文件夹下找到nginx.conf文件打开进行编辑)
#user nginx;
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
server_name api.www.xx.com;
location / {
proxy_pass http://localhost:5000;
}
}
server{
listen 80;
server_name page.www.xx.com;
location / {
alias /站点路径/xx/page/www/;
index index.html index.htm;
}
}
}
配置好了重启一下服务就可以了:
sudo systemctl restart nginx.service
就可以通过域名访问发布的WEB API了
六、Nginx怎么配置SSL
server { listen 443 default ssl;这里要加上default ssl否则访问会报错(ERR_SSL_PROTOCOL_ERROR) server_name xx.xxx.com; ssl_certificate /路径/xxx_bundle.crt; ssl_certificate_key /路径/xxx.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; location / { proxy_pass http://localhost:5000; } } server { listen 80; server_name xxx.xxx.com; rewrite ^/(.*)$ https://xxx.xxx.com:443/$1 permanent; }
同样的配置好了需要重启nginx服务