Apache: Difference between revisions

From vwiki
Jump to navigation Jump to search
m (Added php path)
(Added "Host Multiple Sites")
Line 11: Line 11:
| <code> /etc/php5/apache2 </code> || PHP config file
| <code> /etc/php5/apache2 </code> || PHP config file
|}
|}
= Configuration =
== Host Multiple Sites ==
There are various methods that can be used to enable you to host multiple websites on the same server.  This is but one, and allows you to host different domains on the same server.
You'll need a publicly accessible server running Apache, and the ability to create DNS entries for the domains you want to host.  In this example we're creating two websites, called ''web1'' and ''web2'', on a server with IP address ''123.10.10.2''
Before you configure the webserver, you need to set-up DNS correctly, for this example this could be done in one of two ways...
* A records only
** <code> web1.domain.com -- A record --> 123.10.10.2</code>
** <code> web2.domain.com -- A record --> 123.10.10.2</code>
* A and CNAME records
** <code> web.domain.com -- A record --> 123.10.10.2</code>
** <code> web1.domain.com -- CNAME record --> web.domain.com</code>
** <code> web2.domain.com -- CNAME record --> web.domain.com</code>
...how you choose to do it is up to you, and depends on what changes you might expect to make down the line.  If you have a small set-up its probably easiest to go with purely A-records, but where you have a larger number of websites and web-hosts, it can be easier to manage with all your web-hosts having A-records and all web-sites having CNAME-records pointing to the host that they're running from.
Either way, you should be able to ping the addresses of either website and get the correct IP address back.
Next you should check that you web server is working correctly.  If its a new install, you should be able to browse to the server's IP address and get a basic "It works!" page back.  This site, or any site that's using the root of your web-server will need to be (re)moved.
Now to create the web-sites...
# '''If you have an existing site that's served from the root of your webserver, this needs to be removed 1st.''' So to remove the default site that's installed with Apache...
## Disable the web-site by removing the link to its config from <code>sites-enabled</code>
##* <code> rm /etc/apache2/sites-enabled/000-default</code>
##* <code> service apache2 restart</code> (you may get a warning about ''no VirtualHosts'')
## Test by browsing to your web-server's IP address, no webpage should be returned
# '''Now create the required folders...'''
## Create sub-folders for your web-sites, eg
##* <code> mkdir /var/www/web1.domain.com </code>
##* <code> mkdir /var/www/web2.domain.com </code>
## Create sub-folders for your web-sites' logs, eg
##* <code> mkdir /var/log/apache2/web1.domain.com </code>
##* <code> mkdir /var/log/apache2/web2.domain.com </code>
# '''Now create the required configs...'''
## Create a config for web1, using the example file contents below
##* <code> vi /etc/apache2/sites-available/web1.domain.com </code>
## Create a config for web2, using the example file contents below, but edit so that web1 becomes web2
##* <code> vi /etc/apache2/sites-available/web2.domain.com </code>
# '''Now create some test content, and enable...'''
## Copy the default index.html to your new web-site folders, and edit so that they identify the web-site they're in
##* <code> cp /var/www/index.html /var/www/web1.domain.com/ </code> and edit
##* <code> cp /var/www/index.html /var/www/web2.domain.com/ </code> and edit
## Enable the websites
##* <code> cd /etc/apache2/sites-enabled </code>
##* <code> ln -s ../sites-available/web1.domain.com web1.domain.com</code>
##* <code> ln -s ../sites-available/web2.domain.com web2.domain.com</code>
## Restart the Apache service to apply
##* <code> service apache2 restart</code>
# '''Test - you should now have two separately accessible websites..!'''
<VirtualHost *:80>
        ServerAdmin info@domain.com
        ServerName  web1.domain.com
        # Indexes + Directory Root.
        DirectoryIndex index.php
        DocumentRoot /var/www/web1.domain.com/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        # Logfiles
        ErrorLog  /var/log/apache2/web1.domain.com/error.log
        CustomLog /var/log/apache2/web1.domain.com/access.log combined
</VirtualHost


== Log Rotation ==
== Log Rotation ==
Line 40: Line 109:
#* <code> service apache2 restart </code>
#* <code> service apache2 restart </code>


== Troubleshooting ==
= Troubleshooting =
=== NameVirtualHost *:80 has no VirtualHosts ===
== NameVirtualHost *:80 has no VirtualHosts ==
On restarting Apache you get presented with the following warning
On restarting Apache you get presented with the following warning
  [Thu Jan 12 10:58:20 2012] [warn] NameVirtualHost *:80 has no VirtualHosts
  [Thu Jan 12 10:58:20 2012] [warn] NameVirtualHost *:80 has no VirtualHosts

Revision as of 17:28, 6 February 2012

File path What's there
/var/www Default web root
/var/log/apache Logs
/etc/apache2 Config files
/etc/php5/apache2 PHP config file

Configuration

Host Multiple Sites

There are various methods that can be used to enable you to host multiple websites on the same server. This is but one, and allows you to host different domains on the same server.

