linux下安装nginx

14 篇文章 0 订阅

1. 下载及安装过程

如果没有gcc环境,需要安装gcc:
yum install gcc-c++

安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

# 进入文件夹
cd  /usr/local

#下载安装包
wget http://nginx.org/download/nginx-1.18.0.tar.gz

#解压安装包
tar -xvf nginx-1.18.0.tar.gz 

#解压之后不需要重新命名直接进去解压目录
#进入nginx-1.18.0目录 
cd /usr/local/nginx-1.18.0

#执行命令
#prefix= 指向安装目录(编译安装)
#conf-path= 指向配置文件(nginx.conf)
#error-log-path= 指向错误日志目录
#pid-path= 指向pid文件(nginx.pid)
#http-log-path= 设定access log路径
#with-http_gzip_static_module 启用ngx_http_gzip_static_module支持(在线实时压缩输出数据流)
#with-http_stub_status_module 启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
#with-http_ssl_module 启用ngx_http_ssl_module支持(使支持https请求,需已安装openssl)

./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf  --error-log-path=/usr/local/nginx/logs/error.log --pid-path=/usr/local/nginx/logs/nginx.pid  --http-log-path=/usr/local/nginx/logs/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module

#执行命令
make

#执行make install命令 
make install

#启动nginx
 cd  /usr/local/nginx/sbin
./nginx

#查看nginx进程
ps -ef | grep nginx

#打开阿里云的网路安全组开放80端口

#在浏览器访问服务器ip

#设置开机自动启动
vim /lib/systemd/system/nginx.service
#按i编辑 把下面复制进去  按esc建  再按shift+:键 wq  保存退出

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
#自己nginx启动的pid文件自己找到文件目录
PIDFile=/usr/local/nginx/logs/nginx.pid
#自己nginx的启动文件 
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#默认
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target


#启动
systemctl start nginx.service

设置开机自启
systemctl enable nginx.service

#提示
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

#停止开机自启动
systemctl disable nginx.service

#查看服务当前状态
systemctl status nginx.service

#重新启动服务
systemctl reload nginx.service

#停止服务
systemctl stop nginx.service

配置nginx开机自启有两种方案,一种如上面介绍的(推荐);
另一种是vim /etc/rc.d/rc.local
nginx

2. 常见报错

2.1 执行make install 报错问题解决make[1]: *** [install] Error 1 make: *** [install] Error 2

可能是相关依赖没有安装,安装依赖后再重新编译

2.2 make[1]: *** [install] Error 1
make[1]: *** [install] Error 1
make[1]: Leaving directory `/usr/local/nginx'

文件的解压目录和编译目录不能是同一文件夹

2.3 启动nginx服务报错Job for nginx.service failed because the control process exited with error code.nginx使用service nginx restart报错

启动nginx服务时如果遇到这个错误 Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details.

可能原因:

  1. 配置文件语法有误,执行nginx -t

    查看输出提示信息 并检查端口是否被占用netstat -tnlp

  2. nginx的配置文件nginx.conf中监听了其他端口,这些端口的子进程仍然运行,导致端口占用。
    需要首先关闭子进程,才能使用该命令。
    此时可以kill -9 pid 。

3. nginx的相关命令

nginx文件夹下:

  • 启动:./sbin/nginx
  • 根据配置文件启动:./sbin/nginx -c nginx_vue.conf(默认为nginx.conf)
  • 停止:./sbin/nginx -s stop
  • 退出:./sbin/nginx -s quit(优雅的停止)
  • 重启:./sbin/nginx -s reopen
  • 重新加载配置文件:./sbin/nginx -s reload
  • 测试 nginx 配置是否有语法错误:./sbin/nginx -t
  • 查看 nginx 版本:./sbin/nginx -v
  • 查看帮助:./sbin/nginx -s -h

4. 相关优质文章:

参考文献:
https://blog.csdn.net/weixin_41400411/article/details/116059479
https://www.cnblogs.com/xxoome/p/5866475.html
https://blog.csdn.net/paralysed/article/details/104162345

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值