0%

nginx搭建

可直接复制粘贴运行的安装脚本

安装gcc和gcc-c++

1
2
sudo yum -y install gcc
sudo yum -y install gcc-c++

下载pcre

1
2
wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
tar -zxvf pcre-8.41.tar.gz

下载zlib

1
2
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz

pcre和zlib不需要安装,只需下载解压即可。具体说明可见官方文档

下载解压nginx

1
2
3
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar -zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1

配置、编译和安装nginx

先进入到解压后的nginx目录下
–prefix是需要安装nginx的目录
–with-pcre是指定pcre的源文件目录
–with-zlib是指定zlib的源文件目录

1
2
3
./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.41 --with-zlib=/usr/local/src/zlib-1.2.11
make
make install

nginx 常用命令

nginx # 启动nginx
nginx -s reload # 重新载入配置文件
nginx -s reopen # 重启 Nginx
nginx -s stop # 停止 Nginx

nginx命令在nginx安装目录的sbin文件夹下

安装脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash

################################################################################
## ##
## 脚本主要用于安装nginx ##
## 安装目录是/usr/local/nginx/ ##
## ##
################################################################################
sudo yum -y install git
sudo yum -y install gcc
sudo yum -y install gcc-c++
cd /usr/local/src/
sudo wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
sudo tar -zxvf pcre-8.41.tar.gz
sudo wget http://zlib.net/zlib-1.2.11.tar.gz
sudo tar -zxvf zlib-1.2.11.tar.gz
sudo wget http://nginx.org/download/nginx-1.12.1.tar.gz
sudo tar -zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
sudo ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/src/pcre-8.41 --with-zlib=/usr/local/src/zlib-1.2.11
sudo make
sudo make install

常见错误

checking for C compiler … not found
./configure: error: C compiler cc is not found

产生原因:
解决办法: 安装gcc编译套件

1
yum -y install gcc


configure: error: You need a C++ compiler for C++ support.
make[1]: [/usr/local/src/pcre-8.41/Makefile] Error 1
make[1]: Leaving directory `/usr/local/src/nginx-1.12.1’
make:
[build] Error 2

产生原因: 没有安装编译套件
解决办法: 安装gcc-c++编译套件

1
yum -y install gcc-c++


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

产生原因: 配置nginx时未指定pcre所在目录
解决办法: 下载解压pcre,并在配置nginx的./configure时通过–with-pcre指定pcre源码所在目录

1
2
wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
tar -zxvf pcre-8.41.tar.gz


./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using –without-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= option.

产生原因: 配置nginx时未指定zlib源码所在目录
解决办法: 下载解压zlib,并在配置nginx的./configure时通过–with-zlib指定zlib源码所在目录

1
2
wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.gz
tar -zxvf pcre-8.41.tar.gz

nginx进程守护和日志切割脚本