Nginx是一款輕量級的 Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個 BSD-like 協議下發行。其特點是佔有內存少,併發能力強等。
1. 安裝前
因爲nginx是c編寫的需要c的lib庫,g++、gcc、openssl-devel、pcre-devel和zlib-devel等
- gcc:安裝nginx需要先將官網下載的源碼進行編譯,編譯依賴gcc環境,如果沒有gcc環境,需要安裝gcc。
$ yum install gcc-c++
- PCRE:PCRE(Perl Compatible Regular Expressions)是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫。
$ yum install -y pcre pcre-devel
- zlib:zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
$ yum install -y zlib zlib-devel
- OpenSSL:OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。
nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝OpenSSL庫。$ yum install -y openssl openssl-devel
2. 安裝
- 下載
官網下載
或者$ wget http://nginx.org/download/nginx-1.16.1.tar.gz
- 解壓nginx壓縮包
此時會產生一個nginx-1.16.1 目錄,這時進入nginx-1.16.1 目錄$ tar -zxvf nginx-1.16.1.tar.gz
接下來安裝,使用–prefix參數指定nginx安裝的目錄,make、make install安裝$ cd nginx-1.16.1
如果沒有報錯,順利完成後,最好看一下nginx的安裝目錄#默認安裝在/usr/local/nginx $ ./configure & make & make install $ make $ make install # 跑出來的信息 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"
此時進入/usr/local/nginx/sbin目錄,執行命令,啓動nginx$ whereis nginx
其它常用命令$ ./nginx
$ ./nginx -v # 查看 Nginx 版本 $ ./nginx -s reload # 重新載入配置文件 $ ./nginx -s reopen # 重啓 Nginx $ ./nginx -s stop # 停止 Nginx $ ./nginx -t # 檢查配置文件nginx.conf的正確性
現在在瀏覽器的地址欄輸入ip地址,就會出現nginx的標誌啦。