一、安装
Centos系统
Fedora27
使用以下命令直接安装Nginx
sudo dnf install nginx
二 、启动并查找配置文件位置
systemctl start nginx.service #启动
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 Fri 2018-01-12 14:13:49 EST; 1min 34s ago
Process: 5906 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 5905 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 5904 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 5907 (nginx)
Tasks: 2 (limit: 4915)
CGroup: /system.slice/nginx.service
├─5907 nginx: master process /usr/sbin/nginx
└─5909 nginx: worker process
1月 12 14:13:49 Xin-Water systemd[1]: Starting The nginx HTTP and reverse proxy server...
1月 12 14:13:49 Xin-Water nginx[5905]: nginx: [warn] could not build optimal types_hash, you should increase either types_hash_max_size
1月 12 14:13:49 Xin-Water nginx[5905]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
1月 12 14:13:49 Xin-Water nginx[5905]: nginx: configuration file/etc/nginx/nginx.conf test is successful
1月 12 14:13:49 Xin-Water nginx[5906]: nginx: [warn] could not build optimal types_hash, you should increase either types_hash_max_size
1月 12 14:13:49 Xin-Water systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument
1月 12 14:13:49 Xin-Water systemd[1]: Started The nginx HTTP and reverse proxy server.
此时如果在图形界面下,可输入:127.0.0.1 查看nginx服务器欢迎界面,上面也会有nginx家目录位置和配置文件位置:
三、更改nginx访问根目录
1)修改配置文件:
fedora27:vi /etc/nginx/nginx.conf
centos7: vi /etc/nginx/conf.d/default.conf
其他版本看nginx启动日志。
找到:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
要改根目录为:/var/www/html/,则配置修改如下:
方式1:更改root 后为:/var/www/html;
方式2:删除root行,修改location /{
}
为以下内容:
location / {
root /var/www/html; #修改http://locakhost:80/映射地址,此时对应本机访问路径为 :/var/www/html。四、设置防火墙
1)开放80端口、开放http服务,重启防火墙
sudo firewall-cmd --list-all #查看防火墙情况
FedoraWorkstation (active)
target: default
icmp-block-inversion: no
interfaces: enp0s17
sources:
services: dhcpv6-client samba-client mdns
ports: 1025-65535/udp 1025-65535/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
知道防火墙活跃区域为:FedoraWorkstation,则命令为:
sudo firewall-cmd --zone=FedoraWorkstation --add-port=80/tcp --permanent
sudo firewall-cmd --zone=FedoraWorkstation --add-service=http --permanentsudo firewall-cmd --reload
sudo firewall-cmd --list-all #查看开放服务、端口中是否有http服务和80端口。
FedoraWorkstation (active)
target: default
icmp-block-inversion: no
interfaces: enp0s17
sources:
services: dhcpv6-client samba-client mdns http
ports: 1025-65535/udp 1025-65535/tcp 80/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
如果防火墙出现问题导致设置不成功,可关闭防火墙:(我在fedora上遇到前2步提示执行成功,但查看开发端口和服务是确看不到的问题,后来安装firewalld图形化工具firewall-config,在图形界面下来设置)
五、nginx使用命令
1 启动服务:systemctl start nginx.service # 打开浏览器输入127.0.0.1如果出现欢迎页面,说明启动成功。