Apache On Ubuntu

From KdjWiki

Jump to: navigation, search

Installing

Note: You (may) need to include multiverse repositories (see Ubuntu Setup on a Sony Vaio Guide for help on adding additional repositories)

Get Apache:

 $ sudo apt-get install apache2

Get PHP:

 $ sudo apt-get install php4

Get Apache MySQL module:

 $ sudo apt-get install libapache2-mod-auth-mysql

Get PHP MySQL extension:

 $ sudo apt-get install php4-mysql


Configuring

Add a site:

  • Create the web root folder:
 $ sudo mkdir /var/www/wwwroot
  • Create the site config file:
 $ sudo gedit /etc/apache2/sites-available/mysite
NameVirtualHost *
<VirtualHost *>
	ServerName www.mysite.com
	DocumentRoot /var/www/wwwroot
	<Directory /var/www/wwwroot>
		Options -Indexes FollowSymLinks MultiViews
		AllowOverride All
		<IfModule mod_access.c>
			Order deny,allow
			Allow from all
		</IfModule>
	</Directory>
	LogLevel warn
	ErrorLog /var/log/apache2/error.log
	CustomLog /var/log/apache2/access.log combined
</VirtualHost>

Secure the site:

  • Create a folder to store password file:
 $ sudo mkdir /var/www/passwords
  • Create a password file:
 $ sudo htpasswd2 -c /var/www/passwords/passwords myname

Note: The -c parameter creates the file. If it already exists it will be overwritten!

  • Add additional user:
 $ sudo htpasswd2 /var/www/passwords/passwords myothername
  • Update the site config file:
 $ sudo gedit /etc/apache2/sites-available/mysite
NameVirtualHost *
<VirtualHost *>
	ServerName www.mysite.com
	DocumentRoot /var/www/wwwroot
	<Directory /var/www/wwwroot>
		Options -Indexes FollowSymLinks MultiViews
		AllowOverride All
		AuthType Basic
		AuthName "My Site Name"
		AuthUserFile /var/www/passwords/passwords
		Require valid-user
		<IfModule mod_access.c>
			Order deny,allow
			Allow from all
		</IfModule>
	</Directory>
	LogLevel warn
	ErrorLog /var/log/apache2/error.log
	CustomLog /var/log/apache2/access.log combined
</VirtualHost>
  • Secure password file:

Note: This assumes Apache is running as group www-data. See /etc/apache2/apache2.conf to confirm the user and group apache is running as.

 $ sudo chown root:www-data /var/www/passwords/*
 $ sudo chmod 640 /var/www/passwords/*


Other Tips

Authenticate By Group:

  • Setup as above for valid user
  • Create group file:
 $ sudo sh -c "echo admins: myname myothername > /var/www/passwords/groups
  • Update the site config file:
 $ sudo gedit /etc/apache2/sites-available/mysite
NameVirtualHost *
<VirtualHost *>
	ServerName www.mysite.com
	DocumentRoot /var/www/wwwroot
	<Directory /var/www/wwwroot>
		Options -Indexes FollowSymLinks MultiViews
		AllowOverride All
		AuthType Basic
		AuthName "My Site Name"
		AuthUserFile /var/www/passwords/passwords
		AuthGroupFile /var/www/passwords/groups
		Require group admins
		<IfModule mod_access.c>
			Order deny,allow
			Allow from all
		</IfModule>
	</Directory>
	LogLevel warn
	ErrorLog /var/log/apache2/error.log
	CustomLog /var/log/apache2/access.log combined
</VirtualHost>
  • Secure groups file:

Note: This assumes Apache is running as group www-data. See /etc/apache2/apache2.conf to confirm the user and group apache is running as.

 $ sudo chown root:www-data /var/www/passwords/*
 $ sudo chmod 640 /var/www/passwords/*