CentOS7編譯安裝nginx

一、環境介紹

VMware虛擬機

新裝centos7

IP地址爲192.168.1.1 

nginx-1.9.9源碼包

防火牆關閉狀態

二、準備工作

yum -y update
yum -y upgrade

三、解決依賴

1、安裝gcc

gcc是linux下的編譯器在此不多做解釋,感興趣的小夥伴可以去查一下相關資料,它可以編譯 C,C++,Ada,Object C和Java等語言。命令:

gcc -v                #查看當前gcc版本
yum -y install gcc    #安裝gcc

2、安裝pcre、pcre-devel

pcre是一個perl庫,包括perl兼容的正則表達式庫,nginx的http模塊使用pcre來解析正則表達式,所以需要安裝pcre庫。

安裝命令:

yum install -y pcre pcre-devel

3、zlib安裝

zlib庫提供了很多種壓縮和解壓縮方式nginx使用zlib對http包的內容進行gzip,所以需要安裝

安裝命令:

yum install -y zlib zlib-devel

4、安裝openssl

openssl是web安全通信的基石,沒有openssl,可以說我們的信息都是在裸奔。。。。。。

安裝命令:

yum install -y openssl openssl-devel

四、下載nginx

下載nginx安裝包

wget http://nginx.org/download/nginx-1.9.9.tar.gz 

解壓安裝包

tar -zxvf  nginx-1.9.9.tar.gz

五、編譯安裝

執行以下三個命令:

./configure
​
make
​
make install

驗證是否安裝成功

[root@localhost nginx]# ll /usr/local/nginx
總用量 4
drwxr-xr-x. 2 root root 4096 2月  26 11:31 conf
drwxr-xr-x. 2 root root   40 2月  26 11:31 html
drwxr-xr-x. 2 root root    6 2月  26 11:31 logs
drwxr-xr-x. 2 root root   36 2月  26 11:31 sbin

六、配置nginx

配置nginx.conf,他所在的目錄是/usr/local/nginx/conf/   如果找不見也可以搜索:find / | grep nginx.conf

[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf

默認爲80端口,不需要更改任何設置就可以訪問演示頁面。

可以按照自己服務器的端口使用情況來進行配置,ESC鍵,wq!強制保存並退出

七、啓動nginx服務

cd /usr/local/nginx/sbin    #切換目錄
./nginx                     #啓動nginx命令
ps -ef | grep nginx         #查看進程

八、訪問網頁驗證

訪問虛擬機IP 192.168.1.1

附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;
#    }
#}
```
​
}

相關命令:

時間太久,不知道之前nginx安裝在哪個目錄,可以用以下方法:

whereis nginx

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章