Centos7 安装nginx1.14

本文详细介绍了如何在Linux环境下安装Nginx的稳定版本,并提供了从编译环境搭建到具体配置步骤的全过程。文章还包含了Nginx的部分控制命令及常见问题解决方法,适合初学者和运维人员参考。

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

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: [root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 

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

如果不行记得要打开端口 

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 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值