Centos7 安装nginx1.14

一丶官网 http://nginx.org/en/download.html 至于安装那个版本首先要看清楚版本代表什么意思

Centos7 安装nginx1.14

Nginx官网提供了三个类型的版本
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
Stable version:最新稳定版,生产环境上建议使用的版本(毫无疑问,生产环境用于次版本)
Legacy versions:遗留的老版本的稳定版

编译安装前所需要的准备:

1.GCC编译器
首先检查GCC是否安装,命令:gcc -v ,如果显示有相关版本信息,则说明已经安装好,没有就安装:

yum install -y gcc    # -y参数表示一直确认安装 

2.PCRE库
Nginx的HTTP模块要用它来解析正则表达式。

yum install -y pcre pcre-devel 

pcre-devel是使用PCRE做二次开发时所需要的开发库。类似的你可以想到安装LAMP时安装的php-devel。
3.zlib库
gzip格式的压缩会用到它。

yum install -y zlib zlib-devel 

4.OpenSSL库

yum install -y openssl openssl-devel 

wget http://nginx.org/download/nginx-1.14.0.tar.gz #这里安装的是1.14.0生产稳定版本

解压安装

tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0/

  1. ./configure --prefix=/usr/local/nginx --pid-path=/run/nginx.pid  --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log  --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre   #生成 Makefile,为下一步的编译做准备
  2. make             #编译
  3. make install     #安装

附(Nginx部分控制命令):

默认Nginx安装在/usr/local/nginx/中,因此

  1. /usr/local/nginx/sbin/nginx           #默认启动方式 start
  2. /usr/local/nginx/sbin/nginx -t        #测试配置信息
  3. /usr/local/nginx/sbin/nginx -v        #显示版本信息,-V(大V)显示编译时的参数
  4. /usr/local/nginx/sbin/nginx -s stop  #快速停止服务
  5. /usr/local/nginx/sbin/nginx -s quit   #正常停止服务
  6.   /usr/local/nginx/sbin/nginx -s reload                            #重启

Q: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

A: [[email protected] nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

使用nginx -c的参数指定nginx.conf文件的位置

Centos7 安装nginx1.14

如果不行记得要打开端口 

firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)

重新载入

firewall-cmd --reload

 

可选 创建方便启动命令

创建nginx启动命令脚本 

vi /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

#! /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

  设置执行权限 
chmod a+x /etc/init.d/nginx 
注册成服务 
chkconfig --add nginx 
设置开机启动 
chkconfig nginx on