你一定要会的 linux中的nginx服务搭建
下载nginx
[root@server ~]# yum install nginx -y
有一种可能下载会报错
[root@server ~]# yum install nginx -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.ustc.edu.cn
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
No package nginx available.
Error: Nothing to do
没有包。
No package nginx available.
Error: Nothing to do
那么这个时候呢我们有两种解决办法:
一、更改yum下载源
参考文档centos修改下载源
二、用rpm包下载-添加nginx包
wget https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
然后再重新执行下载命令
2.编辑配置文件
[root@server ~]# vim /etc/nginx/conf.d/default.conf
##如果你没有别的要求,我们只需要更改默认端口 80 为别的任意 我这里用81
server {
listen 81;
server_name localhost;
3.firewalld 和 selinux 开放端口
如果是在测试环境下可以不用开放端口,直接关闭firewalld 和 selinux 就ok
当然当然! selinux 默认开放81端口得,不过举例子嘛,万一你要改成8001呢,哈哈,挺尴尬
#关闭防火墙
[root@server ~]# systemctl stop firewalld
#临时关闭selinux 重启之后就恢复了哦
[root@server ~]# setenforce 0 #更改成警告模式,允许通过
[root@server ~]# getenforce #查看selinux状态
Permissive
#永久关闭
[root@server ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced. #严格模式
# permissive - SELinux prints warnings instead of enforcing. #警告模式
# disabled - No SELinux policy is loaded. #关闭
SELINUX=enforcing #永久修改就把这个参数改成以上例子中的一个就OK咯
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
一、selinux开放
[root@server ~]# semanage port -a -t http_port_t -p tcp 81
二、firewalld 开放
[root@server ~]# firewall-cmd --add-port=81/tcp --permanent
[root@server ~]# firewall-cmd --reload
[root@server ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client ssh
ports: 82/tcp 81/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@server ~]#
测试
万事俱备只欠东风,别忘记打开nginx 服务哦
[root@server ~]# systemctl restart nginx
[root@server ~]# systemctl enable nginx
老规矩 ip:port
拜拜咯~~