Home >Operation and Maintenance >Apache >How do I create virtual hosts in Apache for multiple websites?
This article guides configuring Apache virtual hosts for multiple websites. It details creating
blocks specifying ServerName, ServerAlias, and DocumentRoot, along with security considerations like directory permissions,
Creating virtual hosts in Apache allows you to host multiple websites from a single server. This is achieved by configuring Apache to respond differently based on the incoming request's domain name or IP address. Here's a step-by-step guide:
/etc/apache2/apache2.conf
(Debian/Ubuntu), /etc/httpd/conf/httpd.conf
(Red Hat/CentOS), or /etc/httpd/conf/extra/httpd-vhosts.conf
(often preferred for virtual host configurations). Use a text editor with root privileges (like sudo nano
on Linux).<virtualhost></virtualhost>
blocks for each website. Each block defines the settings for a specific virtual host. A basic example looks like this:<code class="apache"><virtualhost> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com <directory> AllowOverride All Require all granted </directory> </virtualhost></code>
ServerName
: The primary domain name for this virtual host.ServerAlias
: Alternative domain names that should point to this virtual host.DocumentRoot
: The directory containing the website's files. Ensure this directory exists.
<directory></directory>
: Specifies permissions for the DocumentRoot directory. AllowOverride All
allows .htaccess
files to override some settings, while Require all granted
allows access for all. Use caution with AllowOverride All
in production environments.
<virtualhost></virtualhost>
block for each website you want to host, changing the ServerName
, ServerAlias
, and DocumentRoot
accordingly.a2ensite example.com
(replacing example.com
with your site's name) followed by sudo systemctl reload apache2
. On Red Hat/CentOS, you might need to restart Apache using sudo systemctl restart httpd
.Security is paramount when hosting multiple websites on a single server. Here are key considerations:
chmod 755
for directories and chmod 644
for files) to prevent unauthorized access or modification. Avoid overly permissive settings like 777
..htaccess
files can introduce security vulnerabilities if not carefully managed. Avoid using them if possible, and if you must use them, carefully review and restrict the directives allowed via AllowOverride
.You can easily configure different ports and domains for each virtual host within the <virtualhost></virtualhost>
directive.
To use a different port, specify it after the *
in the VirtualHost
declaration. For example, to use port 8080 for a virtual host:
<code class="apache"><virtualhost> ServerName example.com:8080 # ... other directives ... </virtualhost></code>
Note that clients will need to access this website using example.com:8080
. Using non-standard ports is generally less common now that HTTPS is prevalent. However, it can be useful for testing or specific applications.
To use different domains, simply specify them in the ServerName
and ServerAlias
directives as shown in the first section. Apache will match the incoming request's Host header to determine which virtual host to use. This is the standard and preferred method.
Yes, you can use Apache virtual hosts with different PHP versions for each website. This typically involves using multiple PHP installations and configuring Apache to use the appropriate PHP handler for each virtual host.
The exact method depends on your system and how PHP is installed. Common approaches include:
mod_php
or php-fpm
) for each virtual host, specifying the path to the correct PHP executable.suexec
(for increased security): Using suexec
enhances security by running each virtual host's PHP scripts under a different user account. This prevents one compromised website from affecting others.Configuring these setups requires careful attention to detail and familiarity with your server's environment and PHP configuration. Refer to your system's documentation and PHP-FPM documentation for detailed instructions. It's generally more complex than basic virtual host setup.
The above is the detailed content of How do I create virtual hosts in Apache for multiple websites?. For more information, please follow other related articles on the PHP Chinese website!