Nginx安裝,開箱即用?

1、官網

2、Nginx版本類型

訪問http://nginx.org/download/獲得以下的網頁html

從上面能夠看出Nginx官網提供了三個類型的版本nginx

  1. Mainline version:Mainline是Nginx目前主力在作的版本,開發版。
  2. Stable version:最新穩定版,生產環境上建議使用的版本。
  3. Legacy versions:遺留的老的穩定版。

咱們安裝的時候注意在官網選擇安裝最近的Stable的版本,這裏咱們選擇的是nginx-1.18.0。web

3、安裝方式

源碼編譯安裝正則表達式

4、安裝Nginx運行所須要的依賴庫

gcc  
pcre  
zlib  
openssl
  • 安裝gcc
    gcc是Linux下的編譯器,它能夠編譯C、C++、Ada、Object C和Java等語言。這裏是nginx編譯須要。
# yum -y install gcc
  • 安裝pcre
    pcre是一個perl庫,Nginx的HTTP模塊使用pcre來解析正則表達式。進行重定向支持。
# yum install -y pcre pcre-devel
  • 安裝zlib
    zlib是一個文件壓縮和解壓縮的庫,Nginx使用zlib對HTTP數據包進行gzip壓縮和解壓。
# yum install -y zlib zlib-devel
  • 安裝openssl
    openssl是一個來進行安全通訊的類庫,Nginx須要依賴openssl來支撐對https的支持。
# yum -y install openssl openssl-devel
  • 查看依賴類庫安裝狀況
# rpm -qa gcc*
gcc-8.3.1-5.el8.0.2.x86_64

# rpm -qa  pcre*
pcre-utf16-8.42-4.el8.x86_64
pcre2-10.32-1.el8.x86_64
pcre-8.42-4.el8.x86_64
pcre-utf32-8.42-4.el8.x86_64
pcre-cpp-8.42-4.el8.x86_64
pcre2-utf32-10.32-1.el8.x86_64
pcre2-devel-10.32-1.el8.x86_64
pcre-devel-8.42-4.el8.x86_64
pcre2-utf16-10.32-1.el8.x86_64

# rpm -qa zlib*
zlib-devel-1.2.11-16.el8_2.x86_64
zlib-1.2.11-16.el8_2.x86_64

# rpm -qa  openssl*
openssl-1.1.1c-15.el8.x86_64
openssl-pkcs11-0.4.10-2.el8.x86_64
openssl-libs-1.1.1c-15.el8.x86_64
openssl-devel-1.1.1c-15.el8.x86_64

5、安裝nginx

  1. 下載Nginx安裝包
# wget http://nginx.org/download/nginx-1.18.0.tar.gz
  1. 解壓Nginx安裝包
# tar -zxvf nginx-1.18.0.tar.gz
  1. 配置並檢查編譯環境(configure)

具體的配置參數看這裏shell

須要強調的參數
--prefix=pathvim

defines a directory that will keep server files. This same directory will also be used for all relative paths set by configure (except for paths to libraries sources) and in the nginx.conf configuration file. It is set to the /usr/local/nginx directory by default.瀏覽器

這是設置Nginx安裝路徑的參數,在nginx.conf配置文件中,配置設置的全部相對路徑(庫源路徑除外)也將使用同一個目錄。
默認設置爲/usr/local/nginx目錄。須要修改Nginx安裝目錄的能夠經過設置這個參數來達到目的。
我這裏使用默認的目錄,不設置該參數。
--with-http_ssl_module安全

enables building a module that adds the HTTPS protocol support to an HTTP server. This module is not built by default. The OpenSSL library is required to build and run this module.運維

添加HTTPS協議支持模塊,該模塊默認不參與編譯構建,須要手動開啓。
因爲目前主流的網站都支持Https協議,因此這裏開啓該模塊。curl

# pwd
/soft/nginx-1.18.0
# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
# ./configure  --with-http_ssl_module

執行./configure *** 命令後終端上會有些輸出。
輸出的信息裏會包含依賴的組件是否完整,若是不完整則須要另行安裝。
輸出的信息裏會包含配置文件目錄信息,日誌文件目錄信息等一些很重要的咱們作運維依賴的信息。

# ./configure  --with-http_ssl_module
# 省略若干行
Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + 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"
  1. 編譯(make)
# make
# 省略若干行
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
	-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
	-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
	-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
	< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/soft/nginx-1.18.0'
  1. 安裝(make install)
# make install
  1. Nginx安裝目錄下啓動Nginx
# cd /usr/local/nginx/
# ls
conf  html  logs  sbin
# sbin/nginx
  1. 測試Nginx啓動
  • 經過curl的方式
# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  • 經過瀏覽器的方式

在瀏覽器地址欄輸入Nginx所在主機的IP,出現以下界面表示安裝啓動成功。
Nginx默認頁面

  1. Nginx的進程
# ps -ef | grep nginx
root      1253     1  0 10:54 ?        00:00:00 nginx: master process ./nginx
nobody    1254  1253  0 10:54 ?        00:00:00 nginx: worker process
root      1267 56707  0 10:54 pts/4    00:00:00 grep nginx

能夠看到Nginx的master和worker進程

  1. 配置Nginx環境變量
# vim /etc/profile
# set nginx environment
export NGINX_HOME=/usr/local/src/nginx
export PATH=${NGINX_HOME}/sbin:$PATH
# source /etc/profile

驗證環境變量是否生效

# pwd
/soft
# nginx -v
nginx version: nginx/1.18.0
  1. nginx 命令行
  • 幫助命令
# nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file
  • 啓動
# nginx
  • 重啓
# nginx -s reload
  • 關閉
# nginx -s stop

或者查進程號以後 kill -9 pid

星辰大海,點滴爲途。

相關文章
相關標籤/搜索