简介
本文主要介绍在CentOS下,Apache与Nginx 服务的搭建与配置
一、关闭防火墙和selinux
1.关闭防火墙
- 查看防火墙状态
systemctl status firewalld
- 临时关闭防火墙(重启电脑会重新启动)
systemctl stop firewalld
systemctl status firewalld
- 永久关闭防火墙
systemctl disable firewalld
1.关闭selinux
-
查看selinux状态
enforcing:强制模式(临时开启)
permissive:宽容模式(临时关闭)
disabled:永久关闭
输入getenforce
-
临时关闭selinux
setenforce 0
getenforce
- 永久关闭selinux
输入**vi /etc/sysconfig/selinux**
将文件中的SELINUX=**enforcing**改为**disabled**
重启虚拟机生效
二、Web服务器与配置
1.Apache
- 安装并启动httpd服务
yum -y install httpd
systemctl start httpd
- 检查httpd是否启动和所使用的端口号
netstat -lntp | grep httpd
- 查看httpd的主配置文件(/etc/httpd/conf/httpd.conf)
及默认发布网站的目录(/var/www/html)
vim /etc/httpd/conf/httpd.conf
- 配置httpd的子配置文件(/etc/httpd/conf.d/)
cd /etc/httpd/conf.d
<Virtualhost *:80>
DocumentRoot /data
ServerName localhost
<Directory "/data">
AllowOverride None
Require all granted
</Directory>
</Virtualhost>
//创建发布网站目录(这里是/data)
mkdir /data
- 测试访问
//重启httpd服务
systemctl restart httpd
输入虚拟机IP地址和端口号
出现此界面说明访问成功
2.Nginx
- 安装并启动Nginx服务(因默认和httpd一样默认使用80端口号,所以需要停止httpd服务)
yum -y install nginx
systemctl stop httpd
systemctl start nginx
- 检查Nginx是否启动和所使用的端口号
netstat -lntp | grep nginx
-
Nginx的主配置文件(/etc/nginx/nginx.conf)
查看默认配置文件(/etc/nginx/conf.d/default.conf)
以及默认发布网站的目录(/var/www/html)
-
配置Nginx的子配置文件(/etc/nginx/conf.d/)
cd /etc/nginx/conf.d
vim test.conf
server{
listen 81; # 因为default.conf已经使用了80端口,所以这里使用81端口
server_name localhost;
location / {
root /data;
index index.html index.htm;
}
}
- 测试访问
//测试nginx的配置和退出
nginx -t
//重新加载nginx配置
nginx -s reload
输入虚拟机IP地址和端口号
出现此界面说明访问成功