一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx、supervisor、mysql环境搭建...
作為.neter,看到.net core 2.0的正式發(fā)布,心里是有點(diǎn)小激動(dòng)的,迫不及待的體驗(yàn)了一把,發(fā)現(xiàn)速度確實(shí)是快了很多,其中也遇到一些小問題,所以整理了一些學(xué)習(xí)筆記:
閱讀目錄
- 環(huán)境說明
- 安裝CentOS7
- 安裝.NET Core SDK for CentOS7
- 搭建ftp服務(wù)器
- 安裝mysql
- 部署ASP.NET Core應(yīng)用程序
- 配置Nginx
- 配置守護(hù)服務(wù)(Supervisor)
環(huán)境說明
服務(wù)器系統(tǒng):CentOS 7.3 64位
相關(guān)工具:putty、Xftp
服務(wù)器軟件軟件:.netcore、nginx、supervisor、mysql
安裝CentOS7
這個(gè)就不細(xì)說了,網(wǎng)上教程很多,我這邊是阿里云的ecs服務(wù)器,操作系統(tǒng)為centos 7.3 x64
安裝.NET Core SDK for CentOS7
sudo yum install libunwind libicu(安裝libicu依賴) curl -sSL -o dotnet.tar.gz https://aka.ms/dotnet-sdk-2.0.0-linux-x64 (下載sdk壓縮包) mkdir -p ~/dotnet && tar zxf dotnet.tar.gz -C ~/dotnet 解壓縮) sudo ln -s /opt/dotnet/dotnet /usr/local/bin(創(chuàng)建鏈接)參見官方文檔,?需要注意的地方是第四行跟官方文檔有點(diǎn)出入,這是建立連接,而官方文檔的是添加path(只在當(dāng)前session有效:export PATH=$PATH:$HOME/dotnet)
,執(zhí)行這些操作之前請(qǐng)務(wù)必卸載舊版本及預(yù)覽版本(我是直接刪除舊版本的文件,執(zhí)行?dotnet --info 可以查看已安裝版本信息),當(dāng)執(zhí)行dotnet --version ?xianshi 2.0.0時(shí)表示安裝.net core 2.0成功
?
參考資料:?https://www.microsoft.com/net/core#linuxcentos
搭建ftp環(huán)境
安裝vsftpd
1、以管理員(root)身份執(zhí)行以下命令
2、設(shè)置開機(jī)啟動(dòng)vsftpd ftp服務(wù)
3、啟動(dòng)vsftpd服務(wù)
管理vsftpd相關(guān)命令:
停止vsftpd: ?service vsftpd stop
重啟vsftpd: ?service vsftpd restart
配置防火墻
在 CentOS 7.3中
暫時(shí)開放 ftp?服務(wù)
#?firewall-cmd --add-service=ftp
永久開放 ftp?服務(wù)
#?firewall-cmd --add-service=ftp --permanent
永久關(guān)閉
#?firewall-cmd --remove-service=ftp --permanent
success
讓設(shè)定生效
#?systemctl restart firewalld
添加ftp用戶
下面是添加ftpuser用戶,設(shè)置根目錄為/home/wwwroot/ftpuser,禁止此用戶登錄SSH的權(quán)限,并限制其訪問其它目錄(當(dāng)不存在指定目錄時(shí)需要?jiǎng)?chuàng)建相關(guān)目錄)。
1、修改/etc/vsftpd/vsftpd.conf
vim /etc/vsftpd/vsftpd.conf如需禁用匿名登錄把第一行的?anonymous_enable=YES ,改為NO
將底下三行
改為
3、增加用戶farmhome,指向目錄/home/wwwroot/farmhome,禁止登錄SSH權(quán)限。
useradd -d /home/wwwroot/farmhome -g ftp -s /sbin/nologin farmhome
4、設(shè)置用戶口令
passwd?farmhome
5、編輯文件chroot_list:
內(nèi)容為ftp用戶名,每個(gè)用戶占一行,如:
farmhome
6、重新啟動(dòng)vsftpd
service vsftpd restart
7.用xftp等客戶端軟件應(yīng)該就能訪問到該目錄了;
安裝mysql
一、官網(wǎng)下載mysql
# wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm (去mysql官網(wǎng)獲取最新的rpm包 https://dev.mysql.com/downloads/repo/yum/) # rpm -ivh mysql-community-release-el7-5.noarch.rpm # yum install mysql-community-server安裝完成后重啟mysql服務(wù)
service mysqld restart 二、mysql相關(guān)配置初次安裝mysql,root賬戶沒有密碼。
[root@yl-web yl]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.6.26 MySQL Community Server (GPL)Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ rows in set (0.01 sec)mysql>設(shè)置密碼
mysql> set password for 'root'@'localhost' =password('password'); Query OK, 0 rows affected (0.00 sec)mysql>不需要重啟數(shù)據(jù)庫(kù)即可生效。
mysql新建用戶(用戶farmhome,密碼123456):
mysql>create user 'farmhome'@'%' identified by '123';mysql遠(yuǎn)程連接設(shè)定(把在所有數(shù)據(jù)庫(kù)的所有表的所有權(quán)限賦值給位于所有IP地址的root用戶)
1 mysql> grant all privileges on *.* to root@'%'identified by 'password'; ?部署asp.net core
1.防火墻配置
運(yùn)行、停止、禁用firewalld
啟動(dòng):# systemctl start ?firewalld
查看狀態(tài):# systemctl status firewalld?或者?firewall-cmd --state
停止:# systemctl disable firewalld
禁用:# systemctl stop firewalld
將接口添加到區(qū)域,默認(rèn)接口都在public
# firewall-cmd --zone=public --add-interface=eth0?--permanent?
永久生效再加上?--permanent?然后reload防火墻
設(shè)置默認(rèn)接口區(qū)域
# firewall-cmd --set-default-zone=public?--permanent?
立即生效無需重啟
打開端口
加入一個(gè)端口到區(qū)域:
# firewall-cmd --zone=public --add-port=5000/tcp?--permanent?
# firewall-cmd --zone=public --add-port=80/tcp?--permanent?
查看所有打開的端口:
# firewall-cmd --zone=dmz --list-ports
2.用xftp上傳程序到指定目錄即上的
進(jìn)入指定目錄運(yùn)行程序
View Code如出現(xiàn)不能綁定ipv6等錯(cuò)誤時(shí),請(qǐng)修改Program.cs,添加UseUrls("http://0.0.0.0:50");
View Code配置Nginx
安裝Nginx
curl -o? nginx.rpm?http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -ivh nginx.rpm
yum install nginx
安裝成功!
輸入:systemctl start nginx?來啟動(dòng)nginx。
輸入:systemctl enable nginx?來設(shè)置nginx的開機(jī)啟動(dòng)(linux宕機(jī)、重啟會(huì)自動(dòng)運(yùn)行nginx不需要連上去輸入命令)。
測(cè)試nginx是否可以訪問。
配置nginx對(duì)ASP.NET Core應(yīng)用的轉(zhuǎn)發(fā)
修改 ?
View Code將文件內(nèi)容替換為
server {
??? listen 80;
??? 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;
??? }}
再次運(yùn)行程序
donet /home/wwwroot/farmhome/famrhome.dll如出現(xiàn)502錯(cuò)誤時(shí)由于SELinux保護(hù)機(jī)制所導(dǎo)致,我們需要將nginx添加至SELinux的白名單。
接下來我們通過一些命令解決這個(gè)問題:
yum install policycoreutils-pythonsudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginxsudo semodule -i mynginx.pp配置守護(hù)服務(wù)(Supervisor)
目前存在三個(gè)問題
問題1:ASP.NET Core應(yīng)用程序運(yùn)行在shell之中,如果關(guān)閉shell則會(huì)發(fā)現(xiàn)ASP.NET Core應(yīng)用被關(guān)閉,從而導(dǎo)致應(yīng)用無法訪問,這種情況當(dāng)然是我們不想遇到的,而且生產(chǎn)環(huán)境對(duì)這種情況是零容忍的。
問題2:如果ASP.NET Core進(jìn)程意外終止那么需要人為連進(jìn)shell進(jìn)行再次啟動(dòng),往往這種操作都不夠及時(shí)。
問題3:如果服務(wù)器宕機(jī)或需要重啟我們則還是需要連入shell進(jìn)行啟動(dòng)。
為了解決這個(gè)問題,我們需要有一個(gè)程序來監(jiān)聽ASP.NET Core 應(yīng)用程序的狀況。在應(yīng)用程序停止運(yùn)行的時(shí)候立即重新啟動(dòng)。這邊我們用到了Supervisor這個(gè)工具,Supervisor使用Python開發(fā)的。
安裝Supervisor
yum install python-setuptools
easy_install supervisor
配置Supervisor
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
修改supervisord.conf文件,
?
vim /etc/supervisor/supervisord.conf?
將文件尾部的配置(表示加載配置文件目錄下的配置文件)
修改為
ps:如果服務(wù)已啟動(dòng),修改配置文件可用“supervisorctl reload”命令來使其生效
配置對(duì)ASP.NET Core應(yīng)用的守護(hù)
創(chuàng)建一個(gè) farmhomeapp.conf文件
View Code?
,內(nèi)容大致如下
[program:farmhomewebapp] command=dotnet farmhome.dll ; 運(yùn)行程序的命令 directory=/home/wwwroot/farmhome/ ; 命令執(zhí)行的目錄 autorestart=true ; 程序意外退出是否自動(dòng)重啟 stderr_logfile=/var/log/WebApplication1.err.log ; 錯(cuò)誤日志文件 stdout_logfile=/var/log/WebApplication1.out.log ; 輸出日志文件 environment=ASPNETCORE_ENVIRONMENT=Production ; 進(jìn)程環(huán)境變量 user=root ; 進(jìn)程執(zhí)行的用戶身份 stopsignal=INT運(yùn)行supervisord,查看是否生效
supervisord -c /etc/supervisor/supervisord.confps -ef如存在指定進(jìn)程表示成功
至此關(guān)于ASP.NET Core應(yīng)用程序的守護(hù)即配置完成。
配置Supervisor開機(jī)啟動(dòng)
新建一個(gè)“supervisord.service”文件
vim /usr/lib/systemd/system/supervisord.service內(nèi)容如下
# dservice for systemd (CentOS 7.0+) # by ET-CS (https://github.com/ET-CS) [Unit] Description=Supervisor daemon[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執(zhí)行命令:systemctl enable supervisord 設(shè)定開機(jī)啟動(dòng)
執(zhí)行命令:systemctl is-enabled supervisord #來驗(yàn)證是否為開機(jī)啟動(dòng)
本文是環(huán)境搭建完后依據(jù)記憶寫的博客,可能會(huì)有錯(cuò)誤,歡迎指正,不知不覺已經(jīng)凌晨了,明天繼續(xù)上asp.net core2 及EF Core Fluent API 及 EF Core for?MySql 相關(guān)介紹
相關(guān)內(nèi)容參考:
將ASP.NET Core應(yīng)用程序部署至生產(chǎn)環(huán)境中(CentOS7)
?CentOS開啟FTP及配置用戶
centos7 mysql數(shù)據(jù)庫(kù)安裝和配置
轉(zhuǎn)載于:https://www.cnblogs.com/shatanku/p/7434474.html
總結(jié)
以上是生活随笔為你收集整理的一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx、supervisor、mysql环境搭建...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用消息队列实现分布式事务-公认较为理想
- 下一篇: Ubuntu17.04安装WineQQ7