安装 .NET Core Runtime运行环境
安装
sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
sudo yum update
sudo yum install aspnetcore-runtime-2.1 ##版本看自己
如果环境安装成功,可以用如下命令检测
dotnet --info
部署程序
使用ftp等类似工具将发布好的代码上传到 /usr/local/wwwroot/项目目录目录
这个时候可以进入项目文件夹,启动你的项目了
cd /usr/local/wwwroot/项目目录
dotnet 项目名称.dll
如果显示如下信息,则你项目代码和环境已经没问题,但这时候还不能访问
[root@localhost rvcs]# dotnet LinuxDemo.dll
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /usr/local/wwwroot/rvcs
注意,默认会有两个访问端口,5000是http,5001是HTTPS,端口是程序项目文件launchSettings.json代码里面设置的,这个自行了解
使用Nginx转发
下载
wget -c http://nginx.org/download/nginx-1.9.9.tar.gz
tar -zxvf nginx-1.9.9.tar.gz
安装gcc
cd nginx-1.9.9
yum install gcc gcc-c++
./configure
安装插件
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
为https做准备
yum install -y openssl openssl-devel
安装Nginx
./configure
make install
安装完成后查找目录,一般都在下面目录
cd /usr/local/nginx/sbin/
设置自启,先创建一个软连接指向Nginx安装目录
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
在/etc/init.d/目录下创建脚本nginx,个人觉得vim不好用,不如直接右键手动操作
vim /etc/init.d/nginx
脚本内容如下
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
授权----------------------------------------------------------------------
chmod a+x /etc/init.d/nginx
chkconfig --add /etc/init.d/nginx
chkconfig nginx on
常用命令
systemctl start nginx ##查看状态
./nginx ##启动nginx
./nginx -s stop ##停止
./nginx -s quit ##停止
./nginx -s reload ##重新加载 Nginx 配置
停止还可以用命令-s stop: 查出nginx的进程ID,然后kill强制沙雕进程
在nginx中配置关于.net core程序的配置
进入nginx目录,打开配置文件nginx.conf,还是不建议用vim
cd /usr/local/nginx/conf/
vim nginx.conf
找到sever节点,修改为如下配置
server {
listen 80;
server_name localhost;
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;
}
}
server_name是域名,默认就是IP地址,你们懂得
proxy_pass是启动.net core是分配的地址,这里使用http的地址,https的另谷歌自行配置
常用命令
./nginx -t ##测试格式是否正确
./nginx -s reload ##重新加载配置
完成到这步,就可以到发布的程序目录下启动项目,然后通过浏览器就可以访问了
cd /usr/local/wwwroot/项目目录
dotnet 项目名称.dll
但是只要关闭linux连接工具或者重启==,项目就会停止,这里还需安装一个服务来自动运行项目,微软推荐用Supervisor
安装Supervisor实现.net core自动运行
安装
sudo yum install supervisor
创建配置目录
mkdir -m 700 -p /etc/supervisor
引用配置文件
echo_supervisord_conf > /etc/supervisor/supervisord.conf
在 /etc/supervisor 目录下我们创建一个存放我们 dotnet core 进程文件的存放目录 conf.d
mkdir -m 700 /etc/supervisor/conf.d
修改刚刚引用的配置文件supervisord.conf,主要就是把文档的最后两行修改如下。注意,这里一定要把前面的 ; 去掉,否则的话这个 include 节点还是被注释无法被应用的!!
[include]
files=/etc/supervisor/conf.d/*.conf
创建一个 LinuxDemo.conf配置文件来管理我们这个项目的 dotnet 进程。
cd /etc/supervisor/conf.d/
touch LinuxDemo.conf
文件内容如下
[program:LinuxDemo]
command=/bin/bash -c "dotnet 项目名称.dll"
directory=/usr/local/wwwroot/项目目录/
environment=ASPNETCORE__ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=3
stderr_logfile=/usr/local/wwwroot/log/LinuxDemo.err.log
stdout_logfile=/usr/local/wwwroot/log/LinuxDemo.out.log
注意这里的日志文件是不会自己生成的,一定要手动创建,不然服务启动失败
创建 Supervisor 的自启动服务。
vim /etc/systemd/system/supervisor.service
内容如下
[Unit]
Description=supervisor
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
重新载入我们的设置。
systemctl daemon-reload
设置 supervisor.service 服务开机启动
systemctl enable supervisor.service
启动服务
systemctl start supervisor.service ##启动
systemctl stop supervisor.service ##停止
使用命令检查程序,ps -ef | grep 项目名称 如果显示有两个进程,则大功告成
[root@localhost /]# ps -ef | grep LinuxDemo
root 15677 15676 0 10:04 ? 00:00:02 dotnet LinuxDemo.dll
root 16718 15535 0 10:37 pts/0 00:00:00 grep --color=auto LinuxDemo
注意,当你对配置文件做了任何的改变后,你都需要将 Supervisor 进行重启。
踩过的坑
一,虽然我们并没有配置https,但程序启动后跳转到https的访问端口,这很可能是NetCore项目的默认配置造成的。在net core官网文档查到Startup类默认是建议使用HTTPS的。
解决方法有二、一是你在centos上安装好HTTPS证书,二是注释掉项目Startup类里面app.UseHttpsRedirection();
二,为supervisord服务创建的 *.conf配置文件里面的日志一定要手动创建
三, 服务器是否开放端口
端口
查看已开放的端口: firewall-cmd --list-ports
开启的端口号: firewall-cmd --zone=public --add-port=9001/tcp --permanent
重新加载: firewall-cmd --reload
防火墙
关闭防火墙命令:systemctl stop firewalld.service
开启防火墙:systemctl start firewalld.service
关闭开机自启动:systemctl disable firewalld.service
开启开机启动:systemctl enable firewalld.service
查看进程端口:netstat -ltunp
参考
1. ASP.NET Core 实战:Linux 小白的 .NET Core 部署之路
2. .netcore部署Linux并结合Nginx反向代理 get started
3. Centos 7 .Net core后台守护进程Supervisor配置
4. 微软官方文档