編譯安裝Nginx 1.18.0

#編譯安裝 Nginx1.18.0

CentOS平臺編譯環境:

安裝make:

yum -y install gcc automake autoconf libtool make

安裝g++:

yum install gcc gcc-c++

sudo yum install libxslt*

sudo yum install libxml2*

sudo yum -y install gd-devel

sudo yum -y install perl-devel perl-ExtUtils-Embed

sudo yum -y install GeoIP GeoIP-devel GeoIP-data

Debian & ubuntu平臺編譯環境:

本文描述Nginx 的源碼編譯安裝過程

apt-get install build-essential g++ gcc make libtool openssl libpcre3 libpcre3-dev openssl libssl-dev libperl-dev automake

apt-get install libgd2-xpm*

apt-get install libxslt*

apt-get install libxml2*

apt-get install libgeoip-dev

############## 一、安裝OpenSSL ######################

 

下載地址 https://www.openssl.org/source/

//wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz

wget https://www.openssl.org/source/openssl-1.1.0k.tar.gz

tar zxvf openssl-1.1.0k.tar.gz

cd openssl-1.1.0h/

./config

make && sudo make install

 

################ 二、安裝PCRE ########################

下載地址 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

//wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.42.tar.gz

wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz

tar zxvf pcre-8.43.tar.gz

./configure

make && sudo make install

 

################ 二、安裝zlib ########################

wget https://nchc.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz

tar zxvf zlib-1.2.11.tar.gz

./configure

make && sudo make install

 

################ 三、安裝Nginx ########################

 

下載Nginx源碼包(stable version)

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

 

解壓縮

tar zxvf nginx-1.18.0.tar.gz

 

cd nginx-1.18.0/

 

配置編譯環境(--with-pcre、--with-openssl 的路徑是源碼路徑,pcre、openssl的安裝路徑在/opt/local,但這裏只需要源碼路徑)

 

./configure --prefix=/opt/nginx \

--with-select_module \

--with-poll_module \

--with-threads \

--with-file-aio \

--with-http_ssl_module \

--with-http_v2_module \

--with-http_realip_module \

--with-http_addition_module \

--with-http_xslt_module \

--with-http_xslt_module=dynamic \

--with-http_image_filter_module \

--with-http_image_filter_module=dynamic \

--with-http_geoip_module \

--with-http_geoip_module=dynamic \

--with-http_sub_module \

--with-http_dav_module \

--with-http_flv_module \

--with-http_mp4_module \

--with-http_gunzip_module \

--with-http_gzip_static_module \

--with-http_auth_request_module \

--with-http_random_index_module \

--with-http_secure_link_module \

--with-http_degradation_module \

--with-http_slice_module \

--with-http_stub_status_module \

--with-stream \

--with-stream=dynamic \

--with-stream_ssl_module \

--with-stream_realip_module \

--with-stream_geoip_module \

--with-stream_geoip_module=dynamic \

--with-stream_ssl_preread_module \

--with-compat \

--with-pcre \

--with-pcre=../pcre-8.43 \

--with-zlib=../zlib-1.2.11 \

--with-openssl=../openssl-1.1.0k

 

make && sudo make install

#################### 四、服務管理方法 ###################

 

1.賦予普通用戶操作權限

sudo chmod u+s /opt/nginx/sbin/nginx

chown root:nginx /opt/nginx/sbin/nginx

2.查看幫助

$ /opt/nginx/sbin/nginx -h

3.檢查配置文件正確性

$ /opt/nginx/sbin/nginx -t

4.查看進程號

cat /opt/nginx/logs/nginx.pid

23214

ps -ef | grep "nginx: master process" | grep -v 'grep' | awk -F ' ' '{print $2}'

23214

5.重新加載配置

sudo kill -HUP `cat /opt/nginx/logs/nginx.pid`

 

6.停止nginx的兩種方式:

 

(1)發送停止信號

/opt/local/nginx/sbin/nginx -s stop

處理完請求後,關閉進程

(2)kill -QUIT ` cat /opt/local/nginx/logs/nginx.pid `

 

前提

如果你通過其它方法將nginx執行路徑改在其它地方,如 /opt/guiji/nginx/sbin/,那麼你需要修正下面的腳本

停止

如果你的nginx正在運行,你可以通過如下命令停止

 

Init腳本

vi /etc/init.d/nginx

 

