Nginx 是一個高效能的 Web 和反向代理伺服器, 它具有有很多非常優越的特性:
Web伺服器:相比 Apache,Nginx 使用更少的資源,支援更多的並行連線,體現更高的效率,這點使 Nginx 尤其受到虛擬主機提供商的歡迎。能夠支援高達 50,000 個並行連線數的響應,感謝 Nginx 為我們選擇了 epoll and kqueue 作為開發模型.
負載均衡伺服器:Nginx 既可以在內部直接支援 Rails 和 PHP,也可以支援作為 HTTP代理伺服器 對外進行服務。Nginx 用 C 編寫, 不論是系統資源開銷還是 CPU 使用效率都比 Perlbal 要好的多。
郵件代理伺服器: Nginx 同時也是一個非常優秀的郵件代理伺服器(最早開發這個產品的目的之一也是作為郵件代理伺服器),Last.fm 描述了成功並且美妙的使用經驗。
Nginx 安裝非常的簡單,且Nginx 啟動特別容易,並且幾乎可以做到24小時不間斷執行,即使執行數個月也不需要重新啟動。你還能夠在不間斷服務的情況下進行軟體版本的升級。接下來我們要針對於安裝和運維操作進行實戰和分析。
使用CMD命令start命令啟動nginx,再執行nginx,到nginx所在目錄,如果啟動前已經啟動nginx並記錄下pid檔案,會kill指定程序,按照指定設定去啟動nginx
nginx.exe -c conf/nginx.conf
-t:代表測試組態檔語法正確性
nginx.exe -c -t conf\nginx.conf
nginx.exe -s stop
nginx.exe -v
啟動成功後,瀏覽器存取 localhost,即可看到Nginx 歡迎頁
Nginx 的使用比較簡單,就是幾條命令,常用到的命令如下(nginx.exe 可以等價於nginx):
Mac OSX 安裝特別簡單,首先你需要安裝 Brew, 通過 brew 快速安裝 nginx。
brew install nginx
# Updating Homebrew...
# ==> Auto-updated Homebrew!
# Updated 2 taps (homebrew/core, homebrew/cask).
# ==> Updated Formulae
# ==> Installing dependencies for nginx: openssl, pcre
# ==> Installing nginx dependency: openssl
# ==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o_1.high_sierra.bottle.tar.gz
# ######################################################################## 100.0%
# ==> Pouring openssl-1.0.2o_1.high_sierra.bottle.tar.gz
# ==> Caveats
# A CA file has been bootstrapped using certificates from the SystemRoots
# keychain. To add additional certificates (e.g. the certificates added in
# the System keychain), place .pem files in
# /usr/local/etc/openssl/certs
#
# and run
# /usr/local/opt/openssl/bin/c_rehash
#
# This formula is keg-only, which means it was not symlinked into /usr/local,
# because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.
#
# If you need to have this software first in your PATH run:
# echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc
#
# For compilers to find this software you may need to set:
# LDFLAGS: -L/usr/local/opt/openssl/lib
# CPPFLAGS: -I/usr/local/opt/openssl/include
# For pkg-config to find this software you may need to set:
# PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
#
# ==> Summary
# ? /usr/local/Cellar/openssl/1.0.2o_1: 1,791 files, 12.3MB
# ==> Installing nginx dependency: pcre
# ==> Downloading https://homebrew.bintray.com/bottles/pcre-8.42.high_sierra.bottle.tar.gz
# ######################################################################## 100.0%
# ==> Pouring pcre-8.42.high_sierra.bottle.tar.gz
# ? /usr/local/Cellar/pcre/8.42: 204 files, 5.3MB
# ==> Installing nginx
# ==> Downloading https://homebrew.bintray.com/bottles/nginx-1.13.12.high_sierra.bottle.tar.gz
# ######################################################################## 100.0%
# ==> Pouring nginx-1.13.12.high_sierra.bottle.tar.gz
# ==> Caveats
# Docroot is: /usr/local/var/www
#
# The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
# nginx can run without sudo.
#
# nginx will load all files in /usr/local/etc/nginx/servers/.
#
# To have launchd start nginx now and restart at login:
# brew services start nginx
# Or, if you don't wacd /usr/local/Cellar/nginx/1.13.12/n just run:
# cd /usr/local/Cellar/nginx/1.13.12/
注意預設埠不是80檢視確認埠是否被佔用。
brew services start nginx
Nginx可以使用各平臺的預設包來安裝,包括具體的編譯引數資訊。正式開始前,編譯環境gcc g++ 開發庫之類的需要提前裝好,這裡預設你已經裝好。CentOS6.5左右預設這兩個包都沒安裝全,所以這兩個都執行安裝即可。
yum -y install gcc make gcc-c++ wget
yum -y install openssl openssl-devel
prce(重定向支援)和openssl(https支援,如果不需要https可以不安裝。)
yum install -y pcre-devel
yum -y install gcc automake autoconf libtool make
yum -y install gcc make gcc-c++ openssl-devel
如果不安裝會出現安裝報錯誤的話比如:「C compiler cc is not found」,這個就是缺少編譯環境,安裝一下就可以了。
下載Linux版本的地址根頁面:http://nginx.org/download/,如下圖所示。
yum install wget
wget http://nginx.org/download/nginx-1.X.X.tar.gz
tar -zxvf nginx-X.X.X.tar.gz
然後進入目錄編譯安裝,configure引數說明
cd nginx-1.xx
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.34 \
--with-zlib=/usr/local/src/zlib-1.2.8 \
--with-openssl=/usr/local/src/openssl-1.0.1c
./configure --prefix=/你的安裝目錄 --add-module=/第三方模組目錄
kqueue(sun os),epoll (linux kenel 2.6+), rtsig(- 實時訊號)或者/dev/poll(一種類似select的模式,底層實現與SELECT基本相 同,都是採用輪訓方法) SELECT模式將是預設安裝模式
啟動後會進行列印對應的設定資訊
....
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
Requires OpenSSL. On Debian, this is libssl-dev. 開啟HTTP SSL模組,使NGINX可以支援HTTPS請求。這個模組需要已經安裝了OPENSSL,在DEBIAN上是libssl
設定完成之後,使用 make 和 make install 編譯和安裝 nginx。
$ wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
nginx rpm 包實際上安裝的是 nginx 的 yum 源。
$ rpm -ivh nginx-*.rpm
$ yum install nginx
$ firewall-cmd --zone=public --add-port=80/tcp --permanent
$ firewall-cmd --reload
make
make install
採用編譯方式安裝 Nginx, 並將其註冊為 systemd 服務
安裝路徑為:/usr/local/nginx
預設下載安裝 1.16.0 版本
預設安裝 - 執行以下任意命令即可:
curl -o- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/nginx-install.sh | bash
wget -qO- https://gitee.com/turnon/linux-tutorial/raw/master/codes/linux/soft/nginx-install.sh | bash
自定義安裝 - 下載指令碼到本地,並按照以下格式執行:
sh nginx-install.sh [version]
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx -s quit
/usr/local/nginx/sbin/nginx -V
systemctl start nginx.service
systemctl disable nginx.service
systemctl status nginx.service
systemctl list-units --type=service
systemctl restart nginx.service
systemctl enable nginx.service
systemctl is-enabled servicename.service # 查詢服務是否開機啟動
systemctl enable *.service # 開機執行服務
systemctl disable *.service # 取消開機執行
systemctl start *.service # 啟動服務
systemctl stop *.service # 停止服務
systemctl restart *.service # 重啟服務
systemctl reload *.service # 重新載入服務組態檔
systemctl status *.service # 查詢服務執行狀態
systemctl --failed # 顯示啟動失敗的服務
如果通過yum安裝,使用下面命令安裝。
yum remove nginx
編譯安裝,刪除/usr/local/nginx目錄即可,如果設定了自啟動指令碼,也需要刪除。
假如採用傳統請執行一下命令:
systemctl stop firewalld
systemctl mask firewalld
Iptables 是用來設定、維護和檢查 Linux核心 的IP包過濾規則的。
yum install iptables-services
systemctl enable iptables
service iptables restart
本文來自部落格園,作者:洛神灬殤,轉載請註明原文連結:https://www.cnblogs.com/liboware/p/16944218.html,任何足夠先進的科技,都與魔法無異。