Nginx-note-day01

Nginx-note-day01

一、Nginx的yum安装
1、配置yum nginx源
vi /etc/yum.nginx.repos.d/nginx.repo
加入以下:
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/releasever/releasever/basearch/
gpgcheck=0
enabled=1
2、查看nginx版本:
yum list| grep nginx
Nginx-note-day01
3、安装nginx
yum install -y nginx
Nginx-note-day01
4、启动nginx
systemctl start nginxNginx-note-day01
5、查看端口占用情况
netstat -lntp
Nginx-note-day01
6、浏览器测试
Nginx-note-day01
注意如果有防火墙需要关闭防火墙,centos7中防火墙的关闭命令:
systemctl stop firewalld.service
有防火墙规则的需要添加防火墙规则:iptables -I INPUT -p tcp --dport 80 -j ACCEPT
Nginx-note-day01

7、查看nginx版本
Nginx-note-day01

二、nginx的源码安装
1、下载nginx:
wget http://nginx.org/download/nginx-1.16.1.tar.gz
Nginx-note-day01
2、解压nginx包
tar -zxvf nginx-1.16.1.tar.gzNginx-note-day01
3、指定安装目录等参数
./configure --prefix=/usr/local/nginx
Nginx-note-day01
出现错误,解决办法如下:
yum -y install gcc gcc-c++ autoconf automake make
Nginx-note-day01
为解决出现的以下问题,执行:
yum -y install pcre-devel
Nginx-note-day01
Nginx-note-day01
为解决以下问题,执行
yum -y install openssl openssl-devel
Nginx-note-day01

4、make&& make install
Nginx-note-day01
查看版本/usr/local/nginx/sbin/nginx -V 与nginx -V一样
Nginx-note-day01
启动nginx:/usr/local/nginx/sbin/nginx
Nginx-note-day01

重启nginx和修改配置文件检测以及重新加载:
killall nginx
vi /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
Nginx-note-day01

5、用启动脚本启动nginx
编写启动脚本vi /etc/init.d/nginx
#!/bin/bash

chkconfig: - 30 21

description: http service.

Source Function Library

. /etc/init.d/functions

Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog=“Nginx”

start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c NGINXCONFRETVAL=NGINX_CONF RETVAL=?
echo
return $RETVAL
}

stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID NGINXSBINTERMrmrf/dev/shm/nginxtempRETVAL=NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=?
echo
return $RETVAL
}

reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID NGINXSBINHUPRETVAL=NGINX_SBIN -HUP RETVAL=?
echo
return $RETVAL
}

restart()
{
stop
start
}

configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}

case “$1” in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $“Usage: $0 {start|stop|reload|restart|configtest}”
RETVAL=1
esac

exit $RETVAL

Nginx-note-day01

6、chkconfig --add nginx
chkconfig nginx on
chkconfig --list
Nginx-note-day01