微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux下安装nginx

文章目录

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文件夹下:

参考文献:
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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