September 27, 2009

Apache virtual host for virtual domain hosting

At an environtment virtual domain hosting using apache as its(the webserver besides we are configuration name server ( aka bind) we also needs setting virtual host at apache that redirect ( request domain) what thrown from bind and then is continued to apache port server 80 will be pointed at virtual domain/path appropriate for account/domain the intended.

1. Create an special dir/folder include client datas

# create primary group
groupadd client

# create user and folder structure for client 1

mkdir -p /home/vhost/client1/www
mkdir -p /home/vhost/client1/logs
mkdir -p /home/vhost/client1/cgi-bin
useradd client1 -g client -d /home/vhost/client1/
passwd client1
chown client1.client -R  /home/vhost/client1

# create user and folder structure for client 2


mkdir -p /home/vhost/client2/www
mkdir -p /home/vhost/client2/logs
mkdir -p /home/vhost/client2/cgi-bin
useradd client2 -g client -d /home/vhost/client2/
passwd client2
chown client2.client -R  /home/vhost/client2

2. add domain for user account to  database's DNS record
3. Pointing domain name (virtual host) pada apache

mkdir /home/vhost-conf/
mc -e /etc/httpd/httpd.conf

# add include httpd.conf for load
# all *.conf in folder  /home/vhost-conf

### Use name-based virtual hosting.
DirectoryIndex index.html index.htm index.php
NameVirtualHost *:80
NameVirtualHost *:443
Include /home/vhost-conf/*.conf
LoadModule ssl_module lib/httpd/modules/mod_ssl.so

4.activing ssl
(http://slacksite.com/apache/certificate.php )




mkdir /etc/httpd/ssl
cd /etc/httpd/ssl
openssl genrsa -des3 -rand file1:file2:file3:file4:file5 -out server.key 1024
openssl rsa -in server.key -out server.pem
openssl req -new -key server.key -out server.csr
openssl x509 -req -days 60 -in server.csr -signkey server.key -out server.crt

5. vhost config for client 1

mc -e /home/vhost-conf/client1.conf

#paste this code to client1.conf

<VirtualHost *:80>
ServerAdmin client1@client1-domain1.com
DocumentRoot /home/vhost/client1/www/
ServerName client1-domain1.com
ServerAlias client1-domain1.com *.client1-domain1.com
ErrorLog /home/vhost/client2/logs/client1-domain1.com-error_log
CustomLog /home/vhost/client2/logs/client1-domain1.com-access_log common
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
<Directory "/home/vhost/client1/www/">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
ScriptAlias /cgi-bin/ "/home/vhost/client1/cgi-bin"
<Directory "/home/vhost/client1/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerName client1-domain1.com
DocumentRoot /home/vhost/client1/www/
CustomLog /home/vhost/client2/logs/client1-domain1.com-ssl-access.log combined
ErrorLog /home/vhost/client2/logs/client1-domain1.com-ssl-error.log
SSLEngine on
SSLCertificateFile /etc/httpd/ssl/server.crt
SSLCertificateKeyFile /etc/httpd/ssl/server.pem
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
</VirtualHost>

#change permision client1.conf
chown client1.client /home/vhost-conf/client1.conf

6.  repeat step 4 for others client

7.  restart apache
killall httpd
/etc/rc.d/rc.httpd restart
Disqus Comments