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
四、排错
- nginx: [error] open() “/run/nginx.pid” failed (2: No such file or directory)
重启nginx试试 - nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
netstat找到端口被谁用了,kill掉;然后再启动nginx