Difference between revisions of "Apache 2 HTTP virtual host"
(Created page with "Category:Linux =Requirements= Before going through this tutorial, I recommend you to setup: * MySQL server * SSL server * LDAP server =Installation= ...") |
|||
(One intermediate revision by the same user not shown) | |||
Line 2: | Line 2: | ||
+ | =Preparation= | ||
− | + | Initialize configuration | |
− | + | <syntaxhighlight lang="bash"> | |
− | + | cd /etc/apache2/sites-available/ | |
− | + | </syntaxhighlight> | |
− | |||
+ | Create target directory | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | mkdir -p /var/www/myServer | ||
+ | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | Prepare the log files | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | + | mkdir -p /var/log/apache2/myServer | |
+ | touch /var/log/apache2/myServer/access.log | ||
+ | touch /var/log/apache2/myServer/error.log | ||
+ | chmod -R 660 /var/log/apache2/myServer/* | ||
+ | chown -R www-data:www-data /var/log/apache2/myServer/* | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | Copy default index file | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | + | cp /var/www/html/index.html /var/www/myServer | |
+ | chown -R www-data:www-data /var/log/apache2/myServer/* | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | =Configuration= | ||
− | + | Init configuration | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | + | cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myServer.conf | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | '''Edit configuration''' | |
− | |||
− | |||
− | |||
− | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | + | vim /etc/apache2/sites-available/myServer | |
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | To begin the virtual host, write the following lines: | |
+ | * Adjust the settings to your own configuration | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | <VirtualHost 192.168.0.100:80> → Choose the best options for your needs | ||
+ | <VirtualHost *:80> | ||
+ | ############################# | ||
+ | # Server main properties | ||
+ | ############################# | ||
− | + | ServerName myServer | |
+ | ServerAlias www.myServer *.myServer | ||
+ | ServerAdmin webmaster@domain | ||
+ | |||
+ | # Logs settings | ||
+ | LogLevel Warn | ||
+ | CustomLog ${APACHE_LOG_DIR}/myServer/access.log combined | ||
+ | ErrorLog ${APACHE_LOG_DIR}/myServer/error.log | ||
− | + | ############################# | |
+ | # Root folder properties | ||
+ | ############################# | ||
+ | DocumentRoot /var/www/myServer | ||
− | < | + | # SECURITY: forbid access to .htaccess so no outsider can ever change it |
− | + | <Files ~ "^\.ht"> | |
− | + | ## Old Apache2 (before 2.4) syntax | |
+ | Order allow,deny | ||
+ | deny from all | ||
+ | ## Apache 2.4 syntax | ||
+ | Require all denied | ||
+ | </Files> | ||
+ | # Restrict access to server root | ||
+ | <Directory /> | ||
+ | Options FollowSymLinks | ||
+ | AllowOverride None | ||
+ | Require all denied | ||
+ | </Directory> | ||
− | |||
− | < | + | # Virtual host root directory |
− | + | <Directory /var/www/myServer> | |
− | + | Options Indexes FollowSymLinks MultiViews | |
− | + | AllowOverride None | |
− | |||
− | |||
− | |||
− | |||
− | |||
+ | ## Old Apache2 (before 2.4) syntax | ||
+ | Order allow,deny | ||
+ | allow from all | ||
+ | |||
+ | ## Apache 2.4 | ||
+ | Require all granted | ||
+ | </Directory> | ||
− | |||
− | + | ############################# | |
− | + | # Other configuration | |
− | + | # Alias, proxy redirections, CGI scripts, Directory, etc. | |
+ | ############################# | ||
− | |||
− | + | </VirtualHost> | |
− | |||
− | < | ||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | + | =Enable / disable virtual host(s)= | |
+ | '''Virtual Host desactivation''' | ||
− | + | If you're listening on '''*:80''' then you should probably disable the default virtual host before enabling yours! | |
− | |||
− | |||
− | |||
− | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | + | a2dissite 000-default | |
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | |||
− | |||
+ | '''Virtual Host activation''' | ||
− | + | To activate a Virtual Host, just type | |
+ | <syntaxhighlight lang="bash"> | ||
+ | a2ensite myServer | ||
+ | </syntaxhighlight> | ||
− | + | Then, restart your web server | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | + | /etc/init.d/apache2 restart | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | Check your server! You should see your "index.html" page. | |
− |
Latest revision as of 17:51, 8 August 2014
Preparation
Initialize configuration
cd /etc/apache2/sites-available/
Create target directory
mkdir -p /var/www/myServer
Prepare the log files
mkdir -p /var/log/apache2/myServer
touch /var/log/apache2/myServer/access.log
touch /var/log/apache2/myServer/error.log
chmod -R 660 /var/log/apache2/myServer/*
chown -R www-data:www-data /var/log/apache2/myServer/*
Copy default index file
cp /var/www/html/index.html /var/www/myServer
chown -R www-data:www-data /var/log/apache2/myServer/*
Configuration
Init configuration
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myServer.conf
Edit configuration
vim /etc/apache2/sites-available/myServer
To begin the virtual host, write the following lines:
- Adjust the settings to your own configuration
<VirtualHost 192.168.0.100:80> → Choose the best options for your needs
<VirtualHost *:80>
#############################
# Server main properties
#############################
ServerName myServer
ServerAlias www.myServer *.myServer
ServerAdmin webmaster@domain
# Logs settings
LogLevel Warn
CustomLog ${APACHE_LOG_DIR}/myServer/access.log combined
ErrorLog ${APACHE_LOG_DIR}/myServer/error.log
#############################
# Root folder properties
#############################
DocumentRoot /var/www/myServer
# SECURITY: forbid access to .htaccess so no outsider can ever change it
<Files ~ "^\.ht">
## Old Apache2 (before 2.4) syntax
Order allow,deny
deny from all
## Apache 2.4 syntax
Require all denied
</Files>
# Restrict access to server root
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
# Virtual host root directory
<Directory /var/www/myServer>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
## Old Apache2 (before 2.4) syntax
Order allow,deny
allow from all
## Apache 2.4
Require all granted
</Directory>
#############################
# Other configuration
# Alias, proxy redirections, CGI scripts, Directory, etc.
#############################
</VirtualHost>
Enable / disable virtual host(s)
Virtual Host desactivation
If you're listening on *:80 then you should probably disable the default virtual host before enabling yours!
a2dissite 000-default
Virtual Host activation
To activate a Virtual Host, just type
a2ensite myServer
Then, restart your web server
/etc/init.d/apache2 restart
Check your server! You should see your "index.html" page.