源码安装Nginx

1、源码安装nginx

创建安装目录
[root@localhost ~]# mkdir -p /opt/data/nginx/
[root@localhost ~]# groupadd nginx
[root@localhost ~]# useradd -g nginx nginx
安装编译工具
[root@localhost ~]# yum install gcc gcc-c make pcre-devel openssl openssl-devel -y
解压下载下来的nginx包并指定解压目录
[root@localhost ~]# tar zxf nginx-1.14.0.tar.gz -C /usr/local/src/
进入解压后的目录
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# ls
nginx-1.14.0
[root@localhost src]# cd nginx-1.14.0/
进行预编译
[root@localhost nginx-1.14.0]# ./configure --prefix=/opt/data/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream
看上一步执行是否成功0为成功否则为失败
[root@localhost nginx-1.14.0]# echo $?
0
进行编译
[root@localhost nginx-1.14.0]# make
查看编译是否成功
[root@localhost nginx-1.14.0]# echo $?
0
安装
[root@localhost nginx-1.14.0]# make install
查看安装是否成功
[root@localhost nginx-1.14.0]# echo $?
0

2、配置环境变量

[root@localhost ~]# cd /opt/data/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# cd sbin/
检测语法是否有错误
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /opt/data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/data/nginx/conf/nginx.conf test is successful
打开配置文件向里面添加
[root@localhost sbin]# vi /etc/profile
export PATH=/opt/data/nginx/sbin:$PATH
重新加载配置文件
[root@localhost sbin]# source /etc/profile
做软连接
[root@localhost nginx]# ln -s /opt/data/nginx/conf/ /etc/nginx
[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# vi 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
打开文件向添加
[root@localhost conf]# 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
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl start nginx
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Job for nginx.service failed because a timeout was exceeded. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@localhost ~]# systemctl daemon-reload
[root@localhost nginx]# systemctl start nginx
[root@localhost nginx]# systemctl status nginx

可能出现的问题1、*Warning: nginx.service changed on disk. Run ‘systemctl daemon-reload’ to reload units.出现这种问题是按照提示执行就可以
2、或者可能出现
Failed to start nginx - high performance web server.*错误 此时首先可能是80端口被占用。可以用lsof -i:80 查看80端口是否被占用如果80端口被占用那就要禁用占用的程序之后重启nginx。如果80端口没有被占用,那么可能是配置文件在书写的时候出现错误。检查配置文件是否正确。

3、设置开机自启

开机自启动
[root@localhost nginx]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

你可能感兴趣的:(linux)