Nginx安装方式
一.源码安装
1.下载nginx源码包 http://nginx.org/download/获取地址
这里以nginx-1.14.0.tar.gz演示
2.创建安装目录,准备安装环境
[root@localhost ~]# mkdir -p /opt/data/nginx/
[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -g nginx nginx
3.安装依赖包
[root@localhost ~]# yum install gcc gcc-c++ make recp pcre-devel openssl openssl-devel -y
4.解压
[root@localhost ~]# tar -xf nginx-1.14.0.tar.gz -C /usr/local/src/
(/usr/local/src/ 为解压目录切勿与安装目录混淆)
5.编译安装
[root@localhost ~]# cd /usr/local/src/nginx-1.14.0/
[root@localhost nginx-1.14.0]# ./configure --prefix=/opt/data/nginx --with-http_stub_stat us_module --with-http_ssl_module --with-stream
[root@localhost nginx-1.14.0]# make & make install
6.配置环境变量
[root@localhost ~]# vim /etc/profile
[root@localhost ~]# source /etc/profile
7.nginx配置
[root@localhost ~]# ln -s /opt/data/nginx/conf /etc/nginx
[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# mv nginx.conf nginx.conf.bak
[root@localhost nginx]# vim nginx.conf
user nginx;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
access_log off;
client_max_body_size 128M;
include /etc/nginx/conf.d/*.conf;
}
[root@localhost nginx]# mkdir /etc/nginx/conf.d
[root@localhost nginx]# mkdir /var/log/nginx
[root@localhost nginx]# chown -R nginx:nginx /var/log/nginx
8.nginx启动
[root@localhost nginx]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/opt/data/nginx/sbin/nginx -t -c /opt/data/nginx/conf/nginx.conf
ExecStart=/opt/data/nginx/sbin/nginx -c /opt/data/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
ExecStartPost=/bin/sleep 0.1
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost nginx]# systemctl start nginx
[root@localhost nginx]# systemctl enable nginx
[root@localhost nginx]# systemctl status nginx
启动成功
此时nginx进程已经启动 但是没有nginx实例没有80端口。
遇见问题
首次启动
Failed to read PID from file /var/run/nginx.pid: Invalid argument
解决办法:vim /lib/systemd/system/nginx.service
加入图上箭头上的内容解决
[root@localhost nginx]# systemctl daemon-reload
[root@localhost nginx]# systemctl restart nginx.service
[root@localhost nginx]# systemctl status nginx
问题解决因为 nginx 启动需要一点点时间,而 systemd 在 nginx 完成启动前就去读取 pid file
造成读取 pid 失败
让 systemd 在执行 ExecStart 的指令后等待一点点时间即可
如果你的 nginx 启动需要时间更长,可以把 sleep 时间改长一点
参考资料:参考链接
二、yum安装方式
yum安装请参考这里