Centos7 搭建http服务器创建多站点

搭建HTTP服务器全攻略

搭建http服务器步骤如下:

  1、安装httpd服务

sudo yum install httpd

Apache 的所有配置文件都位于  /etc/httpd/conf  和  /etc/httpd/conf.d  。网站的数据默认位于  /var/www,但如果你愿意,你可以改变它。

2、防火墙端口设置:打开端口80(一般不用设置)

(1)查询TCP/UDP的80端口占用情况:

sudo firewall-cmd --query-port=80/tcp

sudo firewall-cmd --query-port=80/udp

如果返回结果为“no”,则表示该端口尚未开放,需要作以下设置才可以;否则,跳过步骤2.3。

(2)永久开放TCP/UDP的80端口

sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
sudo firewall-cmd --permanent --zone=public --add-port=80/udp

(3)重启防火墙

sudo firewall-cmd --reload

3、启动httpd服务

sudo systemctl start httpd

4、访问服务

输入127.0.0.1 或者 localhost 访问即可

5、重启后无法访问解决办法

首先查询http服务的状态: sudo systemctl status httpd 。

如果http服务的状态正常,显示它“active”,则问题不出在httpd服务上。

最有可能出问题的是SELINUX的设置,因为重启之后,之前的设置都不生效了。

(1)临时关闭SELINUX

#设置SELinux 成为permissive模式
##setenforce 1 设置SELinux 成为enforcing模式
setenforce 0

(2)永久关闭SELINUX

vi /etc/selinux/config

将 SELINUX=enforcing 改为 SELINUX=disabled ,设置后需要重启才能生效。

6、开启目录结构

(1)修改配置文件 welcome.conf

将配置文件

 /etc/httpd/conf.d/welcome.conf 
Options -Indexes 

改为

Options +Indexes 

(2)重启httpd服务生效

sudo systemctl restart httpd

7、更改http服务器的默认目录

在配置文件 /etc/httpd/conf/httpd.conf 中一共有三个地方需要修改。

(1)修改参数 “DocumentRoot”:

# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
    # access content that does not live under the DocumentRoot.

改成自己需要的目录路径

(2)修改目录参数

#
# Relax access to content within /var/www.
#
#<Directory "/var/www">
<Directory "自定义的目录"> (路径层数匹配) 例如/var/xxx/
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>
...

(3)再次修改目录参数

# Further relax access to the default document root:
#<Directory "/var/www/html">
<Directory "自定义目录"> (路径层数匹配) 例如/var/xxx/xxx
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #

(4)重启服务生效

sudo systemctl restart httpd

创建多站点步骤如下:

1、创建站点目录文件

在 /var/www/  下 创建文件夹  (例如test)

2、添加站点

在 /etc/httpd/conf.d 中的 vhost.conf 文件中加入以下东西 (vhost.conf 文件没有请自行创建)

<VirtualHost _default_:80>
DocumentRoot "/var/www/html"
  <Directory "/var/www/html">
    Options +Indexes +FollowSymLinks +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/html/test/"
    ServerName www.a.com
    ServerAlias a.com
  <Directory "/var/www/html/test/">
      Options FollowSymLinks ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all
      Require all granted
  </Directory>
</VirtualHost>

如果更换tcp端口  例如8080  头部添加Listen 8080

3、配置本地DNS服务器

在 /etc/hosts 中加入解析

例如:你的域名为 www.test.com

加入  127.0.0.1  www.test.com      127.0.0.1  test.com

 

apache 2.2 命令

service httpd start 

service httpd stop

service httpd restart

apache2.4 命令

sudo systemctl start httpd

sudo systemctl restart httpd

sudo systemctl stop httpd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值