Add Sites Apache2

From SysadminPunk Wiki!

Jump to: navigation, search

Contents

Create Apache Site config files

vi /etc/apache2/sites-available/mynewsite
<VirtualHost *:80>
       ServerAdmin webmaster@example.com
       ServerName  www.mynewsite.com
       ServerAlias mynewsite.com

       # Indexes + Directory Root.
       DirectoryIndex index.php
       DocumentRoot /var/www/mynewsite/
       ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

       <Directory "/usr/lib/cgi-bin">
               AllowOverride None
               Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
               Order allow,deny
               Allow from all
       </Directory>

      <Directory /var/www/mynewsite/logs>
              Options None
              AllowOverride None
              Order deny,allow
              Deny from all
      </Directory>

       ErrorLog  /var/www/mynewsite/logs/error.log
       CustomLog  /var/www/mynewsite/logs/access.log combined

</VirtualHost>
  • For Secure Virtual Host:
<VirtualHost *:443>
       ServerAdmin webmaster@example.com
       ServerName  www.mynewsite.com
       ServerAlias mynewsite.com

       # Indexes + Directory Root.
       DirectoryIndex index.php
       DocumentRoot /var/www/mynewsite/
       ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

       <Directory "/usr/lib/cgi-bin">
               AllowOverride None
               Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
               Order allow,deny
               Allow from all
       </Directory>

      <Directory /var/www/mynewsite/logs>
              Options None
              AllowOverride None
              Order deny,allow
              Deny from all
      </Directory>

       ErrorLog  /var/www/mynewsite/logs/error.log
       CustomLog  /var/www/mynewsite/logs/access.log combined

</VirtualHost>

Log Files

  • Before we can activate this config we need to make sure the directories and log files exist.
mkdir -p /var/www/mynewsite/logs
touch /var/www/mynewsite/logs/error.log
touch /var/www/mynewsite/logs/access.log

Activate Site and Restart Apache

  • Now activate/enable the site config using a2ensite
# a2ensite mynewsite
Enabling site mynewsite.
  • Run '/etc/init.d/apache2 reload' to activate new configuration!
# apache2ctl restart
  • If you need to deactivate use a2dissite
# a2dissite mynewsite
Disabling site mynewsite.
  • Run '/etc/init.d/apache2 reload' to activate new configuration!
# apache2ctl restart

Log File Rotation

  • Add log files to logrotate script so they don't get too big!
# vi /etc/logrotate.d/apache2-sites
  • Add something similar to the entry below, substituting the error log directory and the user if a user dir. (In Bold)
/var/www/mynewsite/logs/*.log {
       weekly
       missingok
       rotate 52
       compress
       notifempty
       create 640 www-data www-data
       sharedscripts
}
  • If you need to force the log rotation on a log file thats huge:
# logrotate -f /etc/logrotate.d/apache2-sites
Personal tools