Linux Apache Virtual host configuration (IP-based, port, domain)

Source: Internet
Author: User

Configure the Environment:

Linux version: Centos6.4

HTTPD version:

[Email protected] init.d]# pwd
/etc/init.d
[Email protected] init.d]# httpd-v
Server version:apache/2.2.15 (Unix)
Server built:oct 19 2017 16:43:38

1, install httpd service

Yum Install Httpd-y

2, turn off SELinux and firewall

Temporarily set SELinux to permissive (disabled) state

[Email protected] ~]# Getenforce

Enforcing

[Email protected] ~]# Setenforce 0

[Email protected] ~]# Getenforce

Permissive

Permanent modification Method:

[Email protected] ~]# Vim/etc/sysconfig/selinux

Selinux=permissive

3. Turn off the firewall

Service Iptables Stop

The first, multi-IP-based access configuration:

1, the first httpd.conf back up, in case of errors, can be restored

httpd.conf file path (/etc/httpd/conf)

[[email protected] conf]# ls
Httpd.conf Httpd.conf.bak Magic

2, if you need to enable the virtual host configuration, first the central host configuration comments,

#DocumentRoot "/var/www/html"

3, in httpd.conf, the default package CONF.D all configuration files ending in. conf in the directory.

Include conf.d/*.conf

Therefore, the configuration files of the virtual host are isolated and placed in the CONF.D directory

[[email protected] httpd]# ls
Conf CONF.D logs modules run
[Email protected] httpd]# pwd
/etc/httpd

[Email protected] httpd]# CD CONF.D
[Email protected] conf.d]# pwd
/etc/httpd/conf.d
[[email protected] conf.d]# ls
README virtualhost.conf welcome.conf

Virtualhost.conf Configuration file Contents:

<virtualhost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
</VirtualHost>


<virtualhost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
</VirtualHost>

My host IP is 192.168.1.8, add another IP:

IP addr Add 192.168.1.2/24 dev eth0

To create a file in the corresponding directory:

[Email protected] www]# tree
.
├──7mxt.com
│└──index.html
└──7mxt.net
└──index.html

Restart Services: Service httpd Restart, in the browser with each of these two IP access, you can see the corresponding page

The second, multi-port access-based configuration:

1, add a configuration to the virtualhost.conf:

<virtualhost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
</VirtualHost>
<virtualhost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
</VirtualHost>
<virtualhost 192.168.1.2:8080>
ServerName www.abc.net
DocumentRoot "/www/abc.net"
</VirtualHost>

2, listening on the httpd.conf 8080 port

Listen 80
Listen 8080

3,/www creating the corresponding directories and files

Restart Services: Service httpd Restart, in the browser with http://192.168.1.2:8080/can access to the Abc.net directory under the file index.html content

[Email protected] www]# tree
.
├──7mxt.com
│└──index.html
├──7mxt.net
│└──index.html
└──abc.net
└──index.html

The third type: domain-based host configuration

1,namevirtualhost: Specify 192.168.1.8:80 This IP address using domain name interpretation

Namevirtualhost 192.168.1.8:80

<virtualhost 192.168.1.8:80>
ServerName www.7mxt.com
DocumentRoot "/www/7mxt.com"
</VirtualHost>

<virtualhost 192.168.1.8:80>
ServerName www.7mxt.org
DocumentRoot "/www/7mxt.org"
</VirtualHost>

<virtualhost 192.168.1.2:80>
ServerName www.7mxt.net
DocumentRoot "/www/7mxt.net"
</VirtualHost>

<virtualhost 192.168.1.2:8080>
ServerName www.abc.net
DocumentRoot "/www/abc.net"
</VirtualHost>

2, create the file in the corresponding directory

[Email protected] www]# tree
.
├──7mxt.com
│└──index.html
├──7mxt.net
│└──index.html
├──7mxt.org
│└──index.html
└──abc.net
└──index.html

3. Increase the host mapping configuration in the Windows host file

"C:\Windows\System32\drivers\etc"

192.168.1.8 www.7mxt.com
192.168.1.8 www.7mxt.org

4, restart Services: Service httpd restart, respectively, with the domain name www.7mxt.com, www.7mxt.org can access to the corresponding file content

Use ip:192.168.1.8 to return the configuration of the first virtual host, that is, the contents of the www.7mxt.com corresponding directory

Linux Apache Virtual host configuration (IP-based, port, domain)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.