nginx的简单安装

1.安装依赖包
//一键安装上面四个依赖

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

2.下载并解压安装包
//创建一个文件夹

cd /usr/local
mkdir nginx
cd nginx

//下载tar包

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvf nginx-1.18.0.tar.gz

3.安装nginx
//进入nginx目录

cd /usr/local/nginx

//进入目录

cd nginx-1.18.0

//执行命令
./configure
3.1 执行./configure过程
[root@localhost nginx-1.18.0]# ./configure

checking for OS
 + Linux 3.10.0-1062.4.1.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
。。。。
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

//执行make命令

make

3.2 执行make的过程
[root@localhost nginx-1.18.0]# make

make -f objs/Makefile
make[1]: 进入目录“/usr/local/nginx/nginx-1.18.0”
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
        -o objs/src/core/nginx.o \
        src/core/nginx.c
。。。
。。。
objs/src/http/modules/ngx_http_upstream_zone_module.o \
objs/ngx_modules.o \
-ldl -lpthread -lcrypt -lpcre -lz \
-Wl,-E
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
        -e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
        -e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
        -e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
        < man/nginx.8 > objs/nginx.8
make[1]: 离开目录“/usr/local/nginx/nginx-1.18.0”

//执行make install命令

make install

3.3 执行make install 的过程
[root@localhost nginx-1.18.0]# make install
make -f objs/Makefile install

make[1]: 进入目录“/usr/local/nginx/nginx-1.18.0”
test -d '/usr/local/nginx' || mkdir -p '/usr/local/nginx'
。。。
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/usr/local/nginx/nginx-1.18.0”
[root@localhost nginx-1.18.0]# 

4.配置nginx.conf
打开配置文件

vi /usr/local/nginx/conf/nginx.conf

将端口号改成81,因为可能apeache占用80端口,apeache端口尽量不要修改,我们选择修改nginx端口。
localhost修改为你服务器ip地址,利用ip addr / ifconfig 可以查看到ens33中的inet 其后面的ip为服务器ip地址。
反向代理的简单配置

server {
        listen       81;
        server_name  172.1.3.56; #服务端地址
	
        location / {
            root   html;
            index  index.html index.htm;
        }
}

5.启动nginx

/usr/local/nginx/sbin/nginx -s reload

如果出现报错:nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed

则运行:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

再次启动即可!

ps -ef | grep nginx

访问:
http://172.1.3.56:81/

成功!

若第一次安装并服务启动后 但是无法在浏览器中打开,则基本上是防火墙的问题,可以在防火墙中开启nginx端口访问:
1.查看防火墙是否开启
systemctl status firewalld (下图表示防火墙正在运行)

2.手动开启端口相关命令

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

命令含义: --zone #作用域 --add-port=81/tcp #添加端口,格式为:端口/通讯协议 --permanent #永久生效,没有此参数重启后失效

3.开启后需要重启防火墙生效

systemctl restart firewalld.service

4.查看防火墙是否开启了81端口的访问

 firewall-cmd --list-all

相关命令及问题日志:
重启

/usr/local/nginx/sbin/nginx -s reload

启动命令:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

重新打开日志文件(分割日志):

/usr/local/nginx/sbin/nginx -s reopen :

关闭nginx:
/usr/local/nginx/sbin/nginx -s stop : 快速停止nginx

/usr/local/nginx/sbin/nginx -s quit :完整有序的停止nginx
直接杀nginx进程
ps aux|grep nginx 查看nginx的主进程号,调用kill -9 nginx的进程号来强制停止nginx
(还有“kill -quit nginx的进程号”来从容停止nginx,
“kill -term nginx的进程号”来快速的停止nginx)

问题日志:
问题:Nginx重启时丢失nginx.pid文件
原因:
nginx被停止时,/opt/nginx/logs/nginx.pid被删除了。
而 reopen和 reload命令需要通过nginx.pid获取进程号,
会去找/opt/nginx/logs/nginx.pid ,如果不存在,就报错了。
解决:
reopen是在nginx启动的情况给做分割日志用的,
reload也是在nginx启动的情况下做平滑重启的,
他们都依赖于nginx进程存在的情况下。 并不是字面上启动或打开的意思。
真正的启动命令是
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值