Home Backend Development PHP Tutorial Apache virtual host configuration and usage guide in ubuntu

Apache virtual host configuration and usage guide in ubuntu

Nov 25, 2016 pm 02:42 PM
apache ubuntu virtual host

How to enable and disable the site

a2ensite 站点名
a2dissite 站点名
Copy after login

Name-based (distinguished by domain name) virtual host

After installing apache, there is a virtual host called default by default. When creating a new virtual host, you can directly copy the configuration file of the default virtual host and modify the configuration parameters of the new virtual host based on it.

#copy /etc/apache2/site-available/default /etc/apache2/site-available/sitename
Copy after login

Test environment

Operating system: Ubuntu Server 12.04 LTS

Test machine address: 10.39.6.59

Test machine domain name: *.example.com

Basic configuration

We all know that if we want to run a single machine When setting multiple domain names or host names on the machine, we will use name-based virtual hosts. So how to set it up? That's what this guide aims to solve. There is the main configuration file apache2.conf for Apache2 in Ubuntu's /etc/apache2/ directory. In this file we can see the following fields:

# Include the virtual host configurations:
Include /etc/apache2/sites-enabled/[^.#]*(12.04版本里无[^.#]*)
Copy after login

The meaning of this line indicates that the file contains the file name in the /etc/apache2/sites-enabled/ directory that does not contain the two characters "." or "#" All files. When we listed the files in this directory, we found that there was only one soft link file of 000-default. The actual connection was the default file in the /etc/apache2/sites-available directory. It is not difficult to see that the file name of the file does not contain Does not contain "." or "#". So of course this file must be included in the configuration file apache2.conf. Open the file and find that it is actually a configuration file for a virtual host. However, since the virtual host in the file is *, it is actually a general configuration file. If we want to create a virtual host, then we need to change the file to look like the following:

<VirtualHost *:80>
ServerName www.firehare.com
ServerAdmin admin@mail.firehare.com
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
Allow from all
# This directive allows us to have apache2&#39;s default start page
# in /apache2-default/, but still have / go to the right place
# Commented out for Ubuntu
#RedirectMatch ^/$ /apache2-default/
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
Copy after login

Let’s analyze the setting statements related to the virtual host in the above settings:

`NameVirtualHost :80`: means What we want to do is a name-based virtual host, the listening port is 80.

` and `: indicates the configuration of a virtual host. Note that if the port number is specified in the above field, it must also be specified here

`ServerName www.firehare.com`: Set the domain name of the virtual host, www.firehare.com can be any domain name you registered

`ServerAdmin admin@mail .firehare.com`: Set the email of the network administrator of the virtual host

`DocumentRoot /var/www/`: Set the home directory path of the virtual host

`ErrorLog /var/log/apache2/error.log`: Set The error message of the virtual host

`CustomLog /var/log/apache2/access.log combined`: Set the access information of the virtual host

In this way, we have configured a virtual host www.firehare.com. But since this is the default configuration, after Apache2 restarts, no matter you enter any domain name pointing to this host in the DNS server, you will be directed to the /var/www directory pointed to by the default configuration of www.firehare.com. Unless the domain name is used by other virtual host configurations, for example, we have also configured edunuke.firehare.com to point to this machine and configured the corresponding virtual host. In this case, entering the domain name edunuke.firehare.com will be included in the directory corresponding to the domain name. middle.

Further explanation

In order to explain clearly, let’s add another virtual host site example.com. First, create a file edunuke in the /etc/apache2/sites-available/ directory and edit the file:

<VirtualHost *:80>
ServerName edunuke.example.com
ServerAdmin edunuke@mail.example.com
DocumentRoot "/var/www/edunuke/"
ErrorLog "/var/log/apache2/edunuke_errors.log"
CustomLog "/var/log/apache2/edunuke_accesses.log" common    
</VirtualHost>
Copy after login

The specific meaning of the settings is the same It’s similar to the above, so I won’t say more about it. Then run the command:

sudo a2ensite edunuke
Copy after login

In this case, the virtual host site edunuke.example.com has been installed. At this time, you can also find an additional soft link to /etc/apache2/sites-available/edunuke in the /etc/apache2/sites-enabled/ directory. The next step is to restart Apache2 to make the virtual host site run:

sudo /etc/init.d/apache2 restart  这里可以使用reload 重新加载
Copy after login

In this way, if you enter edunuke.example.com on the browser, it will be pointed to the /var/www/edunuke directory, and other input will point to the local machine The domain name will point to the /var/www directory in the default configuration. Friends who are familiar with Apache2 will ask why it is so troublesome. Isn’t it possible to put it in a file? Why use two files? It's actually very simple, because if I want to maintain the edunuke site, I only need to run the command:

sudo a2dissite edunuke
sudo /etc/init.d/apache2 restart
Copy after login

. This way I can maintain the edunuke site without affecting the normal operation of other sites.

Advanced configuration

上面谈了一下简单的虚拟主机配置方法。这个基本上能满足我们大部分的需要。但如果要是安装 Zope+Plone 的话,上面的这点设置是远远不够的,由于 Zope+Plone 结构所采用的端口并非是80端口,所以我们还得做端口重定向。为了能够做这个,我们得激活 Rewrite 和 Proxy 两个模块。激活模块很简单,同站点配置目录一样,在 Apache2 中也有两个模块配置目录:mods-available 和 mods-enabled。在 mods-available 目录中的是所有可用的模块,而在 mods-enabled 目录中的则是已被安装到 Apache2 中的模块。由于在 mods-available 目录中已经有了 Rewrite 和 Proxy 模块的配置引导文件,所以只需要简单地将其安装到 Apache2 中即可。使用命令:

sudo a2enmod rewrite
sudo a2enmod proxy
Copy after login

然后,添加虚拟主机站点 plone.example.com,同 edunuke 站点创建相似在/etc/apache2/sites-available/ 目录中建立一个文件 plone。显然这个文件名中是没有 "." 或 "#" 这两个字符的了。然后编辑该文件:

<VirtualHost plone.example.com:80>
ServerName plone.example.com
ServerAdmin plone@mail.example.com
ErrorLog "/var/log/apache2/plone_errors.log"
CustomLog "/var/log/apache2/plone_accesses.log" common
RewriteEngine on
RewriteRule ^/(.*) http://127.0.0.1:8081/VirtualHostBase/http/plone.firehare.com:80/plone/VirtualHostRoot/$1 [L,P]
<Proxy *>
Order Deny,Allow
Deny from all
Allow from all
</Proxy>
</VirtualHost>
Copy after login

这样就安装好了 plone.example.com 虚拟主机站点,可以在浏览器中地址栏中输入 http://plone.example.com 就可以重定向到 Zope+Plone 站点去了。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Android TV Box gets unofficial Ubuntu 24.04 upgrade Android TV Box gets unofficial Ubuntu 24.04 upgrade Sep 05, 2024 am 06:33 AM

For many users, hacking an Android TV box sounds daunting. However, developer Murray R. Van Luyn faced the challenge of looking for suitable alternatives to the Raspberry Pi during the Broadcom chip shortage. His collaborative efforts with the Armbia

PHP Framework Performance Comparison: The Ultimate Showdown of Speed ​​vs. Efficiency PHP Framework Performance Comparison: The Ultimate Showdown of Speed ​​vs. Efficiency Apr 30, 2024 pm 12:27 PM

According to benchmarks, Laravel excels in page loading speed and database queries, while CodeIgniter excels in data processing. When choosing a PHP framework, you should consider application size, traffic patterns, and development team skills.

How to conduct concurrency testing and debugging in Java concurrent programming? How to conduct concurrency testing and debugging in Java concurrent programming? May 09, 2024 am 09:33 AM

Concurrency testing and debugging Concurrency testing and debugging in Java concurrent programming are crucial and the following techniques are available: Concurrency testing: Unit testing: Isolate and test a single concurrent task. Integration testing: testing the interaction between multiple concurrent tasks. Load testing: Evaluate an application's performance and scalability under heavy load. Concurrency Debugging: Breakpoints: Pause thread execution and inspect variables or execute code. Logging: Record thread events and status. Stack trace: Identify the source of the exception. Visualization tools: Monitor thread activity and resource usage.

How to add a server in eclipse How to add a server in eclipse May 05, 2024 pm 07:27 PM

To add a server to Eclipse, follow these steps: Create a server runtime environment Configure the server Create a server instance Select the server runtime environment Configure the server instance Start the server deployment project

The evasive module protects your website from application layer DOS attacks The evasive module protects your website from application layer DOS attacks Apr 30, 2024 pm 05:34 PM

There are a variety of attack methods that can take a website offline, and the more complex methods involve technical knowledge of databases and programming. A simpler method is called a "DenialOfService" (DOS) attack. The name of this attack method comes from its intention: to cause normal service requests from ordinary customers or website visitors to be denied. Generally speaking, there are two forms of DOS attacks: the third and fourth layers of the OSI model, that is, the network layer attack. The seventh layer of the OSI model, that is, the application layer attack. The first type of DOS attack - the network layer, occurs when a large number of of junk traffic flows to the web server. When spam traffic exceeds the network's ability to handle it, the website goes down. The second type of DOS attack is at the application layer and uses combined

Application of algorithms in the construction of 58 portrait platform Application of algorithms in the construction of 58 portrait platform May 09, 2024 am 09:01 AM

1. Background of the Construction of 58 Portraits Platform First of all, I would like to share with you the background of the construction of the 58 Portrait Platform. 1. The traditional thinking of the traditional profiling platform is no longer enough. Building a user profiling platform relies on data warehouse modeling capabilities to integrate data from multiple business lines to build accurate user portraits; it also requires data mining to understand user behavior, interests and needs, and provide algorithms. side capabilities; finally, it also needs to have data platform capabilities to efficiently store, query and share user profile data and provide profile services. The main difference between a self-built business profiling platform and a middle-office profiling platform is that the self-built profiling platform serves a single business line and can be customized on demand; the mid-office platform serves multiple business lines, has complex modeling, and provides more general capabilities. 2.58 User portraits of the background of Zhongtai portrait construction

How to deploy and maintain a website using PHP How to deploy and maintain a website using PHP May 03, 2024 am 08:54 AM

To successfully deploy and maintain a PHP website, you need to perform the following steps: Select a web server (such as Apache or Nginx) Install PHP Create a database and connect PHP Upload code to the server Set up domain name and DNS Monitoring website maintenance steps include updating PHP and web servers, and backing up the website , monitor error logs and update content.

How to implement PHP security best practices How to implement PHP security best practices May 05, 2024 am 10:51 AM

How to Implement PHP Security Best Practices PHP is one of the most popular backend web programming languages ​​used for creating dynamic and interactive websites. However, PHP code can be vulnerable to various security vulnerabilities. Implementing security best practices is critical to protecting your web applications from these threats. Input validation Input validation is a critical first step in validating user input and preventing malicious input such as SQL injection. PHP provides a variety of input validation functions, such as filter_var() and preg_match(). Example: $username=filter_var($_POST['username'],FILTER_SANIT

See all articles