#! /bin/bash

### BEGIN INIT INFO

# Provides: nginx

# Required-Start: $all

# Required-Stop: $all

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: starts the nginx web server

# Description: starts nginx using start-stop-daemon

### END INIT INFO

 

 

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DAEMON=/opt/nginx/sbin/nginx

NAME=nginx

DESC=nginx

 

test -x $DAEMON || exit 0

 

# Include nginx defaults if available

if [ -f /etc/default/nginx ] ; then

. /etc/default/nginx

fi

 

set -e

 

case "$1" in

start)

echo -n "Starting $DESC: "

start-stop-daemon --start --quiet --pidfile /opt/guiji/nginx/$NAME.pid \

--exec $DAEMON -- $DAEMON_OPTS

echo "$NAME."

;;

stop)

echo -n "Stopping $DESC: "

start-stop-daemon --stop --quiet --pidfile /opt/nginx/$NAME.pid \

--exec $DAEMON

echo "$NAME."

;;

restart|force-reload)

echo -n "Restarting $DESC: "

start-stop-daemon --stop --quiet --pidfile \

/opt/nginx/$NAME.pid --exec $DAEMON

sleep 1

start-stop-daemon --start --quiet --pidfile \

/opt/nginx/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS

echo "$NAME."

;;

reload)

echo -n "Reloading $DESC configuration: "

start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/$NAME.pid \

--exec $DAEMON

echo "$NAME."

;;

*)

N=/etc/init.d/$NAME

echo "Usage: $N {start|stop|restart|force-reload}" >&2

exit 1

;;

esac

 

exit 0

 

當然你也可以通過附件下載腳本

執行

此腳本爲shell命令,所以要先有可執行權限

chmod +x /etc/init.d/nginx

update-rc

腳本準備好後,我們把它加到默認運行級別

/usr/sbin/update-rc.d -f nginx defaults

開始,停止,重啓

/etc/init.d/nginx start/etc/init.d/nginx stop

/etc/init.d/nginx restart

 

腳本在系統啓動時會自動執行start操作

 

==================================================================================

普通用戶在restart和reload nginx時,會報錯:

nginx:[warn] the "user" directive makes sense only if the master processruns with super-user privileges, ignored in/opt/nginx/conf/nginx.conf:2

 

原因是:默認情況下linux的1024以下端口是隻有root用戶纔有權限佔用,解決方法如下:

 

方法一:

所有用戶都可以運行(因爲是755權限,文件所有者:root,組所有者:root)

chown root:root nginx

chmod 755 nginx

chmod u+s nginx

 

方法二:

僅 root 用戶和 test 用戶可以運行(因爲是750權限,文件所有者:root,組所有者:test)

chown root:nginx nginx

chmod 750 nginx

chmod u+s nginx

 

chmod u+s 就是給某個程序的所有者以suid權限,可以像root用戶一樣操作。

 

Set uid, gid,sticky bit的三個權限的詳細說明:

一個文件都有一個所有者, 表示該文件是誰創建的. 同時, 該文件還有一個組編號, 表示該文件所屬的組, 一般爲文件所有者所屬的組.

如果是一個可執行文件, 那麼在執行時, 一般該文件只擁有調用該文件的用戶具有的權限. 而setuid, setgid 可以來改變這種設置.

setuid: 設置使文件在執行階段具有文件所有者的權限. 典型的文件是 /usr/bin/passwd. 如果一般用戶執行該文件, 則在執行過程中, 該文件可以獲得root權

限, 從而可以更改用戶的密碼.

setgid: 該權限只對目錄有效. 目錄被設置該位後, 任何用戶在此目錄下創建的文件都具有和該目錄所屬的組相同的組.

sticky bit: 該位可以理解爲防刪除位. 一個文件是否可以被某用戶刪除, 主要取決於

該文件所屬的組是否對該用戶具有寫權限. 如果沒有寫權限, 則這個目錄下的所有文件都不能被刪除, 同時也不能添加新的文件. 如果希望用戶能夠添加文件

但同時不能刪除文件, 則可以對文件使用sticky bit位. 設置該位後, 就算用戶對目錄

具有寫權限, 也不能刪除該文件.

下面說一下如何操作這些標誌:

操作這些標誌與操作文件權限的命令是一樣的, 都是 chmod. 有兩種方法來操作,

1) chmod u+s temp —爲temp文件加上setuid標誌. (setuid 只對文件有效)

