前言
项目需要在内网环境中的linux服务器运行,网上很少有关于如何在ASP.NET Core相关的文章,找到相关的各种教程花费了我很长时间,在这里整理出来,希望能给大家提供参考的作用。
Vue3打包后的文件放在ASP.NET Core项目中的wwwroot文件夹下
Program.cs 中添加以下代码打开静态资源服务
app.UseDefaultFiles();
app.UseStaticFiles();
部署环境:
-
系统:Centos7.9.2009 Linux
-
依赖框架 .Net 6
-
反向代理 Nginx 1.6.2
-
采用技术栈:
-
后端:ASP.NET Core
-
前端:Vue3、vite0
-
数据库:Oracle
-
主要步骤
-
配置ftp服务
-
安装dotnet环境
-
上传、解压程序文件,将程序配置为systemctl服务,设置为自启动。
-
安装Nginx
-
配置Nginx
正文
第一步:配置ftp服务
被分配到的服务器已经配置过ftp服务,就直接用Filezilla登录使用了。大家可以去找找如何配置ftp服务的文章,网上已经有很多了。
第二步:安装dotnet运行环境
由于服务器无法连接外网,所以这里要用离线安装的方式。(注意:下载ASP.NET Core运行时,不是sdk)
这里我的linux服务器是X64架构的,所以我要点击下载x64
.NET 6.0 离线安装包下载地址: dotnet.microsoft.com/zh-cn/downl…
-
首先在Linux上创建一个目录:
mkdir -p /var/lib/dotnet -
创建完成后,用ftp把tar.gz文件上传到创建的目录, 然后在终端进入创建的目录,解压安装包
tar zxf 安装包文件名 -C /var/lib/dotnet -
设置环境变量:
export DOTNET_ROOT=/var/lib/dotnet export PATH=$PATH:/var/lib/dotnet -
创建软链接:
ln -s /var/lib/dotnet/dotnet /usr/local/bin
因为我们要用nginx把程序反向代理出来,所以这里不用设置防火墙放行程序运行后的5000端口。
但若要测试程序运行是否正常,可以先放行。
firewall-cmd --zone=public --add-port=5000/tcp --permanent
firewall-cmd --reload
也可以直接关闭防火墙
service firewalld stop
第三步:发布程序
- 发布程序,这里在VS点击左上角的菜单中的生成-->发布-->选择发布到文件夹。点击发布。
-
然后把文件复制出来,压缩为zip压缩包(压缩前先把文件夹重名为自己的项目名)
-
为服务程序创建目录
mkdir -p /var/www/ -
用ftp把zip压缩包上传至/var/www/目录下
-
cd到目录/var/www/下,解压压缩包
unzip -d -o /var/www/ 压缩包名试运行程序
cd 解压出来的文件夹名 dotnet 项目名.dll浏览器打开 http://IP地址:5000 查看
-
设为systemctl服务,设置开机自启动。
创建服务文件
sudo vi /etc/systemd/system/项目名.service文件内容
[Unit] Description=Example .NET Web API App running on Ubuntu [Service] WorkingDirectory=/var/www/第5步解压出来的文件夹名 ExecStart=/usr/local/bin/dotnet /var/www/第5步解压出来的文件夹名/项目名.dll Restart=always # Restart service after 10 seconds if the dotnet service crashes: RestartSec=10 KillSignal=SIGINT SyslogIdentifier=dotnet-example User=用户名 Environment=ASPNETCORE_ENVIRONMENT=Production Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false [Install] WantedBy=multi-user.target保存该文件并启用该服务
sudo systemctl enable 项目名.service启用该服务,并确认它正在运行。
sudo systemctl start 项目名.service sudo systemctl status 项目名.service
第四步:安装nginx
内网环境下安装nginx还需要安装nginx所需的依赖,不像使用包管理器安装——跑一句命令那么方便。
我的linux为centos7,所以在下面的网址里可以下载到所需的依赖包。
里面的包名一致但版本不一定,例如cmake,我在里面就找cmake-版本号-x86_64.rpm
cmake-2.8.12.2-2.el7.x86_64.rpm
make-3.82-24.el7.x86_64.rpm
gcc-4.8.5-39.el7.x86_64.rpm
gcc-c++-4.8.5-39.el7.x86_64.rpm
pcre-devel-8.32-17.el7.x86_64.rpm
pcre2-10.23-2.el7.x86_64.rpm
zlib-devel-1.2.7-18.el7.x86_64.rpm
openssl-devel-1.0.2k-8.el7.x86_64.rpm
下载好之后,新建目录,上传至该目录下。
mkdir /usr/local/software
cd /usr/local/software
下面这条命令安装该文件夹下所有安装包
rpm -Uvh *.rpm --nodeps --force
准备好nginx的安装包
nginx-1.6.2.tar.gz
Nginx安装
tar zxvf nginx-1.6.2.tar.gz -C /usr/local/
cd /usr/local/nginx-1.6.2/ && ./configure --prefix=/usr/local/nginx
make && make install
cd /usr/local/nginx && ls
防火墙端口放行
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports
服务配置
vi /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: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/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/sbin/nginx"
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
# NGINX_CONF_FILE="/etc/nginx/nginx.conf"
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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
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
fi
}
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 $prog -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
给 /etc/init.d/nginx 文件赋予执行权限
chmod +x /etc/init.d/nginx
创建pid文件
echo 10000 > /var/run/nginx.pid
将启动脚本配置到chkconfig服务
chkconfig --add /etc/init.d/nginx
chkconfig --list
开机自启
chkconfig nginx on
Nginx的查看状态、启动、停止、重启命令
service nginx status
service nginx start
service nginx stop
service nginx restart
第五步:托管服务
1.6.2版本的Nginx的配置文件在这里修改
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
server {
listen 80;
server_name 域名或服务器ip;
location / {
proxy_pass http://127.0.0.1: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;
try_files $uri $uri/ /index.html;
}
# 我的接口都是在api下的
location /api/ {
proxy_pass http://127.0.0.1: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;
}
# 解决找不到vue打包文件中的静态资源的问题
location /assets/ {
proxy_pass http://127.0.0.1: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;
}
}
启动上文中配置好的服务及Nginx服务,至此完成部署。
sudo systemctl start 配置的服务名.service
service nginx start