docker中的centos7搭建.net7运行环境

centos7搭建.net7运行环境

  • docker中已拉去centos7的镜像
  • 安装centos中安装工具
  • 安装asp.net 7.0运行环境
  • 安装nginx

docker中已拉去centos7的镜像

创建容器:

docker run -i -t -d --name centos7 -p 2000:20 -p 2100:21 -p 2200:22 -p 8000:80 -p 4430:443 --privileged=true -v /d/centos:/www/wwwroot centos:7.8.2003 /usr/sbin/init

安装centos中安装工具

创建的centos容器缺乏很多工具,需要安装。
1、安装sudo

yum -y install sudo

2、安装wget

yum -y install wget

3、安装dnf

yum install dnf

安装asp.net 7.0运行环境

1、配置受信任源

sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

2、安装运行环境

sudo dnf install aspnetcore-runtime-7.0

安装nginx

以下引用nginx官方的方法
Install the prerequisites:

sudo yum install yum-utils

To set up the yum repository, create the file named /etc/yum.repos.d/nginx.repo with the following contents:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

By default, the repository for stable nginx packages is used. If you would like to use mainline nginx packages, run the following command:

sudo yum-config-manager --enable nginx-mainline (不必要)
To install nginx, run the following command:

安装nginx

sudo dnf install nginx

启动nginx(需要先确保80端口未被其他程序占用)

systemctl start nginx

设为开机启动

systemctl enable nginx

查看版本

nginx -v

配置nginx文件,覆盖nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;
    keepalive_timeout  65;
    client_max_body_size 500m;

    server {
        listen        80;
        server_name  _;
        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;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_connect_timeout       600;
            proxy_send_timeout          600;
            proxy_read_timeout          600;
            send_timeout                600;
        }
    }
}

验证配置

sudo nginx -t

重载

sudo nginx -s reload

以sscms系统为例:

创建服务文件
创建服务定义文件 sscms.service:

sudo touch /etc/systemd/system/sscms.service

将以下内容保存至 /etc/systemd/system/sscms.service:

[Unit]
Description=SSCMS

[Service]
WorkingDirectory=/var/www
ExecStart=/usr/bin/dotnet /var/www/SSCMS.Web.dll
Restart=always


#Restart service after 10 seconds if the sscms service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=sscms
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

保存文件并启用服务。

sudo systemctl enable sscms.service

运行服务,并确认它正在运行。

sudo systemctl start sscms.service
sudo systemctl status sscms.service

你可能感兴趣的:(docker,.net,容器)