Apache

From gr0x0rd
Revision as of 12:44, 4 September 2013 by Gr0x0rd (talk | contribs) (→‎Configuring apache vhosts)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Installing apache

Apache is the most commonly used web server in the linux world. There are alternatives such as lighttpd and spin-offs such as tomcat; this guide focuses on plan old apache using php and mySQL, otherwise known as a LAMP server (Linux-Apache-mySQL-php).

Before you begin, check that your /etc/portage/package.use file contains the php and mysql USE flags if they aren't in your /etc/make.conf.

$ sudo emerge -av apache

Once the emerge completes, we'll need to set the ServerName directive before we can start the server successfully.

$ sudo nano -w /etc/apache2/httpd.conf

Make sure the file contains the following directive:

ServerName yourserversname

Let's start apache with the system

$ sudo rc-update add apache2 default

And start the server

$ sudo /etc/init.d/apache2 start

You'll also want to add any user accounts desired to the apache group.

$ sudo usermod -a -G apache username

The default location for your web root is /var/www/localhost/htdocs/. To be sure your apache server is up and running, if you visit http://localhost , you should see a simple page with black text saying, "It works!"

Configuring apache vhosts

An elegant way to manage multiple sites on the same server using the same port is by using vhosts. You'll want to make sure that the vhosts USE flag was enabled when you emerged apache.

The next step is to ensure that virtual host support is enabled in the apache config.

$ sudo nano -w /etc/apache2/httpd.conf

Check for the following setting:

# Virtual-host support
#
# Gentoo has made using virtual-hosts easy. In /etc/apache2/vhosts.d/ we
# include a default vhost (enabled by adding -D DEFAULT_VHOST to
# APACHE2_OPTS in /etc/conf.d/apache2).
Include /etc/apache2/vhosts.d/*.conf

You can now configure your website as a virtual host. First, place the site content in the desired location. In this case, we'll go with the default Gentoo web configuration hierarchy.

$ cd /var/www
$ sudo mkdir wiki.gr0x0rd.com
$ cd wiki.gr0x0rd.com
$ sudo mkdir htdocs

Copy your website content to the htdocs folder. In this example, we'd extract the mediawiki installation archive, then copy the contents:

$ sudo cp /path/to/content /var/www/wiki.gr0x0rd.com/htdocs/

After the copy completes, you'll want to change the file ownership to the apache user.

$ sudo chown -R apache:apache /var/www/wiki.gr0x0rd.com

Now, we'll create a configuration file for the new site. This needs to be placed in /etc/apache2/vhosts.d/.

$ sudo nano -w /etc/apache2/vhosts.d/01_wiki.gr0x0rd.com.conf

Use the following example as a guideline for your new site.

<IfDefine DEFAULT_VHOST>
 <VirtualHost *:80>
      ServerName wiki.gr0x0rd.com
      ServerAlias     wiki.gr0x0rd.ca
      ServerAdmin     admin@yoursite.com
      Include /etc/apache2/vhosts.d/default_vhost.include
      DocumentRoot /var/www/wiki.gr0x0rd.com/htdocs/mediawiki
      <Directory "/var/www/wiki.gr0x0rd.com/htdocs/mediawiki">
              Options Indexes FollowSymLinks
              AllowOverride All
              Order allow,deny
              Allow from all
      </Directory>
      <IfModule mpm_peruser_module>
              ServerEnvironment apache apache
      </IfModule>
  </VirtualHost>
</IfDefine>

You'll need to restart apache for the changes to take effect.

$ sudo /etc/init.d/apache2 restart

You should now be able to view your default apache page via localhost in your browser, and your virtual host via the URL you entered for its ServerName or ServerAlias given you have already configured DNS or made an entry in your /etc/hosts file.

Apache tips

To restart the apache server after a configuration change without interrupting service, enter the command

$ sudo /etc/init.d/apache2 graceful