chmod g+s tempdir —爲tempdir目錄加上setgid標誌 (setgid 只對目錄有效)

chmod o+t temp —爲temp文件加上sticky標誌 (sticky只對文件有效)

2) 採用八進制方式. 對一般文件通過三組八進制數字來置標誌, 如 666, 777, 644等. 如果設置這些特殊標誌, 則在這組數字之外外加一組八進制數字. 如

4666, 2777等. 這一組八進制數字三位的意義如下,

abc

a - setuid位, 如果該位爲1, 則表示設置setuid

b - setgid位, 如果該位爲1, 則表示設置setgid

c - sticky位, 如果該位爲1, 則表示設置sticky

設置完這些標誌後, 可以用 ls -l 來查看. 如果有這些標誌, 則會在原來的執行標誌位置上顯示. 如

rwsrw-r– 表示有setuid標誌

rwxrwsrw- 表示有setgid標誌

rwxrw-rwt 表示有sticky標誌

那麼原來的執行標誌x到哪裏去了呢? 系統是這樣規定的, 如果本來在該位上有x, 則這些特殊標誌顯示爲小寫字母 (s, s,t

==================================================================================

###CA根證書的生成:

生成CA私鑰(.key)–>生成CA證書請求(.csr)–>自簽名得到根證書(.crt)(CA給自已頒發的證書)。

 

# Generate CA private key (生成私鑰)

openssl genrsa -out ca.key 2048

# Generate CSR (生成證書籤發請求)

openssl req -new -key ca.key -out ca.csr

# Generate Self Signed certificate(生成CA根證書)

openssl x509 -req -days 3650 -in ca.csr -signkey ca.key -out ca.crt

 

#“KEY文件+CRT文件”轉換爲標準的PEM文件:

cat ca.key ca.crt > ca.pem

 

配置示例:

server {

listen 443 ssl;

server_name localhost;

 

ssl on;

ssl_certificate ca.pem;

ssl_certificate_key ca.key;

ssl_session_timeout 5m;

 

==============================================================================

 

###生成根證書

####a).生成根證書私鑰(pem文件)

 

OpenSSL通常使用PEM(Privacy Enbanced Mail)格式來保存私鑰,構建私鑰的命令如下:

 

openssl genrsa -aes256 -out cakey.pem 2048

 

該命含義如下:

genrsa——使用RSA算法產生私鑰

-aes256——使用256位密鑰的AES算法對私鑰進行加密

-out——輸出文件的路徑

2048——指定私鑰長度

 

####b).生成根證書籤發申請文件(csr文件)

 

使用上一步生成的私鑰(pem文件),生成證書請求文件(csr文件):

 

openssl req -new -key cakey.pem -out ca.csr -subj \

"/C=CN/ST=jiangsu/L=nanjing/O=guiji/OU=guiji/CN=*.guiji.ai"

 

該命令含義如下:

req——執行證書籤發命令

-new——新證書籤發請求

-key——指定私鑰路徑

-out——輸出的csr文件的路徑

-subj——證書相關的用戶信息(subject的縮寫)

 

####c).自簽發根證書(cer文件)

 

csr文件生成以後,可以將其發送給CA認證機構進行簽發,當然,這裏我們使用OpenSSL對該證書進行自簽發:

 

openssl x509 -req -days 365 -sha1 -extensions v3_ca -signkey \

cakey.pem -in ca.csr -out ca.cer

 

該命令的含義如下:

 

x509——生成x509格式證書

-req——輸入csr文件

-days——證書的有效期(天)

-sha1——證書摘要採用sha1算法

-extensions——按照openssl.cnf文件中配置的v3_ca項添加擴展

-signkey——簽發證書的私鑰

-in——要輸入的csr文件

-out——輸出的cer證書文件

 

==================================================================================

###NGINX加載動態模塊:

 

load_module "modules/ngx_rtmp_module.so";

load_module "modules/ngx_stream_module.so";

==================================================================================

#Docker安裝nginx1.18.0

docker pull nginx:stable-alpine

docker pull nginx:1.18.0-alpine

mkdir -p /opt/nginx/{conf,conf.d,html,logs}

docker run --name nginx -d -p 80:80 -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf.d:/etc/nginx/conf.d -v /opt/nginx/logs:/var/log/nginx nginx:stable-alpine

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章