Hexo

点滴积累 豁达处之

0%

Linux下安装Nginx并配置一个图片服务器

本文采用二进制安装nginx并配置图片服务

1 安装必须的库

1
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

2 安装 PCRE

1
2
3
4
5
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install

若没有安装则在安装可能出现如下错误

若出现缺少依赖包则先安装依赖包,以下纯净centos mini版碰到的两个依赖包问题

1
./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --withou-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
出现上面这个执行 yum -y install pcre-devel 安装依赖, 
1
./configure: error: the HTTP gzip module requires the zlib library. You can either disable the module by using --withou-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option

​ 出现这个yum install -y zlib-devel 安装依赖,

3 安装nginx

1
2
3
4
5
6
7
8
##获取nginx最新的安装包
wget http://nginx.org/download/nginx-1.11.10.tar.gz
##解压缩
tar zxvf nginx-1.11.10.tar.gz
##进入目录
cd nginx-1.11.10
##检测系统配置, 生成make相关文件
./configure --prefix=/usr/local/nginx ## --prefix 指定安装目录

编译并安装

  1. make && make install
    
    1
    2
    3
    4
    5

    ### 4 创建nginx启动命令脚本

    ```properties
    vi /etc/init.d/nginx

插入以下内容, 注意修改PATH和NAME字段, 匹配自己的安装路径

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
#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0

5 设置执行权限

1
chmod a+x /etc/init.d/nginx

6 修改配置文件nginx.conf

1 添加pid文件路径

1
2
3
在配置文件中有个注释的地方: #pid        logs/nginx.pid;

将注释放开,并修改为:pid /usr/local/nginx/logs/nginx.pid;

添加location并配置路径

1
2
3
4
location /images/ {
root /home/ftpadmin/health/;
autoindex on;
}

说明: 重启nginx会报错: nginx: [error] open() “/var/run/nginx/nginx.pid” failed (2: No such file or directory) 。配置后会生成pid文件

7 启动

在/usr/local/nginx文件夹下执行

1
/sbin/nginx -c /usr/local/nginx/conf/nginx.conf。  

8 设置开机启动

注册成服务

1
chkconfig --add nginx

设置开机启动

1
2
3
4
5
6
7
8
chkconfig nginx on

## 查看
chkconfig --list

## 重启, 查看nginx服务是否自动启动
shutdown -h 0 -r
netstat -apn|grep nginx

9 nginx服务执行操作

1
2
3
4
5
6
7
8
#启动nginx服务
systemctl start nginx.service
#停止nginx服务
systemctl stop nginx.service
#重启nginx服务
systemctl restart nginx.service
#重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
systemctl reload nginx.service