nginx源码编译安装和配置
? Linux系統下Configure命令參數
 configure腳本有大量的命令行選項.對不同的軟件包來說,這些選項可能會有變化,但是許多基本的選項是不會改變的.帶上'-- help'選項執行'configure'腳本可以看到可用的所有選項。
–prefix=PEWFIX
'–prefix’是最常用的選項。制作出的’Makefile’會查看隨此選項傳遞的參數,當一個包在安裝時可以徹底的重新安置他的結構獨立部分。舉一個例子,當安裝一個包,例如說Emacs,下面的命令將會使Emacs Lisp file被安裝到"/opt/gnu/share":
$ ./configure --prefix=/opt/gnu
–exec-prefix=EPREFIX
與’–prefix’選項類似,但是他是用來設置結構倚賴的文件的安裝位置,編譯好的’emacs’二進制文件就是這樣一個問件。如果沒有設置這個選項的話,默認使用的選項值將被設為和’–prefix’選項值一樣。
?一、安裝nginx
安裝分為三部:
1.配置
configure 腳本負責在你使用的系統上準備好軟件的構建環境。確保接下來的構建和安裝過程所需要的依賴準備好,并且搞清楚使用這些依賴需要的東西。
2. 構建
當 configure 配置完畢后,可以使用 make 命令執行構建。這個過程會執行在 Makefile 文件中定義的一系列任務將軟件源代碼編譯成可執行文件。
載的源碼包一般沒有一個最終的 Makefile 文件,一般是一個模塊文件 Makefile.in 文件,然后 configure 根據系統的參數生成一個定制化的 Makefile 文件。
3. 安裝
現在軟件已經被構建好并且可以執行,接下來要做的就是將可執行文件復制到最終的路徑。make install 命令就是將可執行文件、第三方依賴包和文檔復制到正確的路徑。
此段參考:configure、 make、 make install 背后的原理(翻譯) - 知乎
[root@server1 ~]# tar -zxvf nginx-1.20.1.tar.gz [root@server1 ~]# cd nginx-1.20.1 ##注意:需要在此目錄下執行configure [root@server1 nginx-1.20.1]# ./configure --help ##可以看到可用參數[root@server1 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module checking for OS ##安裝模塊+ Linux 3.10.0-957.el7.x86_64 x86_64 checking for C compiler ... not found./configure: error: C compiler cc is not found ##提示沒有下載編輯器,下載編輯器[root@server1 nginx-1.20.1]# yum install gcc -y?
[root@server1 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module ./configure: error: the HTTP rewrite module requires the PCRE library. ##提示沒有庫 You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.[root@server1 nginx-1.20.1]# yum install -y pcre-devel ##下載庫通常為庫名+ -devel?
?同理,提示缺少OpenSSL庫,下載OpenSSL
?
安裝好模塊之后,使用make指令
make指令,是在安裝有GNU Make的計算機上的可執行指令。該指令是讀入一個名為 ’Makefile’的文件,然后執行這個文件中指定的指令。
?
?make完成之后,使用make install 命令將可執行文件、第三方依賴包和文檔復制到正確的路徑
?
?此時安裝完成,可以看到objs目錄下有nginx,conf中放有配置文件 ?
創建軟鏈接到/usr/local/sbin/(這樣在開啟nginx服務的時候就不需要進到目錄下開啟,軟連接的方式可以方便開啟全局nginx)?
?
如果想要使用systemctl 命令需要添加腳本,將nginx放進指定文件中
腳本如下:
[root@server1 nginx-1.20.1]# cd /usr/lib/systemd/system [root@server1 system]# vim nginx.service[Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target[Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true[Install] WantedBy=multi-user.target[root@server1 system]# systemctl daemon-reload ##刷新服務列表二、優化配置
編輯主配置文件,優化nginx工作效率
修改進程數
[root@server1 ~]# cd /usr/local/nginx/conf [root@server1 conf]# vim nginx.conf user nginx; worker_processes 2; ##根據cpu數最優,增加同時工作的進程數 worker_cpu_affinity 01 10;#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;#pid logs/nginx.pid;??
?
修改open files值
可以看到此時的open files數為1024
?我們想將open files值改為65535,需要兩步:
1、修改主配置文件
2、修改文件/etc/security/limits.conf
[root@server1 conf]# vim nginx.conf
??
?[root@server1 conf]# vim /etc/security/limits.conf
??
[root@server1 conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@server1 conf]# nginx -s reload為安全和方便,我們為nginx添加一個用戶
[root@server1 conf]# useradd nginx [root@server1 conf]# usermod -s /sbin/nologin nginx ##一般管理軟件的用戶不需要登陸切換用戶 再次查詢可看到open files 值更改為65535
?
?在主配置文件添加負載均衡器和反向代理
?vim nginx.conf
??
?
在真機中進行地址解析,curl可以看到負載均衡
?
添加備用機
[root@server1 conf]# vim /etc/httpd/conf/httpd.conf ##listen改為8080 [root@server1 conf]# systemctl start httpd [root@server1 conf]# curl localhost:8080 [root@server1 conf]# vim nginx.confhttp {upstream westos {server 172.25.6.2:80;server 172.25.6.3:80;server 172.25.6.1:8080 backup; ##添加備份機}include mime.types;default_type application/octet-stream;[root@server1 conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@server1 conf]# nginx -s reload?關閉server2 server3的httpd服務,可以看到備用機啟用
權重控制
?可以看到訪問server2更多
?固定ip固定負載機
?可以看到ip不變就一直訪問同一個負載機
編譯第三方模塊
[root@server1 ~]# yum install unzip [root@server1 ~]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip[root@server1 nginx-1.20.1]# make clean [root@server1 nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 [root@server1 nginx-1.20.1]# make?隱藏版本號
[root@server1 nginx-1.20.1]# vim src/core/nginx.h
??
?
?關閉debug
[root@server1 nginx-1.20.1]# vim auto/cc/gcc
??
?nginx縮小
??
加入模塊
[root@server1 objs]# cd /usr/local/nginx/sbin [root@server1 sbin]# ls nginx [root@server1 sbin]# mv nginx nginx.old [root@server1 sbin]# cd - /root/nginx-1.20.1/objs [root@server1 objs]# cp nginx /usr/local/nginx/sbin[root@server1 objs]# cd /usr/local/nginx/conf [root@server1 conf]# vim nginx.conf http {upstream westos {#ip_hash;sticky;server 172.25.6.2:80;server 172.25.6.3:80;#server 172.25.6.1:8080 backup;} [root@server1 conf]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@server1 conf]# nginx -s reload??
總結
以上是生活随笔為你收集整理的nginx源码编译安装和配置的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: bp神经网络人口预测 C语言,BP神经网
- 下一篇: PreferenceScreen
