010 Linux 下通过yum、apt、dnf方式安装和配置Nginx服务器
一、安装
Centos系统
Centos7下:#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Centos6下:#rpm -Uvh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
Centos5下:#rpm -Uvh http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm
2、使用yum命令安装Nginx。
yum install nginx
Ubuntu系统
使用以下命令直接安装Nginx
sudo apt-get install nginx
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。index index.jpg index.html index.htm;
}
2)修改根目录访问权限
如果权限设置不对,就没法读文件了,一读就是403错误!
目录权限1 目录运行权限:x 可进入目录
目录权限2 目录写权限:w 可复制、粘贴、删除、移动该目录下文件
目录权限4 目录读权限:r 可列出目录下所含文件
文件权限1 文件运行权限:x 可执行文件
文件权限2 文件写权限:w 可修改文件里面内容
文件权限4 文件读权限:r 可查看文件内容
所以:要查看目录内容,最低权限为:5(要先能够进入目录,再有列出文件权限),要查看文件内容,最低权限为4
最后,设置人们对该目录和里面文件的读写权限为:
chmod 751 /var/www/html/ # 其他用户只能进入目录,但看不到目录下有哪些文件
chmod -R 755 /var/www/html/* # -R 是递归遍历子目录,设置改目录下的东西为所有人可查看。
3)重启nginx服务生效
systemctl restart nginx.service
四、设置防火墙
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如果出现欢迎页面,说明启动成功。2 查看、显示服务当前状态:systemctl status nginx.service
3 设置开机自启动:systemctl enable nginx.service
4 停止开机自启动:systemctl disable nginx.service
5 重新启动服务: systemctl restart nginx.service
6 停止ftp服务: systemctl stop nginx.service
附
Centos下卸载Nginx:yum remove nginx
Ubuntu下卸载Nginx:sudo apt-get remove nginx
Fedora27下卸载Nginx:sudo dnf remove nginx