Centos7 安装Nginx和开机自启详细教程

安装Nginx

创建虚拟机

ps -ef | grep nginx
root       7195   7178  0 14:19 pts/0    00:00:00 grep --color=auto nginx

创建新用户

[root@localhost ~]# useradd leyou

切换到用户目录

[root@localhost ~]# cd /home/leyou/
[root@localhost leyou]# pwd

/home/leyou

将nginx放入该目录,解压

[root@localhost leyou]# tar xvf nginx-1.14.2.tar.gz 

删除压缩包

[root@localhost leyou]# rm -rf nginx-1.14.2.tar.gz 

进去nginx目录

[root@localhost leyou]# cd nginx-1.14.2/

报错安装gcc

yum -y install gcc

安装pcre-devel

yum -y install pcre-devel

安装zlib-devel

yum -y install zlib-devel

指定安装目录/opt/nginx

[root@localhost nginx-1.14.2]# ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx

安装nginx

make && make install

启动nginx

[root@localhost nginx-1.14.2]# nginx

查看进程

[root@localhost nginx-1.14.2]# ps -ef | grep nginx
root      11158      1  0 14:52 ?        00:00:00 nginx: master process nginx
nobody    11159  11158  0 14:52 ?        00:00:00 nginx: worker process
root      11161   7178  0 14:52 pts/0    00:00:00 grep --color=auto nginx

关闭防火墙(选)

查看防火墙状态: systemctl status firewalld.service
执行关闭命令: systemctl stop firewalld.service
再次执行查看防火墙命令:systemctl status firewalld.service
执行开机禁用防火墙自启命令  : systemctl disable firewalld.service

开放端口

firewall-cmd --zone=public --add-port=80/tcp --permanent
加载
firewall-cmd --reload
重启防火墙:
    systemctl stop firewalld.service  
    systemctl start firewalld.service
    
配置前先确保防火墙是运行着的 firewall-cmd --state
开启防火墙 service firewalld start

访问nginx

http://192.168.25.128/

开机自启

vi /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/bin/nginx
ExecReload=/usr/bin/nginx -s reload
ExecStop=/usr/bin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target
systemctl enable nginx.service

修改nginx.conf配置文件,配置反向代理

server {
        listen       80;
        server_name  manage.leyou.com;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
			proxy_pass http://192.168.1.141:9001;
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
    }
    server {
        listen       80;
        server_name  api.leyou.com;

        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        location / {
			proxy_pass http://192.168.1.141:10010;
			proxy_connect_timeout 600;
			proxy_read_timeout 600;
        }
    }

重新加载nginx配置文件

nginx -s reload

猜你喜欢

转载自blog.csdn.net/qq_41916173/article/details/88890204