1厌杜、編譯安裝
1.1疤孕、安裝編譯工具
首先需要安裝各種必須工具:
? GCC
? GCC-C++
? Autoconf
? automake
[root@myhost /]# yum install gcc gcc-c++ autoconf automake
還需要安裝一些專用的庫:
? 支持gzip功能的:zlib庫
? rewrite模塊:pcre庫
? ssl功能:openssl庫
[root@myhost /]# yum install zlib zlib-devel pcre pcre-devel openssl openssl-devel
1.2屿聋、開始安裝
安裝時(shí),首先需要從Nginx網(wǎng)站下載Nginx安裝包,比如本例:nginx-1.11.3.tar.gz
該安裝包是源碼包掂恕,需要本地編譯才可以安裝運(yùn)行蹭越。
將安裝源碼包解壓到某個(gè)目錄下:
[root@myhost server]# tar –zxvf nginx-1.11.3.tar.gz
然后開始編譯Nginx源碼障本,進(jìn)入nginx目錄,然后執(zhí)行三步編譯:
[root@myhost nginx-1.11.3]# ./configure
checking for OS
+ Linux 3.10.0-327.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
checking for gcc -pipe switch ... found
… …
[root@myhost nginx-1.11.3]# make
make -f objs/Makefile
make[1]: 進(jìn)入目錄“/usr/server/nginx-1.11.3”
cc -c -pipe? -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g? -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
… …
[root@myhost nginx-1.11.3]# make install
make -f objs/Makefile install
make[1]: 進(jìn)入目錄“/usr/server/nginx-1.11.3”
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
test -d '/usr/local/nginx/sbin' \
|| mkdir -p '/usr/local/nginx/sbin'
… …
編譯安裝之后响鹃,Nginx將會(huì)被默認(rèn)安裝到路徑“/usr/local/nginx”下驾霜。
檢查是否安裝成功,首先進(jìn)入/usr/local/nginx/sbin目錄下买置,執(zhí)行:
[root@myhost sbin]# ./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
若出現(xiàn)如上提示粪糙,則代表安裝成功。
1.3忿项、開機(jī)自啟動(dòng)
需要寫一個(gè)服務(wù)腳本蓉冈,將Nginx作為CentOS的服務(wù),隨開機(jī)自行啟動(dòng)轩触。
在“/lib/systemd/system”中創(chuàng)建文件nginx.service:
[root@myhost /]# vi /lib/systemd/system/nginx.service
然后將如下內(nèi)容根據(jù)具體情況進(jìn)行修改后寞酿,添加到nginx.service文件中:
[Unit]
Description=nginx1.11.3
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
使配置生效:
[root@myhost /]# systemctl enable nginx.service
然后重啟linux系統(tǒng),或者徹底殺死Nginx
[root@myhost /]# pkill -9 nginx
最終執(zhí)行自動(dòng)啟動(dòng)任務(wù)
[root@myhost /]# systemctl start nginx.service