go語言現(xiàn)在越來越流程了,如何從零開始在nginx上面安裝一個(gè)我們自己的web網(wǎng)站程序?
我們創(chuàng)建了一個(gè)簡(jiǎn)單的 Web 應(yīng)用程序,提供“Hello world”路線。
對(duì)于那些被這種與我們常規(guī)編程的偏差所吸引的人來說,您可能會(huì)遇到的下一個(gè)邏輯問題是如何通過讓其他人可以訪問這些知識(shí)來使其變得“有用” 。
我們將繼續(xù)利用上周的 Golang 勢(shì)頭來做到這一點(diǎn):將用 Go 編寫的 Web 應(yīng)用程序部署到 Linux 主機(jī)(例如 Ubuntu)。我們將涵蓋從安裝 Go、創(chuàng)建 systemctl 服務(wù)和配置 Nginx 的所有內(nèi)容。您所需要的只是一個(gè) VPS。
安裝
如果您還沒有這樣做,請(qǐng)確保您的 VPS 已安裝 Nginx。
$ apt update $ apt upgrade -y $ apt install nginx
安裝 Nginx
在 Linux 上安裝 Go
我們將通過源代碼安裝 Go。從Go 下載頁面(https://golang.org/dl/)選擇適合您的 Linux 發(fā)行版需求的 Go 版本。
我們將其下載到/tmp文件夾,構(gòu)建源代碼,并將構(gòu)建的源代碼移動(dòng)到它所屬的位置:
$ cd /tmp $ wget https://dl.google.com/go/go1.14.3.linux-amd64.tar.gz $ tar -xvf go1.11.linux-amd64.tar.gz $ sudo mv go /usr/local
從源代碼構(gòu)建 Go。
我們剛剛解壓了 Go 語言并將其移至 Linux 通常喜歡保留其編程語言的地方。該路徑就是 Go 所說的GOROOT,其內(nèi)容應(yīng)如下所示:
/usr/local/go ├── AUTHORS ├── CONTRIBUTING.md ├── CONTRIBUTORS ├── LICENSE ├── PATENTS ├── README.md ├── SECURITY.md ├── VERSION ├── /api ├── /bin ├── /doc ├── favicon.ico ├── /lib ├── /misc ├── /pkg ├── robots.txt ├── /src └── /test
GOROOT 的內(nèi)容
將 GOPATH 和 GOROOT 添加到您的 Shell 腳本中
我們已經(jīng)安裝并解壓了 Go,但是我們還沒有給我們的操作系統(tǒng)提供一種方法來識(shí)別我們已經(jīng)完成了這些工作。我們可以通過修改 shell 腳本來實(shí)現(xiàn)這一點(diǎn),該腳本通常稱為.profile:
$ vim ~/.profile
編輯~/.profile
在這里,我們將添加GOROOT和GOPATH文件路徑。如果您還記得,GOROOT是我們的操作系統(tǒng)查找 Go 編程語言的地方,GOPATH是我們保存所有 Go 項(xiàng)目和依賴項(xiàng)的工作目錄。我選擇將GOPATH設(shè)置為/go,這是我們尚未創(chuàng)建的目錄:
export GOPATH=/go export GOROOT=/usr/local/go export PATH=$PATH:$GOPATH export PATH=$PATH:$GOROOT/bin
?/.profile
保存更改并激活 shell 腳本:
$ source ~/.profile
激活~/.profile
我們現(xiàn)在可以驗(yàn)證一切是否已安裝:
$ go version >> go version go1.14.3 linux/amd64
驗(yàn)證安裝
設(shè)置我們的 GOPATH 和項(xiàng)目
我們必須手動(dòng)創(chuàng)建GOPATH,就像創(chuàng)建以下目錄一樣簡(jiǎn)單:
$ mkdir ~/go $ mkdir ~/go/bin $ mkdir ~/go/pkg $ mkdir ~/go/src
現(xiàn)在我們有地方保存我們的 Go 項(xiàng)目了!為了方便起見,我將刪除我們上周創(chuàng)建的“Hello world”項(xiàng)目:
$ cd /go $ go get github.com/hackersandslackers/golang-helloworld
獲取 go 項(xiàng)目
我也選擇將 Github 存儲(chǔ)庫(kù)克隆到我的/src路徑中,這使得我的GOPATH結(jié)構(gòu)如下所示:
/go ├── /bin │ └── golang-helloworld ├── /pkg └── /src └── /github.com └── /hackersandslackers └── /golang-helloworld ├── README.md ├── go.mod ├── go.sum ├── golang-helloworld ├── main.go └── main_test.go
GOPATH 的內(nèi)容
創(chuàng)建 Nginx 配置
您以前可能已經(jīng)這樣做過幾次,但無論如何。我們將設(shè)置一個(gè) Ngnix 反向代理來偵聽我們的應(yīng)用程序?qū)⒃谄渖线\(yùn)行的端口,在我們的例子中恰好是端口9100 。當(dāng)然,我們需要先確保該端口已啟用:
$ sudo ufw allow 9100
打開一個(gè)端口
在 Nginx /sites-available文件夾中創(chuàng)建配置文件:
$ sudo vim /etc/nginx/sites-available/golang-helloworld.conf
創(chuàng)建 Nginx 配置
我們將在這里刪除反向代理的標(biāo)準(zhǔn)樣板。我恰好用于此應(yīng)用程序的域是golanghelloworld.hackersandslackers.app。將其替換為您選擇的域:
server { listen 80; listen [::]:80; server_name golanghelloworld.hackersandslackers.app; location / { 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_set_header Host $http_host; proxy_pass http://127.0.0.1:9100; } location ~ /.well-known { allow all; } }
golang-helloworld.conf
我們通過從sites-available到sites-enabled中的文件創(chuàng)建符號(hào)鏈接來激活此配置:
$ sudo ln -s /etc/nginx/sites-available/golang-helloworld.conf /etc/nginx/sites-enabled/golang-helloworld.conf
符號(hào)鏈接 Nginx 配置
最后,這些更改將在 Nginx 重新啟動(dòng)時(shí)應(yīng)用。如果以下命令沒有產(chǎn)生任何輸出,那么您就沒有問題了:
$ sudo service nginx restart
重啟 Nginx
SSL 與 Certbot
添加 SSL 可能超出了本教程的重點(diǎn)范圍,但無論如何。Certbot使添加 SSL 變得非常簡(jiǎn)單,我們可以在不到一分鐘的時(shí)間內(nèi)完成它。
在我們實(shí)際安裝 Certbot 之前,我們需要添加正確的存儲(chǔ)庫(kù):
$ sudo apt-get update $ sudo apt-get install software-properties-common $ sudo add-apt-repository universe $ sudo add-apt-repository ppa:certbot/certbot $ sudo apt-get update
添加 Certbot PPA 存儲(chǔ)庫(kù)
現(xiàn)在我們可以真正安裝 Certbot 了:
$ sudo apt-get install certbot python3-certbot-nginx
安裝證書機(jī)器人
Certbot CLI 能夠接受一個(gè)--nginx標(biāo)志,該標(biāo)志會(huì)掃描我們?cè)谟?jì)算機(jī)上設(shè)置的配置:
$ certbot --nginx
根據(jù)您的 Nginx 配置創(chuàng)建證書。
這將列出您計(jì)算機(jī)上的每個(gè)Nginx 配置,并提示您要使用 SSL 設(shè)置哪個(gè)應(yīng)用程序。我的 Ubuntu 機(jī)器恰好托管著一堆站點(diǎn)。如果您愿意,請(qǐng)隨意查看其中任何一個(gè):
Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator nginx, Installer nginx Which names would you like to activate HTTPS for? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: broiestbro.com 2: www.broiestbro.com 3: consider.pizza 4: stockholm.ghostthemes.io 5: hackersandslackers.app 6: hackersandslackers.tools 7: django.hackersandslackers.app 8: flaskblueprints.hackersandslackers.app 9: flasklogin.hackersandslackers.app 10: flasksession.hackersandslackers.app 11: flasksqlalchemy.hackersandslackers.app 12: flaskwtf.hackersandslackers.app 13: plotlydashflask.hackersandslackers.app 14: www.hackersandslackers.tools 15: hustlers.club 16: www.hustlers.club 17: toddbirchard.app 18: golanghelloworld.hackersandslackers.app - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel):
Certbot CLI
我正在尋找的配置是#18。選擇此選項(xiàng)后將提示我們是否要將 HTTP 流量重定向到 HTTPS,這是您絕對(duì)應(yīng)該做的事情(甚至有理由不這樣做嗎?請(qǐng)?jiān)谙旅娴脑u(píng)論中告訴我,并記得像這樣粉碎它)按鈕)。
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
Certbot CLI
選擇選項(xiàng)2。
查看 Certbot 如何修改我們?cè)瓉淼膅olang-helloworld.conf Nginx 配置:
server { server_name golanghelloworld.hackersandslackers.app; location / { 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_set_header Host $http_host; proxy_pass http://127.0.0.1:9100; } location ~ /.well-known { allow all; } listen [::]:443 ssl; listen 443 ssl; ssl_certificate /etc/letsencrypt/live/golanghelloworld.hackersandslackers.app/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/golanghelloworld.hackersandslackers.app/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; } server { if ($host = golanghelloworld.hackersandslackers.app) { return 301 https://$host$request_uri; } listen 80; listen [::]:80; server_name golanghelloworld.hackersandslackers.app; return 404; }
golang-helloworld.conf
這就是我們喜歡看到的。
創(chuàng)建 Systemctl 服務(wù)
如果您以前從未使用過 Systemctl 服務(wù),那么“服務(wù)”就是我們希望在服務(wù)器上持續(xù)運(yùn)行的東西(例如,Nginx本身就是一個(gè)服務(wù))。我們將為 Go 應(yīng)用程序創(chuàng)建一個(gè)服務(wù),以確保我們的應(yīng)用程序始終運(yùn)行,即使我們的服務(wù)器重新啟動(dòng):
$ vim /etc/systemd/system/golanghelloworld.service
添加服務(wù)
Linux 服務(wù)的語法遵循.ini文件格式。這里有很多內(nèi)容,我們稍后將進(jìn)行剖析:
[Unit] Description=golanghelloworld.hackersandslackers.app ConditionPathExists=/go/src/github.com/hackersandslackers/golang-helloworld After=network.target [Service] Type=simple User=root Group=root WorkingDirectory=/go/src/github.com/hackersandslackers/golang-helloworld ExecStart=/go/src/github.com/hackersandslackers/golang-helloworld/golang-helloworld Restart=on-failure RestartSec=10 ExecStartPre=/bin/mkdir -p /var/log/golang-helloworld ExecStartPre=/bin/chown syslog:adm /var/log/golang-helloworld ExecStartPre=/bin/chmod 775 /go/src/github.com/hackersandslackers/golang-helloworld/golang-helloworld [Install] WantedBy=multi-user.target
golanghelloworld.service
以下是上面設(shè)置的值得注意的值:
User& Group:可能不是最好的主意,但這告訴我們的服務(wù)以 Ubuntu root 用戶身份運(yùn)行我們的應(yīng)用程序。請(qǐng)隨意將其更改為其他 Linux 用戶。
WorkingDirectory:我們將從中提供應(yīng)用程序服務(wù)的工作目錄。
ExecStart:我們的Go項(xiàng)目編譯后的二進(jìn)制文件。
Restart& RestartSec:這些值對(duì)于確保我們的應(yīng)用程序始終運(yùn)行且可用非常有用,即使在因不可預(yù)見的情況崩潰后也是如此。這兩個(gè)值告訴我們的服務(wù)檢查我們的應(yīng)用程序是否每 10 秒運(yùn)行一次。如果應(yīng)用程序恰好關(guān)閉,我們的服務(wù)將重新啟動(dòng)應(yīng)用程序,因此Restart的失敗值。
ExecStartPre:每一行都包含一個(gè)在啟動(dòng)我們的應(yīng)用程序之前運(yùn)行的命令。我們?cè)O(shè)置了三個(gè)這樣的命令:前兩個(gè)確保我們的應(yīng)用程序正確記錄到名為/var/log/golang-helloworld 的目錄。第三個(gè)(也是更重要的)命令設(shè)置 Go 二進(jìn)制文件的權(quán)限。
保存您的服務(wù)文件。下面我們注冊(cè)對(duì)系統(tǒng)服務(wù)所做的更改,啟動(dòng)新服務(wù),并使我們的服務(wù)在系統(tǒng)啟動(dòng)時(shí)運(yùn)行:
$ systemctl daemon-reload $ service golanghelloworld start $ service golanghelloworld enable
啟動(dòng)服務(wù)
讓我們檢查一下事情進(jìn)展如何:
$ service golanghelloworld status
檢查服務(wù)狀態(tài)
如果您非常幸運(yùn),您將看到如下所示的 SUCCESS 輸出:
golanghelloworld.service - golanghelloworld.hackersandslackers.app Loaded: loaded (/etc/systemd/system/golanghelloworld.service; disabled; vendor preset: enabled) Active: active (running) since Fri 2020-05-29 01:57:02 UTC; 4s ago Process: 21454 ExecStartPre=/bin/chmod 775 /go/src/github.com/hackersandslackers/golang-helloworld/golang-helloworld (code=exited, status=0/SUCCESS) Process: 21449 ExecStartPre=/bin/chmod 775 /var/log/golang-helloworld (code=exited, status=0/SUCCESS) Process: 21445 ExecStartPre=/bin/chown syslog:adm /var/log/golang-helloworld (code=exited, status=0/SUCCESS) Process: 21444 ExecStartPre=/bin/mkdir -p /var/log/golang-helloworld (code=exited, status=0/SUCCESS) Main PID: 21455 (golang-hellowor) Tasks: 6 (limit: 4915) CGroup: /system.slice/golanghelloworld.service └─21455 /go/src/github.com/hackersandslackers/golang-helloworld/golang-helloworld
服務(wù)狀態(tài)
調(diào)試 Systemctl 服務(wù)
創(chuàng)建 Linux 服務(wù)的一個(gè)不幸的事實(shí)是,有很多移動(dòng)部件在起作用。我認(rèn)為我從來沒有在第一次嘗試時(shí)就沒有出現(xiàn)某種錯(cuò)誤的新服務(wù)。這些錯(cuò)誤包括權(quán)限錯(cuò)誤、不正確的文件路徑或端口沖突。好消息是,每個(gè)問題都很容易解決。
調(diào)試服務(wù)的第一道防線是journalctl檢查任何服務(wù)的日志輸出:
$ journalctl -u golanghelloworld.service
檢查服務(wù)的日志
調(diào)試問題不可或缺的工具是能夠grep讓進(jìn)程查看它們是否正常運(yùn)行。我們可以按名稱或端口搜索活動(dòng)進(jìn)程。如果輸出端口9100正在使用的journalctl錯(cuò)誤,您可以通過以下方式找到該進(jìn)程:
$ ps aux | grep 9100
通過端口號(hào)搜索進(jìn)程
我們可以殺死任何返回的進(jìn)程kill -9 [PID]。
或者,我們可以通過進(jìn)程名稱 grep 檢查我們的應(yīng)用程序是否正在運(yùn)行:
$ ps aux | grep golang-helloworld
按名稱搜索進(jìn)程
如果需要,我們可以使用 終止該進(jìn)程pkill -9 golang-helloworld。
如果您需要對(duì).service文件進(jìn)行更改,請(qǐng)記住運(yùn)行systemctl daemon-reload以獲取更改,然后service golanghelloworld restart再試一次。
結(jié)束
我相信您會(huì)解決這些問題并成功啟動(dòng)并運(yùn)行您的 Go 應(yīng)用程序。我自己也花了一些時(shí)間,但我那該死的你好世界已經(jīng)建立起來,并在這里生活得很榮耀。文章來源:http://www.zghlxwxcb.cn/article/347.html
文章來源地址http://www.zghlxwxcb.cn/article/347.html
到此這篇關(guān)于在Nginx上部署Golang網(wǎng)站web應(yīng)用程序,的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!