**⭐️⭐️⭐️ Nginx **
官网地址:http://nginx.org/
环境说明
1
2
3
4
5
6
|
ubuntu@VM-0-12-ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic
|
项目背景
Nginx 一般有3个版本:
- Mainline version: 是Nginx目前主力研发的版本。
- Stable version: 是最新的稳定版本,主要用在生产环境。
- Legacy version: 稳定的老版本。
项目安装
依赖安装
- 源码编译依赖 **gcc 环境,需要安装 gcc、g++**包。
- 部分 Nginx 模块依赖 zlib、pcre、openssl库,需要安装。
⭐️ 以为安装过程需要root权限,所以选择root用户安装。
升级apt-get软件包
1
|
sudo apt-get update && sudo apt-get upgrade
|
安装nginx的依赖包
** zlib pcre openssl**(可以源码安装也可以直接系统安装)
1
|
sudo apt-get install libpcre3 libpcre3-dev zlib1g-dev libssl-dev build-essential
|
下载安装 openssl 源码包
1
2
3
4
5
6
7
8
9
|
# 下载
wget http://www.openssl.org/source/openssl-1.0.2a.tar.gz
# 解压
sudo tar -zxvf openssl-1.0.2a.tar.gz -C /usr/local/src/
cd /usr/local/src/openssl-1.0.2a/
# 配置
sudo ./config
# 编译安装
sudo make && sudo make install
|
下载Nginx源码包
1
2
3
4
5
6
7
8
9
10
|
# 下载版本与地址参考官网:http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.10.3.tar.gz
# 解压
sudo tar -zxvf nginx-1.10.3.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.10.3
# 配置
sudo ./configure --prefix=/usr/local/nginx --with-openssl=/usr/include/openssl
# 编译安装
sudo make && sudo make install
|
错误排查

1
2
3
4
5
|
objs/Makefile:458: recipe for target 'objs/src/core/ngx_murmurhash.o' failed
make[1]: *** [objs/src/core/ngx_murmurhash.o] Error 1
make[1]: Leaving directory '/usr/local/src/nginx-1.10.3'
Makefile:8: recipe for target 'build' failed
make: *** [build] Error 2
|
⭐️ ⭐️ 将对应的makefile文件夹中(如本文中在 /nginx-1.10.3/objs/Makefile) 找到 -Werror 并去掉 在重新make即可
查了-Werror意思之后 发现原来它要求GCC将所有的警告当成错误进行处理 所有导致错误输出 并不能进行下一步
配置nginx服务
设置个nginx系统服务,可以通过**service nginx {start|stop|restart|status} **操作。
同时配置 systemctl 管理 systemctl {start|stop|restart|status} nginx
添加环境变量(软连接)
1
|
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
|
创建服务脚本
目录: /etc/init.d/nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### 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
# Author: licess
# website: http://www.bt.cn
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
ulimit -n 8192
case "$1" in
start)
echo -n "Starting $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" = '' ];then
echo "$NAME is not running."
exit 1
fi
else
echo "$NAME is not running."
exit 1
fi
$NGINX_BIN -s stop
if [ "$?" != 0 ] ; then
echo " failed. Use force-quit"
exit 1
else
echo " done"
fi
;;
status)
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
else
echo "$NAME is stopped"
exit 0
fi
else
echo "$NAME is stopped"
exit 0
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
else
echo "$NAME is not running, can't reload."
exit 1
fi
;;
configtest)
echo -n "Test $NAME configure files... "
$NGINX_BIN -t
;;
*)
echo "Usage: $0 {start|stop|restart|reload|status|configtest}"
exit 1
;;
esac
|
重新加载 systemctl 配置
1
|
sudo systemctl daemon-reload
|
添加开机启动
1
|
sudo systemctl enable nginx
|
指定用户和用户组
创建用户和用户组
⭐️ 注意:用户要设置为不能登陆
1
2
3
4
|
# 创建用户组
sudo groupadd www-data
# 创建用户,归属 www-data 用户组
sudo useradd -s /sbin/nologin -g www-data www-data
|
修改nginx的用户
1
2
3
4
5
6
7
|
sudo vim /usr/local/nginx/conf/nginx.conf
# 修改下面的值
user www-data;
# 保存后reload nginx 即可
sudo systemctl reload nginx
|