Centos 7 中 部署 asp.net core 3.0 + nginx + mongodb 的一些新手简单入门,非docker


正文开始

零.准备工作

由于以前一直部署在Windows系统下,,这次领导说要测试下部署到Centos上,测试了一个下午,,踩了不少的坑,,因此记录下来

主要参考了几篇文章:
https://docs.microsoft.com/zh-tw/dotnet/core/install/linux-package-manager-centos7 //介绍怎么安装core运行时的
https://docs.microsoft.com/zh-tw/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1 //最重点的文章了,大部分都在这里
https://blog.csdn.net/qq_24232123/article/details/79781527 //这篇主要是防火墙的配置指令,,比较懒得背
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ //mongodb的安装部署
https://docs.mongodb.com/manual/reference/configuration-options/ //mongodb的配置

总共有几条常用的命令:

systemctl enable xxxxxx #允许一个服务自动启
systemctl start xxxx #启动一个服务
systemctl stop xxxx #停止一个服务
firewall-cmd --add-port=666/tcp --permanent   #防火墙开放一个端口
systemctl daemon-reload   #修改服务文件后,用来强制重新加载的
yum install xxxx #安装软件
nano xxxx #编辑文件
chown -R mongod:xxx  #某个目录授权给mongod用户
mkdir -p /usr/website/api #新建目录

需要下载两个软件,都是免费软件,放心下载

https://www.putty.org/ 中的 PuTTY,,Bitvise SSH Client ,,PuTTY用来做远程终端命令行的,另外一个是用来上传文件的,特别建议安装 Bitvise SSH Client,能直接打开文件管理器,后续的修改配置文件的操作,可以直接edit,然后用本机的记事本修改后,自动上传,比在命令行下编辑配置文件,方便许多

安装nano

    yum install nano

一.部署Mongodb

0.创建对应用户

    adduser mongod    #添加一个mongod用户专门跑mongodb的

1.安装Mongodb

    sudo nano /etc/yum.repos.d/mongodb-org-4.2.repo

    粘贴入,保存后退出

	  [mongodb-org-4.2]
    name=MongoDB Repository
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
    gpgcheck=1
       enabled=1
    gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
	   sudo yum install -y mongodb-org

2.创建mongodb的数据目录

    mkdir -p /usr/website/database/mongodb/db
    mkdir -p /usr/website/database/mongodb/log

3.设置目录权限

    sudo chown -R mongod:mongod /usr/website/database/mongodb/db
    sudo chown -R mongod:mongod /usr/website/database/mongodb/log
       sudo chown -R mongod:mongod /opt/mongo  #mongodb的安装路径,给新用户赋权可以访问

4.设置mongodb启动

    sudo systemctl enable mongod.service   

5.修改mongodb的配置文件

    sudo nano /etc/mongod.conf
    修改storage下的dbPath = /usr/website/database/mongodb/db #不带/结尾
    修改net 下的 port = #新的mongodb端口号
          bind= 0.0.0.0 #允许远程连接

6.启动Mongodb服务

    sudo systemctl start mongod.service
    sudo systemctl status mongod.service

备注:如果是自己编译的arm版的mongodb的话,会缺少mongod.service和mongod.conf两个文件,可以复制下面的文本,自己创建一个

mongod.service 文件,放在 /etc/systemd/system 下

[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/opt/mongo/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target

mongod.conf 文件,放在 /etc 下

# mongod.conf

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /usr/website/database/mongodb/log/mongod.log

# Where and how to store data.
storage:
  dbPath: /usr/website/database/mongodb/db
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 8114
  bindIp: 0.0.0.0  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.

#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:

二.安装.net core的运行时:

	sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
        sudo yum install aspnetcore-runtime-3.0
      sudo yum install dotnet-runtime-3.1

三.新建web项目的文件夹

	mkdir -p /usr/website/api

  然后使用 Bitvise SSH Client 把项目站点文件上传到文件夹下
  上传成功后,使用 dotnet xxx.web.dll 试试看项目能不能启动,如果提示类似下面的,就表示成功了

img

四.创建站点自动启动的服务

1.创建服务文件

    sudo nano /etc/systemd/system/kestrel-api.service

    内容编辑中,粘贴入:  

	   [Unit]
     Description=Centos RTU Api
 
     [Service]
     WorkingDirectory=/usr/website/api  #工作目录 
     #调用的,由于非docker,因此端口号都是共用的,所以core 3.x 使用 --urls 设置了端口号,有一个坑就是,在命令行下,--url= 后面可以使用双引号,,
     #但是在这里不能,加了双引号就报错
     ExecStart=/usr/bin/dotnet /usr/website/api/xxx.Web.dll --urls=http://*:5123    
     Restart=always
     # Restart service after 10 seconds if the dotnet service crashes:
     RestartSec=10
     KillSignal=SIGINT
     SyslogIdentifier=dotnet-example 
     User=root    #这个是使用的用户,如果非root,则先创建用户,然后把文件夹的权限赋给所使用的用户
     Environment=ASPNETCORE_ENVIRONMENT=Production  #这是设置运行环境
     Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
 
     [Install]
     WantedBy=multi-user.target

2.注册服务

    sudo systemctl enable kestrel-api.service
    sudo systemctl start kestrel-api.service
    sudo systemctl status kestrel-api.service

3.命令行下使用 ps -ef

   看到有这条:

img

		就表示运行起来了

  使用

		curl http://localhost:5000
  看看有没有返回值,并且返回的是不是正确的

  如果无法正确启动,可以使用微软站点中介绍的:

		sudo journalctl -fu kestrel-api.service

  在日志中,尽量排查出什么问题,一般的话,如果在第二步中,能正常运行的话,那么大部分情况下,程序上就没什么问题,主要是一些目录权限之类的问题
    因为测试的时候是使用root,所以没什么目录权限上的问题,

五.处理nginx

1.安装nginx

  	yum install nginx

2.配置站点

    cd /etc/nginx  #如果是使用sftp的话,一定是要放在 /etc/nginx 目录下,不能是 /etc 否则要自己去修改nginx对配置文件的查找路径
    nano nginx.conf

  在 server 段下 再新增一段

	server {
        listen 8081;                #监听8081端口
        server_name x.x.x.x:8081;   #这里我需要绑定非80端口
        location /{
            proxy_pass         http://localhost:5123;    #反向代理到我们刚才指定的--urls 参数的端口号
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $http_host;          #这个与微软上的不同, 微软上写的是 $host 这个的话,在重定向的时候,会把端口号忽略掉,$http_host会带上端口号
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
        }
    }

3.nginx的常用命令:

	nginx -s reload   #重新加载配置
	nginx -V          #查看版本
	nginx -t          #测试配置文件是否正确
	nginx -s quit     #关闭服务器端,不过一般推荐从systemctl stop 去关闭

六.开放防火墙端口

      firewall-cmd --add-port=8081/tcp --permanent

 如果提示错误,请参考最上面的文章链接,如果还是不能访问,请检查云服务器的安全组是否有打开对应的端口号

七.有几个小问题需要注意一下

1.重定向的问题:

  如果是非80端口的,,需要在程序里重定向的话,一定要在nginx中,配置站点的时候,用
proxy_set_header Host $http_host ,,如果按照微软上的用 $host的话,重定向的时候,端口号会被去掉

2.绘图的问题,如果有调用到System.DrawingCore之类的绘图组件的时候,记得使用下面的命令安装绘图库

  `sudo yum install libgdiplus`

否则会报找不到 libgdiplus 的错误


posted @ 2020-03-03 13:00  启天  阅读(780)  评论(0编辑  收藏  举报