第一步:安裝環境
說明:在安裝這些環境之前你可以先查看一下你有沒有安裝,有則不用再安裝
rpm -qa | grep gcc
一. gcc 安裝
安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:
yum install -y gcc-c++
二. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:
yum install -y pcre pcre-devel
三. zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。
yum install -y zlib zlib-devel
四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不僅支持 http 協議,還支持 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。
yum install -y openssl openssl-devel
第二步:下載並解壓
1.下載nginx
鏈接:https://pan.baidu.com/s/1hnfJfsZaw8Ppn70h0blDtw 提取碼:735v
2.解壓
tar -zxvf nginx-1.18.0.tar.gz -C /usr/local
3.重命名
mv /usr/local/nginx-1.18.0 /usr/local/nginx
第三步:配置
cd /usr/local/nginx 進入到nginx文件夾
./configure 使用默認配置
第四步:編譯安裝
make && make install
第五步:啟動nginx
cd /usr/local/nginx/sbin/ 進入到nginx的sbin目錄
./nginx 啟動nginx
根據報錯信息看到我們沒有文件夾及文件,新建文件夾及文件
mkdir /usr/local/nginx/logs 創建文件夾 touch /usr/local/nginx/logs/error.log 創建文件
touch /usr/local/nginx/logs/access.log 創建文件
ls /usr/local/nginx/logs 查看
1. 啟動nginx
2.查看nginx進程
ps -ef | grep nginx
3.停止nginx
./nginx -s quit: 此方式停止步驟是待nginx進程處理任務完畢進行停止。 ./nginx -s stop: 此方式相當於先查出nginx進程id再使用kill命令強制殺掉進程。
./nginx -s reload 重啟nginx(不推薦此方法,推薦先停止在啟動)
4.重新加載配置文件
當 ngin x的配置文件 nginx.conf 修改后,要想讓配置生效需要重啟 nginx,使用 ./nginx -s reload 不用先停止 nginx再啟動 nginx 即可將配置信息在 nginx 中生效
第六步:開放80端口
前面的文章已經寫了開啟80端口的方式(鏈接跳轉),如果是阿里雲服務器需要在安全組規則開放
訪問ip地址,出現下面圖則代表安裝成功
nginx配置文件 nginx.conf說明
nginx配置文件 nginx.conf說明
#user nobody; worker_processes 1; #工作進程:數目。根據硬件調整,通常等於cpu數量或者2倍cpu數量。 #錯誤日志存放路徑 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; # nginx進程pid存放路徑 events { worker_connections 1024; # 工作進程的最大連接數量 } http { include mime.types; #指定mime類型,由mime.type來定義 default_type application/octet-stream; # 日志格式設置 #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; #用log_format指令設置日志格式后,需要用access_log來指定日志文件存放路徑 sendfile on; #指定nginx是否調用sendfile函數來輸出文件,對於普通應用,必須設置on。 如果用來進行下載等應用磁盤io重負載應用,可設着off,以平衡磁盤與網絡io處理速度,降低系統uptime。 #tcp_nopush on; #此選項允許或禁止使用socket的TCP_CORK的選項,此選項僅在sendfile的時候使用 #keepalive_timeout 0; #keepalive超時時間 keepalive_timeout 65; #gzip on; #開啟gzip壓縮服務 #虛擬主機 server { listen 80; #配置監聽端口號 server_name localhost; #配置訪問域名,域名可以有多個,用空格隔開 #charset koi8-r; #字符集設置 #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #錯誤跳轉頁 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { #請求的url過濾,正則匹配,~為區分大小寫,~*為不區分大小寫。 # root html; #根目錄 # fastcgi_pass 127.0.0.1:9000; #請求轉向定義的服務器列表 # fastcgi_index index.php; # 如果請求的Fastcgi_index URI是以 / 結束的, 該指令設置的文件會被附加到URI的后面並保存在變量$fastcig_script_name中 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; #監聽端口 # server_name localhost; #域名 # ssl_certificate cert.pem; #證書位置 # ssl_certificate_key cert.key; #私鑰位置 # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; #密碼加密方式 # ssl_prefer_server_ciphers on; # ssl_prefer_server_ciphers on; # # location / { # root html; # index index.html index.htm; # } #}