nginx源码编译安装和配置
? Linux系統(tǒng)下Configure命令參數(shù)
configure腳本有大量的命令行選項(xiàng).對(duì)不同的軟件包來(lái)說(shuō),這些選項(xiàng)可能會(huì)有變化,但是許多基本的選項(xiàng)是不會(huì)改變的.帶上'-- help'選項(xiàng)執(zhí)行'configure'腳本可以看到可用的所有選項(xiàng)。
–prefix=PEWFIX
'–prefix’是最常用的選項(xiàng)。制作出的’Makefile’會(huì)查看隨此選項(xiàng)傳遞的參數(shù),當(dāng)一個(gè)包在安裝時(shí)可以徹底的重新安置他的結(jié)構(gòu)獨(dú)立部分。舉一個(gè)例子,當(dāng)安裝一個(gè)包,例如說(shuō)Emacs,下面的命令將會(huì)使Emacs Lisp file被安裝到"/opt/gnu/share":
$ ./configure --prefix=/opt/gnu
–exec-prefix=EPREFIX
與’–prefix’選項(xiàng)類似,但是他是用來(lái)設(shè)置結(jié)構(gòu)倚賴的文件的安裝位置,編譯好的’emacs’二進(jìn)制文件就是這樣一個(gè)問(wèn)件。如果沒(méi)有設(shè)置這個(gè)選項(xiàng)的話,默認(rèn)使用的選項(xiàng)值將被設(shè)為和’–prefix’選項(xiàng)值一樣。
?一、安裝nginx
安裝分為三部:
1.配置
configure 腳本負(fù)責(zé)在你使用的系統(tǒng)上準(zhǔn)備好軟件的構(gòu)建環(huán)境。確保接下來(lái)的構(gòu)建和安裝過(guò)程所需要的依賴準(zhǔn)備好,并且搞清楚使用這些依賴需要的東西。
2. 構(gòu)建
當(dāng) configure 配置完畢后,可以使用 make 命令執(zhí)行構(gòu)建。這個(gè)過(guò)程會(huì)執(zhí)行在 Makefile 文件中定義的一系列任務(wù)將軟件源代碼編譯成可執(zhí)行文件。
載的源碼包一般沒(méi)有一個(gè)最終的 Makefile 文件,一般是一個(gè)模塊文件 Makefile.in 文件,然后 configure 根據(jù)系統(tǒng)的參數(shù)生成一個(gè)定制化的 Makefile 文件。
3. 安裝
現(xiàn)在軟件已經(jīng)被構(gòu)建好并且可以執(zhí)行,接下來(lái)要做的就是將可執(zhí)行文件復(fù)制到最終的路徑。make install 命令就是將可執(zhí)行文件、第三方依賴包和文檔復(fù)制到正確的路徑。
此段參考:configure、 make、 make install 背后的原理(翻譯) - 知乎
[root@server1 ~]# tar -zxvf nginx-1.20.1.tar.gz [root@server1 ~]# cd nginx-1.20.1 ##注意:需要在此目錄下執(zhí)行configure [root@server1 nginx-1.20.1]# ./configure --help ##可以看到可用參數(shù)[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 ##提示沒(méi)有下載編輯器,下載編輯器[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. ##提示沒(méi)有庫(kù) 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 ##下載庫(kù)通常為庫(kù)名+ -devel?
?同理,提示缺少OpenSSL庫(kù),下載OpenSSL
?
安裝好模塊之后,使用make指令
make指令,是在安裝有GNU Make的計(jì)算機(jī)上的可執(zhí)行指令。該指令是讀入一個(gè)名為 ’Makefile’的文件,然后執(zhí)行這個(gè)文件中指定的指令。
?
?make完成之后,使用make install 命令將可執(zhí)行文件、第三方依賴包和文檔復(fù)制到正確的路徑
?
?此時(shí)安裝完成,可以看到objs目錄下有nginx,conf中放有配置文件 ?
創(chuàng)建軟鏈接到/usr/local/sbin/(這樣在開(kāi)啟nginx服務(wù)的時(shí)候就不需要進(jìn)到目錄下開(kāi)啟,軟連接的方式可以方便開(kāi)啟全局nginx)?
?
如果想要使用systemctl 命令需要添加腳本,將nginx放進(jìn)指定文件中
腳本如下:
[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 ##刷新服務(wù)列表二、優(yōu)化配置
編輯主配置文件,優(yōu)化nginx工作效率
修改進(jìn)程數(shù)
[root@server1 ~]# cd /usr/local/nginx/conf [root@server1 conf]# vim nginx.conf user nginx; worker_processes 2; ##根據(jù)cpu數(shù)最優(yōu),增加同時(shí)工作的進(jìn)程數(shù) 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值
可以看到此時(shí)的open files數(shù)為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為安全和方便,我們?yōu)閚ginx添加一個(gè)用戶
[root@server1 conf]# useradd nginx [root@server1 conf]# usermod -s /sbin/nologin nginx ##一般管理軟件的用戶不需要登陸切換用戶 再次查詢可看到open files 值更改為65535
?
?在主配置文件添加負(fù)載均衡器和反向代理
?vim nginx.conf
??
?
在真機(jī)中進(jìn)行地址解析,curl可以看到負(fù)載均衡
?
添加備用機(jī)
[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; ##添加備份機(jī)}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?關(guān)閉server2 server3的httpd服務(wù),可以看到備用機(jī)啟用
權(quán)重控制
?可以看到訪問(wèn)server2更多
?固定ip固定負(fù)載機(jī)
?可以看到ip不變就一直訪問(wèn)同一個(gè)負(fù)載機(jī)
編譯第三方模塊
[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?隱藏版本號(hào)
[root@server1 nginx-1.20.1]# vim src/core/nginx.h
??
?
?關(guān)閉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??
總結(jié)
以上是生活随笔為你收集整理的nginx源码编译安装和配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: bp神经网络人口预测 C语言,BP神经网
- 下一篇: PreferenceScreen