[nginx] 简单搭建

nginx 简单搭建

一、安装使用

安装

# yum -y install pcre nginx

启动/关闭/重启

# systemctl start nginx
# systemctl stop nginx
# systemctl restart nginx

开放80端口

# firewall-cmd --permanent --zone=public --add-port=80/tcp
# firewall-cmd --reload
# firewall-cmd --query-port=80/tcp
yes

用浏览器访问服务器IP,就可以访问nginx的首页

二、配置

nginx.conf

user nginx;  # 创建nginx work进程的用户
worker_processes auto;  # 进程数,设置与CPU数一直
error_log /var/log/nginx/error.log; # 日志路径和级别
pid /run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    ...
    server {
        listen       80;
        listen       [::]:80 default_server;
        server_name  www.test.cn;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

命令

# nginx                 # 启动
# nginx -s stop         # 停止
# nginx -s reopen       # 重启
# nginx -s reload       # 重load config
# nginx -t              # 测试config是否正确

三、查看状态

  • nginx状态
# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2020-02-09 16:52:54 CST; 19min ago
  • nginx进程
# ps -ef |grep nginx
root     17895     1  0 16:52 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx    17896 17895  0 16:52 ?        00:00:00 nginx: worker process
  • nginx端口
# netstat -ltpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17895/nginx: master
tcp6       0      0 :::80                   :::*                    LISTEN      17895/nginx: master

四、排错

  1. nginx: [error] open() “/run/nginx.pid” failed (2: No such file or directory)
    重启nginx试试
  2. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
    netstat找到端口被谁用了,kill掉;然后再启动nginx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值