# Nginx build in SE Linux with MobaXterm 2022-10-24
###### tags: `Nginx` `MobaXterm` `Web server` `Back end` `後端心得`
---
安裝 Nginx 之前先安裝以下套件
Nginx 會用到
> yum -y install pcre-devel gcc zlib-devel openssl-devel lrzsz.x86_64
使用指令在 Linux 虛擬機上比較好操作, 通常不會在虛擬機上裝太多壓縮檔
**使用指令的話可以略過 2. ~ 6. 的步驟**
使用指令安裝 nginx
> yum install nginx
對 bash 可進行設置
> vi /etc/nginx/nginx.conf
nginx 開啟啟動以及自動啟動指令
> systemctl start nginx
> systemctl enable nginx
----
會常用到的指令之一 ↓
[linux 壓縮及解壓縮指令](https://blog.xuite.net/pclin31/twblog/142141892-Linux+%E5%A3%93%E7%B8%AE%E6%8C%87%E4%BB%A4+-+tar)
---
有需要可以在 Nginx 官網下載 stable 壓縮檔
http://nginx.org/
以下文章均參考 https://blog.csdn.net/qq_45273552/article/details/110084915
所建立
----
## 0. MobaXtern 教學
可參考: [MobaXtern 教學](https://blog.csdn.net/xuanying_china/article/details/120080644?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166657756516800182788354%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=166657756516800182788354&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-120080644-null-null.142^v59^control_1,201^v3^control_2&utm_term=mobaxterm&spm=1018.2226.3001.4187)
----
## 1. 使用 SSH session to root
假設有 Linux 虛擬機, 使用 SSH session, 並將 root@192.168.66.16 後, 使用 password 716T 登入
----
## 2. 成功登入後, 假如是從 Nginx 官方網站下載安裝包
將安裝包, nginx stable 的版本拖至 SSH browser(SFTP), 並使用指令 `ls` 查看目錄
接著使用解壓縮命令解壓縮 nginx 安裝包 `tar -xvf nginx-1.22.1.tar.gz`
由於直接安裝在 root 下, 因此會看到三個檔案:
> anaconda-ks.cfg nginx-1.22.1.tar.gz niginx
---
## 3. 移動到 root 底下的 nginx 資料夾
使用指令移動 `cd nginx`
這時使用 `./configure` 會發現, 虛擬機下找不到 ==C compiler==

這時候需要安裝 `gcc` 套件, 用 `cd -` 回到上層 `root@xxxxx` 並使用指令 `yum -y install gcc-c++` 安裝 `gcc`
---
## 4. 在參考該文章的狀況下, 會發現需要 PCRE library
https://blog.csdn.net/qq_45273552/article/details/110084915
完成後再次在 `root@xxxxx` 該層下, 使用指令 `./configure`
會發現多了一行錯誤提示:
```
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
```
用 `cd -` 回到上層 `root@xxxxx`, 一樣需要使用指令 `sudo yum install pcre-devel
` 安裝 PCRE library
參考: https://www.cpming.top/p/nginx-http-rewrite-module-requires-pcre
---
## 5. 完成後使用指令 `./configure` 再檢查一次
使用 `cd nginx` 移動回 nginx 資料夾, 並且使用指令 `./configure` 再檢查一次

發現缺少 http_gzip module
用 `cd -` 回到上層 `root@xxxxx`, 一樣需要使用指令 `sudo yum install zlib-devel
` 安裝 http_gzip module
使用 `cd nginx` 移動回 nginx 資料夾, 並且使用指令 `./configure` 再檢查一次
參考: https://www.zhangbj.com/p/247.html
---
## 6. 在參考該文章 使用 `make` 指令
https://blog.csdn.net/qq_45273552/article/details/110084915
使用 `make` 指令後會看到跑一大串安裝模組的程序, 這時候參考上面那篇文章會發現找不到 sbin, 這是因為我們把 nginx 安裝在 root 底下, 此時用 `nginx -v` 也看不到 nginx 的版本
參考該文章: https://blog.csdn.net/wjkwan/article/details/106710607
發現在 root 下安裝 和 在 usr 內安裝的位置是不一樣的, 並使用組態 Nginx 環境變數
執行 `vi /etc/profile` 命令,`shift + G` 跳到最後,按 `I` 進入編輯模式將下面兩行程式碼新增到最後
```
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
```
執行 `source /etc/profile` 命令重新載入組態檔案, 可以看到 nginx 版本號碼
執行 `ps -ef | grep nginx` 可以查看是否有Nginx處理程序
在 `root@xxxx` 下 nginx 的壓縮檔和解壓縮的資料夾會在 root 使用者的底下, 而一般會以個人使用者底下做使用 `/usr/local/nginx/sbin/`
參考: https://blog.csdn.net/wjkwan/article/details/106710607
[vim 指令](https://ithelp.ithome.com.tw/articles/10230986)
----
## 7. Linux 有兩種系統
RedHat series: RedHat, Centos, Fedora...等
> RedHat series 的管理工具是 yum
Debian series: Debian, Ubuntu...等
> Debian 的管理工具是 apt-get...等
使用指令 `cat /proc/version` 可以看到系統版本
----
## 8. 如果在 root 底下使用 yum 安裝 nginx
在 linux 機器, root 底下使用指令 `sudo yum install sudo yum install nginx` 目錄會指向 ==/usr/local/==
比較完整的安裝教學參考: https://blog.csdn.net/qq_42815754/article/details/82980326
---
## 9. 解決中文編碼問題
使用指令 `sudo yum -y install kde-l10n-Chinese && yum -y reinstall glibc-common`
參考: https://juejin.cn/post/7001150300266430472
---
## 10. 解決防火牆、port 問題
使用指令 `ip addr` 或 `ip a` 去找出虛擬機的 ip
接著用指令 `ping` 去去看連接順利與否
檢查 port: 80 有沒有開啟 `firewall-cmd --query-port=80/tcp`
> 出現 no 表示沒有開啟 port
開啟 port 並且新啟動防火牆
```
firewall-cmd --add-port=80/tcp --permanent
#重新啟動防火墙
systemctl restart firewalld
```
*--permanent 表示永久生效, 沒有此數值, 重新啟動後會失效
參考: https://www.cnblogs.com/xxoome/p/5866475.html
---
## 11. 常用 Linux 指令
參考: https://it.chc.edu.tw/modules/tad_book3/page.php?tbsn=1&tbdsn=2
----
## 12. Nginx 與 php-fpm 運作參考
可以切換 php 版本
https://tec.xenby.com/20-nginx-%E8%88%87-php-fpm-%E9%81%8B%E4%BD%9C%E4%BB%8B%E7%B4%B9%E8%88%87%E8%A8%AD%E5%AE%9A%E8%AC%9B%E8%A7%A3
----
## 13. Nginx 移除教學參考
https://cloud.tencent.com/developer/article/1841908
---
## 14. 反向代理開啟參考:
反向代理定義:
> https://ithelp.ithome.com.tw/articles/10188498
打開網路權限, 並且使用反向代理:
指令使用:
> setsebool -P httpd_can_network_connect 1
參考教學:
https://www.twblogs.net/a/5c966f24bd9eee4a0d091329
---
## 15. 其他參考網站
[網站一條龍 - 從架站到前端 ](https://ithelp.ithome.com.tw/articles/10263854?sc=iThomeR)
[常用的 Nginx Config 與相關指令教學](https://linyencheng.github.io/2019/07/13/tool-nginx/)
[前端工程師一起來種一棵後端技能樹吧](https://ithelp.ithome.com.tw/articles/10241354)
[[基礎觀念系列] Web Server & Nginx](https://medium.com/starbugs/web-server-nginx-2-bc41c6268646)
[負載均衡器技術Nginx和F5的優缺點對比](https://www.itdaan.com/tw/9461b6629f90f200469221b245555905#:~:text=1%E3%80%81nginx%E6%87%89%E7%94%A8%20nginx%20%EF%BC%88engine,x%EF%BC%89%E6%98%AF%E4%B8%80%E5%80%8B%E5%8F%AF%E4%BB%A5%E4%BD%9C%E7%82%BAHTTP%20WEB%E6%9C%8D%E5%8B%99%E5%99%A8%E3%80%81%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8B%99%E5%99%A8%E3%80%81%E9%83%B5%E4%BB%B6%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8B%99%E5%99%A8%E5%92%8C%E4%B8%80%E5%80%8B%E9%80%9A%E7%94%A8%E7%9A%84TCP%20%2F%20UDP%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8B%99%E5%99%A8%EF%BC%881.9.0%E7%89%88%E6%9C%AC%E5%90%8E%EF%BC%89%E7%9A%84%E5%A4%9A%E5%8A%9F%E8%83%BD%E6%9E%B6%E6%A7%8B%E7%B5%84%E4%BB%B6%EF%BC%8C%E5%90%8C%E6%99%82%E4%B9%9F%E5%8F%AF%E4%BB%A5%E6%8F%90%E4%BE%9B%E4%B8%80%E5%AE%9A%E7%9A%84%E7%B7%A9%E5%AD%98%E6%9C%8D%E5%8B%99%E5%8A%9F%E8%83%BD%E3%80%82)
[第八章 TCP Socket 程式介面](http://tsnien.idv.tw/Internet_WebBook/chap8/8-1%20Socket%20%E7%B0%A1%E4%BB%8B.html)
----