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=
  
=Requirements=
+
Initialize configuration
  
Before going through this tutorial, I recommend you to setup:
+
<syntaxhighlight lang="bash">
* [[MySQL server]]
+
cd /etc/apache2/sites-available/
* [[SSL server]]
+
</syntaxhighlight>
* [[LDAP server]]
 
  
  
 +
Create target directory
  
 +
<syntaxhighlight lang="bash">
 +
mkdir -p /var/www/myServer
 +
</syntaxhighlight>
  
=Installation=
 
 
 
==Apache 2==
 
 
This will install web server + PHP + Perl + all required libraries.
 
  
===Apache2 core===
+
Prepare the log files
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt-get install apache2 apache2-mpm-prefork apache2-utils ssl-cert
+
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>
  
  
===Additional libraries===
+
Copy default index file
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt-get install libapache2-mod-fcgid libruby
+
cp /var/www/html/index.html /var/www/myServer
 +
chown -R www-data:www-data /var/log/apache2/myServer/*
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
===Doc===
 
 
<syntaxhighlight lang="bash">
 
apt-get install apache2-doc
 
</syntaxhighlight>
 
  
 +
=Configuration=
  
===Perl===
+
Init configuration
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt-get install libapache2-mod-perl2 libapache2-mod-perl2-doc
+
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myServer.conf
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
===SNMP===
+
'''Edit configuration'''
 
 
Sometimes you might encounter some SNMP errors on latest Debian based distributions.
 
 
 
In that case you have to install a new package and run it.
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
apt-get install snmp-mibs-downloader
+
vim /etc/apache2/sites-available/myServer
download-mibs
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
source: http://www.podciborski.co.uk/miscellaneous/snmp-cannot-find-module/
+
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
 +
#############################
  
==PHP 5==
+
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
  
  
===Core===
+
#############################
 +
        # Root folder properties
 +
#############################
 +
DocumentRoot /var/www/myServer
  
<syntaxhighlight lang="bash">
+
        # SECURITY: forbid access to .htaccess so no outsider can ever change it
apt-get install libapache2-mod-php5 php5 php5-common
+
        <Files ~ "^\.ht">
</syntaxhighlight>
+
                ## 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>
  
===Modules PHP5===
 
  
<syntaxhighlight lang="bash">
+
        # Virtual host root directory
apt-get install php5-cli php5-cgi
+
<Directory /var/www/myServer>
apt-get install php5-curl php5-xmlrpc php5-xsl php5-dev php-pear
+
Options Indexes FollowSymLinks MultiViews
apt-get install php5-mysql
+
AllowOverride None
apt-get install php5-memcache php5-xcache
 
apt-get install php5-mhash php-auth php5-mcrypt mcrypt
 
apt-get install php5-imap
 
apt-get install php5-snmp
 
</syntaxhighlight>
 
  
 +
                ## Old Apache2 (before 2.4) syntax
 +
Order allow,deny
 +
allow from all
 +
               
 +
                ## Apache 2.4
 +
                Require all granted 
 +
</Directory>
  
===Image Magick===
 
  
<syntaxhighlight lang="bash">
+
#############################
apt-get install php5-gd php5-imagick imagemagick
+
        # Other configuration
</syntaxhighlight>
+
        # Alias, proxy redirections, CGI scripts, Directory, etc.
 +
#############################
  
  
===Configuration===
 
  
Edit PHP config file:
+
</VirtualHost>
 
 
<syntaxhighlight lang="bash">
 
vim /etc/php5/apache2/php.ini
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Add / uncomment the following lines in Dynamic extensions area (~ line 865)
 
* extension=mysql.so
 
* extension=gd.so
 
  
  
!! Note this is NOT required on Ubuntu 14.04 because these modules are enabled by default !!
+
=Enable / disable virtual host(s)=
  
  
 +
'''Virtual Host desactivation'''
  
==Firewall==
+
If you're listening on '''*:80''' then you should probably disable the default virtual host before enabling yours!
 
 
You have to open the following ports:
 
* Port 80 = HTTP
 
* Port 443 = HTTPS
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
$IPTABLES -A INPUT -p tcp -m state -i eth0 --dport 80 -j ACCEPT
+
a2dissite 000-default
$IPTABLES -A INPUT -p tcp -m state -i eth0 --dport 443 -j ACCEPT
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Restart the firewall
 
  
<syntaxhighlight lang="bash">
 
/etc/init.d/firewall restart
 
</syntaxhighlight>
 
  
  
 +
'''Virtual Host activation'''
  
==Test your installation==
+
To activate a Virtual Host, just type
  
 +
<syntaxhighlight lang="bash">
 +
a2ensite  myServer
 +
</syntaxhighlight>
  
Restart the Apache2 server
+
Then, restart your web server
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
service apache2 restart
+
/etc/init.d/apache2 restart
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
 
+
Check your server! You should see your "index.html" page.
You can now test your installation by going to 'http://localhost' or 'http://myServer'. You should see the default 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.