LNMP平臺搭建之一:nginx編譯安裝

參考博客:https://www.cnblogs.com/zhang-shijie/p/5294162.html   jack.zhangphp

1、環境說明html

  系統環境:centos6.5mysql

  [root@localhost ~]# cat /etc/redhat-release
  CentOS release 6.5 (Final)nginx

  軟件包:c++

  mysql5.7web

  nginx1.15sql

  php5.6vim

  配置阿里雲鏡像倉庫:centos

  curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
  yum makecache

2、nginx編譯安裝緩存

  一、安裝依賴包

  yum -y install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel

  2.下載nginx

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

  3.解壓

1 [root@localhost ~]# tar -zxf nginx-1.15.10.tar.gz 
2 [root@localhost ~]# cd nginx-1.15.10
3 [root@localhost nginx-1.15.10]# ls
4 auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

  4.編譯安裝

[root@localhost nginx-1.15.10]# ./configure --prefix=/usr/local/nginx  --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log  --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock  --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
[root@localhost nginx-1.15.10]# make && make install  

  5.建立用戶和組及相關文件夾及文件夾權限的修改

groupadd nginx
useradd -M -s /sbin/nologin -g nginx nginx
chown -R nginx:nginx /usr/local/nginx
chown -R nginx:nginx /var/log/nginx
chown -R nginx:nginx /var/run/nginx
chown -R nginx:nginx /var/lock/nginx
chown -R nginx:nginx /var/lock/nginx.lock
mkdir -p /var/tmp/nginx/client
chown -R nginx:nginx /var/tmp/nginx

6.運行nginx

[root@localhost nginx]# /usr/local/nginx/sbin/nginx
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -s stop
修改監聽端口位8090
  vim /usr/local/nginx/conf/nginx.conf
    修改:listen 8090;
運行檢查: [root@localhost nginx]#
ps -ef | grep nginx root 4529 1 0 07:06 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 4530 4529 0 07:06 ? 00:00:00 nginx: worker process root 4534 1721 0 07:06 pts/0 00:00:00 grep nginx [root@localhost nginx]# lsof -i:8090 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 4529 root 6u IPv4 28718 0t0 TCP *:8090 (LISTEN) nginx 4530 nginx 6u IPv4 28718 0t0 TCP *:8090 (LISTEN)

  7.查看nginx主進程或者ps

[root@localhost nginx]# cat /var/run/nginx/nginx.pid 
4529

  8.nginx主配置文件

[root@Server1 conf]# grep -v "#" nginx.conf | grep -v  "^$"
  #全局生效,主要設置nginx的啓動用戶/組,啓動的工做進程數量,Nginx的PID路徑,日誌路徑等。
