CentOS 7 部署.NET5.0+supervisor+nginx+mysql


.NET5.0

安装dotnet

sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
yum install dotnet-sdk-5.0

主要命令

#编译
dotnet build
#编译及运行
dotnet run
#发布
dotnet publish -c release
#跟模板创建项目(dotnet new命令不加参数可以查看所有的模板,使用模板的短名称作为参数可以创建对应的模板程序)
dotnet new classlib -lang "C#" -n projectname -o foldername
#给项目添加nuget包
dotnet add demo.csproj package {package name}

开放浏览器端口firewall-cmd --zone=public --add-port=5000/tcp --permanent

mysql

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
sudo rpm -ivh mysql80-community-release-el7-3.noarch.rpm
yum install mysql-server

修改初始密码

systemctl start mysqld
systemctl enable mysqld
grep 'temporary password' /var/log/mysqld.log
mysql -u root -p
use mysql;
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpass');

nginx

yum install epel-release
yum install nginx
systemctl enable nginx
systemctl start nginx
vim /etc/nginx/nginx.conf
nginx -t
nginx -c /etc/nginx/nginx.conf
nginx -s reload

/etc/nginx/nginx.conf关键配置

listen       80 default_server;
listen       [::]:80 default_server;
server_name  _;
#此处是静态页面托管部署路径
root         /usr/share/nginx/html;

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
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;
}

supervisor

yum install epel-release
yum install -y supervisor
systemctl enable supervisord
supervisord -c /etc/supervisord.conf

关键配置/etc/supervisord.conf
;分号开头为注释项,取消对应注释可以开启网页管理界面与管理界面的密码登录等功能

[inet_http_server]         ; inet (TCP) server disabled by default
port=*:9001        ; (ip_address:port specifier, *:port for all iface)
;username=user              ; (default is no username (open server))
;password=123               ; (default is no password (open server))

[supervisorctl]
;serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=http://*:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set

[include]
files = supervisord.d/*.conf ;这行默认是加载/etc/supervisord.d/目录内所有conf后缀的配置,喜欢用ini的可以改成ini

给demo应用添加配置文件demo.conf至supervisord.d目录(conf内的每个绝对路径必须先存在,不然运行时会报错)

[program:demo]
command=/bin/bash -c "dotnet demo.dll --urls=http://*:5000"
directory=/var/www/demo/
environment=ASPNETCORE__ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=3
redirect_stderr=true
stderr_logfile=/home/supervisor/log/demo.err.log
stdout_logfile=/home/supervisor/log/demo.out.log

给nginx添加配置文件nginx.conf至supervisord.d目录

[program:nginx]
command=nginx -g 'daemon off;'
environment=ASPNETCORE__ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=3
redirect_stderr=true
stderr_logfile=/home/supervisor/log/nginx.err.log
stdout_logfile=/home/supervisor/log/nginx.out.log

如果systemctl里启用了nginx,需要关闭

systemctl stop nginx
systemctl disable nginx

打开了http管理界面的配置后,可以用浏览器访问管理界面

supervisorctl reload #修改完配置文件需要执行此命令重新加载服务
supervisorctl status