CentOS上安装nginx(单点入门)

安装

1.安装所需的依赖库

yum install -y gcc

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel

2.进入官网获取安装资源 http://nginx.org/en/download.html


点击鼠标右键可获取下载链接,然后使用wget进行下载

wget http://nginx.org/download/nginx-1.14.2.tar.gz

3.安装nginx

解压 :tar -zxvf nginx-1.14.2.tar.gz

配置安装 :./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

编译 :make

安装 : make install

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

/usr/local/nginx/sbin/nginx                  #默认启动方式 start

/usr/local/nginx/sbin/nginx -t               #测试配置信息

/usr/local/nginx/sbin/nginx -v              #显示版本信息,-V(大V)显示编译时的参数

/usr/local/nginx/sbin/nginx -s stop      #快速停止服务

/usr/local/nginx/sbin/nginx -s quit       #正常停止服务

/usr/local/nginx/sbin/nginx -s reload   #重启

开放端口

永久开放80端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

firewall-cmd --reload

创建启动脚本

vi /etc/init.d/nginx

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

配置文件

主配置文件 /usr/local/nginx/conf/nginx.conf

引入外部配置文件

假设外部配置文件存放的目录为 /usr/local/nginx/conf/extend/ ,文件名为 extend.conf。则在主配置文件的末尾(右花括号上面)追加 

include /usr/local/nginx/conf/extend/extend.conf;

参考

1.Centos7 安装nginx1.14

2.CentOS7 下 Nginx 安装部署和配置

3.Nginx配置文件nginx.conf详解

你可能感兴趣的:(CentOS上安装nginx(单点入门))