worker_processes  1;   #默認啓動一個工做進程
events {   #events設置快,主要影響nginx服務器與用戶的網絡鏈接,好比是否容許同時接受多個網絡鏈接,使用哪一種事件驅動模型處理請求,每一個工做進程能夠同時支持的最大鏈接數,是否開啓對多工做進程下的網絡鏈接進行序列化等。
    worker_connections  1024;   #設置nginx能夠接受的最大併發,多個進程只和
}
http {   #http塊是Nginx服務器配置中的重要部分,緩存、代理和日誌格式定義等絕大多數功能和第三方模塊均可以在這設置,http塊能夠包含多個server塊,而一個server塊中又能夠包含多個location塊,server塊能夠配置文件引入、MIME-Type定義、日誌自定義、是否啓用sendfile、鏈接超時時間和單個連接的請求上限等。

    include       mime.types;  #文件擴展名與文件類型映射表
    default_type  application/octet-stream; #默認文件類型
    sendfile        on; #是否調用 sendfile 函數(zero copy -->零copy方式)來輸出文件,普通應用打開,能夠大幅提高nginx的讀文件性能,若是服務器是下載的就須要關閉,
    keepalive_timeout  65;  #長鏈接超時時間,單位是秒
    server { #設置一個虛擬機主機,能夠包含本身的全局快,同時也能夠包含多個locating模塊。好比本虛擬機監聽的端口、本虛擬機的名稱和IP配置,多個server 可使用一個端口,好比都使用8090端口提供web服務、
        listen       8090;  #server的全局配置,配置監聽的端口
        server_name  localhost;  #本server的名稱,當訪問此名稱的時候nginx會調用當前serevr內部的配置進程匹配。
        location / {  #location實際上是server的一個指令,爲nginx服務器提供比較多並且靈活的指令,都是在location中提現的,主要是基於nginx接受到的請求字符串,對用戶請求的UIL進行匹配,並對特定的指令進行處理,包括地址重定向、數據緩存和應答控制等功能都是在這部分實現,另外不少第三方模塊的配置也是在location模塊中配置。
            root   html;  #至關於默認頁面的目錄名稱,默認是相對路徑,可使用絕對路徑配置。
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;  #錯誤頁面的文件名稱
        location = /50x.html {  #location處理對應的不一樣錯誤碼的頁面定義到/50x.html,這個跟對應其server中定義的目錄下。
            root   html;   #定義默認頁面所在的目錄
        }
    }
}

  8.2:配置nginx 主進程的啓動用戶和工做進程數:

user  xxxx;   #每一條指令都要以分號結尾
worker_processes  1; #能夠指定啓動的固定nginx進程數,或使用auto,auto是啓動與當前CPU 線程相同的進程數,如CPU是四核八線程的就啓動八個進程的Nginx工做進程。

  8.3綁定Nginx 工做進程到不一樣的CPU上:

  默認Nginx是不進行保定的,綁定並不能是當前nginx進程獨佔以一核心CPU,可是能夠保證此進程不會運行在其餘核心上,這就極大減小了nginx 工做進程在不一樣cpu上的跳轉,減小了CPU對進程的資源分配與回收,所以能夠有效的提高nginx服務器的性能,配置以下:

 

複製代碼
[root@Server1 nginx]# grep process /proc/cpuinfo  | wc -l #確認CPU的核心數量
4
四個線程CPU的配置:
worker_processes  4; 
worker_cpu_affinity 0001 0010 0100 1000;

八個線程CPU的配置:
worker_processes  8; 
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

  8.4PID和錯誤日誌文件路徑:

複製代碼
#pid        logs/nginx.pid;   #能夠指定絕對路徑
#error_log  logs/error.log;  #指定錯誤日誌路徑
#error_log  logs/error.log  notice;  #指定一個日誌記錄級別
#error_log  logs/error.log  info;

支持的日誌級別:
語法:
error_log file [ debug | info | notice | warn | error | crit ]  | [{  debug_core | debug_alloc | debug_mutex | debug_event | debug_http | debug_mail | debug_mysql } ]
日誌級別 = 錯誤日誌級別 | 調試日誌級別; 或者
日誌級別 = 錯誤日誌級別;
錯誤日誌的級別: emerg, alert, crit, error, warn, notic, info, debug, 
調試日誌的級別: debug_core, debug_alloc, debug_mutex, debug_event, debug_http, debug_mail, debug_mysql

error_log 指令的日誌級別配置分爲錯誤日誌級別和調試日誌級別,錯誤日誌只能設置一個級別,並且錯誤日誌必須書寫在調試日誌級別的前面,另外調試日誌能夠設置多個級別,其餘配置方法可能沒法知足需求。

8.5 配置文件的引入:include
複製代碼
include file;  #file是要導入的文件,支持相對路徑,通常在html目錄裏面
導入一個conf文件,並配置不一樣主機名的頁面,編輯nginx.conf主配置文件:
include  /usr/local/nginx/conf.d/samsung.conf;  #在最後一個大括號裏面加入一項,*是導入任何以conf結尾的配置文件

在/usr/local/nginx/conf.d/建立一個samsung.conf,內容以下:
[root@Server1 nginx]# grep -v "#" conf.d/samsung.conf  | grep -v "^$"
    server {
        listen       8090;
        server_name  samsung.chinacloudapp.cn;
        location / {
            root   html;
            index  index1.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
相關文章
相關標籤/搜索