[CentOS] 結合Nginx部署DotNetCore的demo項目

系統CentOS安裝:
  網上不少教程,很詳細,我就再也不贅述了。在安裝過程當中,須要注意的是設置時區、我的帳戶密碼、root密碼(必定要注意,不然後續很麻煩)、在首次啓動時,須要接受許可。
 
NETCoreSDK安裝:
  參考官方教程便可。https://www.microsoft.com/net/core#centos
 
安裝nginx:
我是按照下面的方式安裝的:
# 一、下載對應當前系統版本的nginx包(package),具體版本根據本身狀況http://nginx.org/packages/
 wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 二、創建nginx的yum倉庫
 rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 三、下載並安裝nginx
 yum install nginx
# 四、啓動nginx服務
 systemctl start nginx 
# 或者 service nginx start命令也能夠
五、配置
默認的配置文件在 /etc/nginx 路徑下,使用該配置已經能夠正確地運行nginx;如須要自定義,修改其下的 nginx.conf 等文件便可。
六、測試
在瀏覽器地址欄中輸入部署nginx環境的機器的IP,若是一切正常,應該能看到以下字樣的內容。
==============
配置Nginx.conf,代理,位於etc/nginx/nginx.conf文件,主要是設置了server節點中的一些東西。別的東西還沒動,若是涉及到多站點部署的話,一些配置仍是須要修改的。
 
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
#若是是多站點配置,須要啓用這個配置,而後在conf.d文件夾下,建立多個配置文件便可。好比www.a.com.conf、www.b.com.conf
    #include /etc/nginx/conf.d/*.conf;

server {
    listen 80;

    #root /usr/share/nginx/html;
    #index index.html index.htm;

    # Make site accessible from http://localhost/
    server_name hwapp.netcore.cn;

    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-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
} 
 
 
配置好上面的nginx.conf後,檢查一下是否正確。
 
[root@localhost /]# whereis nginx
nginx: /usr/sbin/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz

# 檢測配置是否有問題 [root@localhost
/]# /usr/sbin/nginx -t nginx: [emerg] invalid URL prefix in /etc/nginx/nginx.conf:49 nginx: configuration file /etc/nginx/nginx.conf test failed [root@localhost /]# /usr/sbin/nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
#重啓nginx服務, [root@localhost
/]# sudo service nginx restart Redirecting to /bin/systemctl restart nginx.service [root@localhost /]# #或者使用reload [root@localhost /]# /usr/sbin/nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@localhost /]# sudo nginx -s reload [root@localhost /]#
 
遇到的問題:
2016/07/19 22:00:02 [crit] 60088#60088: *24 connect() to 127.0.0.1:5000 failed (13: Permission denied) while connecting to upstream, client: 192.168.74.129, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "192.168.74.129"
通過一番檢查搜索,應該是SeLinux的致使的。能夠選擇一些兩種方式進行:
一、關閉SeLinux,能夠查看如下文章:
  CentOS下查看SeLinux狀態及關閉SeLinux:http://www.hpboys.com/824.html
二、執行下面的命令(我執行的是這個
setsebool -P httpd_can_network_connect 1

 

 
項目發佈:
參考各個命令使用以及runtimes的平臺配置 http://www.cnblogs.com/shanyou/archive/2016/07/04/5636920.html
 
進入項目目錄(跟project.json同級),而後執行命令
dotnet publish -r centos.7-x64
dotnet publish -r centos.7-x64好比:我發佈到CentOS7上,dotnet publish -r centos.7-x64
會在\bin\Debug\netcoreapp1.0中生成publish文件夾,而後把整個文件夾copy到CentOS 你指定的文件夾就能夠了。好比個人是/opt/DotNetCorePublish/DotNetCoreDemo1/publish
[hager@localhost publish]$ dotnet HelloWebApp.dll 
Hosting environment: Production
Content root path: /opt/DotNetCorePublish/HelloWebApp/publish
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
 
注意事項
dotnet run HelloWebApp.dllhttp://www.cnblogs.com/linezero/p/aspnetcoreubuntu.html
 # 個人虛擬機IP,以及nginx配置的代理
192.168.74.129 hwapp.netcore.cn
 
目前須要解決的問題是:(後續解決後,再補充,或者從新寫新的筆記)
一、nginx開啓啓動 ----》 systemctl enable nginx.service 便可 2017-09-06 add
二、netcore項目自運行 ---》安裝supervisor 守護進程便可 2017-09-06 add
 
參考資料:
相關文章
相關標籤/搜索