Tuesday, April 6, 2010

Apache HTTPD Virtual Hosting Setup in Step By Step Instructions

A buddy of mine is new to server administration and one of the first things I am going to show him since he is a web developer is how to setup a virtual host in Apache on CentOS 5 allowing you to run multiple web sites on a single IP address. Read the steps carefully and take your time if this is your first time doing this, it is a simple task that can easily go wrong if you rush it.


  1. Add your server IP to GoDaddy or other domain registrar's DNS settings.
    For GoDaddy this is located by navigating to Domain Manager -> Your domain -> Total DNS Control then editing the "A (Host)" record.
  2. Login to your server using putty.exe or some other SSH client.


  3. Type:
    ls /var/www
    Then look to see if there is currently a folder named "vhosts", if there isn't then type:
    mkdir /var/www/vhosts
    Next add a domain by typing:
    mkdir /var/www/vhosts/www.yournewdomain.com
    Then to add a logging directory type:
    mkdir /var/www/vhosts/www.yournewdomain.com/logs
    Then to add an error log file type:
    touch /var/www/vhosts/www.yournewdomain.com/logs/error-log
    Then to give apache permissions to the logging folder type:
    chown -R apache:apache /var/www/vhosts/www.yournewdomain.com/logs


  4. Type:
    vi /etc/httpd/conf/httpd.conf
    Then hold the "Page Down" key to scroll to the very bottom of the file and then type:
    $a
    Then press the enter key and enter the following lines:

    <VirtualHost *:80>
    ServerAdmin support@yournewdomain.com
    DocumentRoot /var/www/vhosts/www.yournewdomain.com
    ServerName www.yournewdomain.com
    ServerAlias yournewdomain.com
    ErrorLog /var/www/vhosts/www.yournewdomain.com/logs/error-log
    </VirtualHost>
    <Directory /var/www/vhosts/www.yournewdomain.com>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    </Directory>

    And then navigate around using the arrow keys and replace all instances of "yournewdomain" with your new domain name. When "-- INSERT --" shows in the lower left corner of the screen you are in edit mode, to enter edit mode you hit "i" or "a", to exit edit mode hit ESC. When in edit mode keystrokes are inserted into the document, when not in edit mode keystrokes are interpreted as commands. Always be aware if you are in edit mode or not.

    You are done editing so hit ESC to exit edit mode.
    When everything looks right then type the following to save and exit (when out of INSERT mode)
    :wq
    If you made a mistake then type:
    :e!
    This series of keystrokes will undo all changes in vi so you can start your edits over.


  5. Restart apache by typing:
    /sbin/service httpd restart


  6. To setup vsftpd (FTP server) to go to the vhosts folder on login then create a ftp user by typing the following command:

    adduser wwwftp -d /var/www/vhosts -M -p PASSWORD_FOR_USER_HERE -s /sbin/nologin

    And you are done. Use FTP with the user "wwwftp" and the password you set to upload your website files to /var/www/vhosts/www.yournewdomain.com/

1 comments:

Anonymous said...

Thanks A Lot

Post a Comment