You'll need a publicly accessible server running Apache, and the ability to create DNS entries for the domains you want to host. In this example we're creating two websites, called web1 and web2, on a server with IP address 123.10.10.2

Before you configure the webserver, you need to set-up DNS correctly, for this example this could be done in one of two ways...

  • A records only
    • web1.domain.com -- A record --> 123.10.10.2
    • web2.domain.com -- A record --> 123.10.10.2
  • A and CNAME records
    • web.domain.com -- A record --> 123.10.10.2
    • web1.domain.com -- CNAME record --> web.domain.com
    • web2.domain.com -- CNAME record --> web.domain.com

...how you choose to do it is up to you, and depends on what changes you might expect to make down the line. If you have a small set-up its probably easiest to go with purely A-records, but where you have a larger number of websites and web-hosts, it can be easier to manage with all your web-hosts having A-records and all web-sites having CNAME-records pointing to the host that they're running from.

Either way, you should be able to ping the addresses of either website and get the correct IP address back.

Next you should check that you web server is working correctly. If its a new install, you should be able to browse to the server's IP address and get a basic "It works!" page back. This site, or any site that's using the root of your web-server will need to be (re)moved.

Now to create the web-sites...

  1. If you have an existing site that's served from the root of your webserver, this needs to be removed 1st. So to remove the default site that's installed with Apache...
    1. Disable the web-site by removing the link to its config from sites-enabled
      • rm /etc/apache2/sites-enabled/000-default
      • service apache2 restart (you may get a warning about no VirtualHosts)
    2. Test by browsing to your web-server's IP address, no webpage should be returned
  2. Now create the required folders...
    1. Create sub-folders for your web-sites, eg
      • mkdir /var/www/web1.domain.com
      • mkdir /var/www/web2.domain.com
    2. Create sub-folders for your web-sites' logs, eg
      • mkdir /var/log/apache2/web1.domain.com
      • mkdir /var/log/apache2/web2.domain.com
  3. Now create the required configs...
    1. Create a config for web1, using the example file contents below
      • vi /etc/apache2/sites-available/web1.domain.com
    2. Create a config for web2, using the example file contents below, but edit so that web1 becomes web2
      • vi /etc/apache2/sites-available/web2.domain.com
  4. Now create some test content, and enable...
    1. Copy the default index.html to your new web-site folders, and edit so that they identify the web-site they're in
      • cp /var/www/index.html /var/www/web1.domain.com/ and edit
      • cp /var/www/index.html /var/www/web2.domain.com/ and edit
    2. Enable the websites
      • cd /etc/apache2/sites-enabled
      • ln -s ../sites-available/web1.domain.com web1.domain.com
      • ln -s ../sites-available/web2.domain.com web2.domain.com
    3. Restart the Apache service to apply
      • service apache2 restart
  5. Test - you should now have two separately accessible websites..!
<VirtualHost *:80>
       ServerAdmin info@domain.com
       ServerName  web1.domain.com

       # Indexes + Directory Root.
       DirectoryIndex index.php
       DocumentRoot /var/www/web1.domain.com/

       <Directory />
               Options FollowSymLinks
               AllowOverride None
       </Directory>

       # Logfiles
       ErrorLog  /var/log/apache2/web1.domain.com/error.log
       CustomLog /var/log/apache2/web1.domain.com/access.log combined

</VirtualHost

Log Rotation

There are two ways to ensure your logs get rotated...

  • logrotate - The standard, cross application log rotation solution. Its very reliable and easy to configure, but requires that Apache is restarted every time your log is rotated so that it follows the log switch around.
  • rotatelog pipe - Has more overhead as logs are piped to a child process to handle, but doesn't require Apache restarts.

rotatelog

Edit you existing config file to redirect logging through the child rotatelogs process, this will probably be found in either your site config (EG in /etc/apache2/sites-enabled) or your Apache service config (EG in /etc/apache2/ )

ErrorLog  "|/usr/sbin/rotatelogs /var/log/apache2/error-%Y-%m-%d.log 86400"
CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access-%Y-%m-%d.log 86400" combined

Check that the path for rotatelogs is correct (search using find / -iname rotatelogs )

Restart Apache, perform a test page load on your site (that would generate log entries), and check that a log file has been generated as per your config

User/pass restrictions

  1. Create a user/pass entry in a file for a username
    • htpasswd -c /usr/local/apache2/conf/htusers username
  2. Add the relavent bits to the /etc/apache2/httpd.conf file, eg
    • <Directory />
    • AuthType Basic
    • AuthName "Restricted Access"
    • AuthUserFile /usr/local/apache2/conf/htusers
    • Require valid-user
    • </Directory>
  3. Restart the apache service
    • service apache2 restart

Troubleshooting

NameVirtualHost *:80 has no VirtualHosts

On restarting Apache you get presented with the following warning

[Thu Jan 12 10:58:20 2012] [warn] NameVirtualHost *:80 has no VirtualHosts

This is generally caused by duplicate NameVirtualHost entries in your config.

Perform the following in /etc/apache2 folder to identify where the entries and delete the unwanted duplicates..

grep NameVir * -R