centos5.5 Apache2 Web 服务器的安装

# vi /etc/httpd/conf/httpd.conf
把以下虚拟机的配置加在 httpd.conf  文件末尾即可

NameVirtualHost *:80               //注意:这行默认是有#号的,记得要去掉

# # VirtualHost example:

# Almost any Apache directive may go into a VirtualHost container.

# The first VirtualHost section is used for all requests that do not

# match a ServerName or ServerAlias in any block.

# ServerAdmin admin@centoscn.com
<VirtualHost *:80>
    ServerAdmin  admin@centoscn.com
    DocumentRoot  /var/www/html/centos
    ServerName www.centoscn.com
    ServerAlias www.centoscn.com
    ErrorLog logs/centoscn.com-error_log
    CustomLog log/centoscn.com-access_log common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin  admin@gamekey.com.cn
    DocumentRoot /var/www/html/gamekey.com.cn
    ServerName www.gamekey.com.cn
    ServerAlias www.gamekey.com.cn
    ErrorLog logs/gamekey.com.cn-error_log
    CustomLog logs/gamekey.com.cn-access_log common
</VirtualHost>

1.Apache2 Web 服务器的安装

sudo apt install apache2 -y

当安装Apache2包之后,Apache2相关的服务是启用的,并在重启后自动运行。
在某些情况下如果你的Apache2服务器没有自动运行和启用,可以输入以下命令来启动它。

sudo systemctl start apache2.service
sudo systemctl enable apache2.service
sudo systemctl status apache2.service

如果开启了防火墙(ufw),可以使用下面的命令来解除Web服务器的端口(80和443)限制。

sudo ufw status
sudo ufw allow in 'Apache Full'

可以访问Web服务器,输入服务器IP地址或主机名(http://IP_Address_OR_Host_Name),顯示如下
image













/etc/httpd/conf/下有httpd.conf,httpd.conf.bak兩個文件,如果套用httpd.conf.bak文件,顯示如下
image











2.
將網站文件傳到服務器
新的linux CentOS服务器,安装好php环境后,apache默认解析路径是/var/www/html,也可以用自己设置的目录路徑:

例:在根目录下新建/data/website文件夹用来存放项目,准备工作:

创建目录


mkdir data
cd data
mkdir website


操作步骤


vim /etc/httpd/conf/httpd.conf


DocumentRoot “/var/www/html” 改为DocumentRoot “/data/website”
image


 
 
 
 
 
 
 
 
 
 
<Directory “/var/www/html”> 改为<Directory “/data/website”>
image
apahce的默认路径就更改完成,重启Apache服务器

service httpd restart


访问localhost的时候,会发现访问拒绝,这是为什么呢?
因为/home/wwwroot/web1/htdocs的权限是750,apache这个用户没有权限访问,需要更改权限

chmod -R 755 /data/website


再去访问,运行了(apache的用户:apache 运行apache的组:apache)
上傳網站文件到/data/website/目录下即可.

顺便说一点,防止网站乱码请将/etc/httpd/conf/httpd.conf中AddDefaultCharset UTF-8 改为 AddDefaultCharset OFF

3.Apache配置多网站多域名

    vi /etc/httpd/conf/httpd.conf
    把虚拟机的配置加在 httpd.conf文件末尾即可

    NameVirtualHost *:80              
    # VirtualHost example:
    # Almost any Apache directive may go into a VirtualHost container.
    # The first VirtualHost section is used for all requests that do not
    # match a ServerName or ServerAlias in any block.
   

    范例:    绑定两个网站

    <VirtualHost *:80>
    ServerAdmin  admin@centoscn.com
    DocumentRoot  /data/website/centos
    ServerName www.centoscn.com
    ServerAlias www.centoscn.com
    ErrorLog logs/centoscn.com-error_log
    CustomLog log/centoscn.com-access_log common
    </VirtualHost>

    <VirtualHost *:80>
    ServerAdmin  admin@gamekey.com.cn
    DocumentRoot /data/website/gamekey.com.cn
    ServerName www.gamekey.com.cn
    ServerAlias www.gamekey.com.cn
    ErrorLog logs/gamekey.com.cn-error_log
    CustomLog logs/gamekey.com.cn-access_log common
    </VirtualHost>

注:配置虚拟主机的时候,第一个虚拟主机是默认的,你需要留着,配置自己的虚拟主机,从第二个开始配置,也就是第二段 <VirtualHost> 代码。

原文地址:https://www.cnblogs.com/k98091518/p/5825444.html