在 centos 7 中編譯安裝 nginx

安裝環境爲:最小化安裝的centos7。


開始安裝nginxnginx

1c++

2web

3vim

4centos

5ui

6centos7

7spa

8.net

9rest

10

11

建立羣組

groupadd www

建立一個用戶,不容許登錄和不創主目錄

useradd -s /sbin/nologin -g www -M www

#下載最新版nginx

wget -C https://nginx.org/download/nginx-1.7.8.tar.gz

tar zxvf nginx-1.7.8.tar.gz

#編譯基本能運行的nginx

./configure --user=www --group=www --prefix=/usr/local/nginx --with-https_stub_status_module --with-https_ssl_module --with-https_gzip_static_module

make

make install

 

若是有錯誤提示:

./configure: error: C compiler cc is not found

解決方法:

1

yum install gcc gcc-c++

 

 

若是有錯誤提示:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-https_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.

解決方法:

1

yum install pcre-devel

 

若是有錯誤提示:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using –with-openssl=<path> option.

解決方法:

1

 yum install openssl-devel

以上錯誤提示依次解決後:再一次的運行

 

1

2

3

./configure --user=www --group=www --prefix=/usr/local/nginx --with-https_stub_status_module --with-https_ssl_module --with-https_gzip_static_module

make

make install

編譯參數解釋:

1

2

3

4

5

6

7

8

9

10

11

12

#指定運行權限的用戶

--user=www

#指定運行的權限用戶組

--group=www

#指定安裝路徑

--prefix=/usr/local/nginx

#支持nginx狀態查詢

--with-https_stub_status_module

#開啓ssl支持

--with-https_ssl_module

#開啓GZIP功能

--with-https_gzip_static_module

所以要順利的經過nginx編譯安裝必須安裝的依賴關係有:

1

yum install gc gcc gcc-c++ pcre-devel zlib-devel openssl-devel

 

二、在 centos7 中爲nginx的啓動、重啓、重載配置添加腳本

nginx直接啓動的方法:

1

/usr/local/nginx/sbin/nginx

可是不是很方便,所以使用下面的腳原本控制nginx的啓動關閉重載更加合理一些。

編輯文件:vim /usr/lib/systemd/system/nginx.service 添加下面的腳本,注意路徑 !

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

[Unit]

Description=nginx - high performance web server

Documentation=https://nginx.org/en/docs/

After=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 -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

 

systemctl的一些使用方法:

1

2

3

4

5

6

7

8

9

systemctl is-enabled servicename.service #查詢服務是否開機啓動

systemctl enable xxx.service #開機運行服務

systemctl disable xxx.service #取消開機運行

systemctl start xxx.service #啓動服務

systemctl stop xxx.service #中止服務

systemctl restart xxx.service #重啓服務

systemctl reload xxx.service #從新加載服務配置文件

systemctl status xxx.service #查詢服務運行狀態

systemctl --failed #顯示啓動失敗的服務

所以,添加上面腳本後,centos7 中操做nginx的方法有

1

2

3

4

5

6

7

8

9

systemctl is-enabled nginx.service #查詢nginx是否開機啓動

systemctl enable nginx.service #開機運行nginx

systemctl disable nginx.service #取消開機運行nginx

systemctl start nginx.service #啓動nginx

systemctl stop nginx.service #中止nginx

systemctl restart nginx.service #重啓nginx

systemctl reload nginx.service #從新加載nginx配置文件

systemctl status nginx.service #查詢nginx運行狀態

systemctl --failed #顯示啓動失敗的服務

 

copyright = {
    "做者": "墨衣夜行", 
    "本文連接": "http://my.oschina.net/letao/blog/524487"
}
相關文章
相關標籤/搜索