Configuring Apache remotely to host a website

Fabric functions can be run as both regular and super users. If you need to host a website in a remote Apache web server, you need the administrative user privileges to create configuration files and restart the web server. This recipe introduces the Fabric sudo() function that runs commands in the remote machine as a superuser. Here, we would like to configure the Apache virtual host for running a website.

Getting ready

This recipe needs Fabric to be installed first on your local machine. You can install Fabric using the Python packing tools, pip or easy_install, as shown in the following command:

$ pip install fabric

Here, we will connect the remote host using the SSH protocol. So, it's necessary to install and run the SSH server on the remote host. It is also assumed that the Apache web server is installed and running on the remote server. On a Debian/Ubuntu machine, this can be done with the package manager, apt-get, as shown in the following command:

$ sudo apt-get install openssh-server apache2

How to do it...

First, we collect our Apache installation paths and some configuration parameters, such as web server user, group, virtual host configuration path, and initialization scripts. These parameters can be defined as constants.

Then, we set up two functions, remote_server() and setup_vhost(), to execute the Apache configuration task using Fabric.

Listing 7.7 gives the code for configuring Apache remotely to host a website as follows:

#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter - 7
# This program is optimized for Python 2.7.
# It may run on any other version with/without modifications.
 
from fabric.api import env, put, sudo, prompt
from fabric.contrib.files import exists
 
WWW_DOC_ROOT = "/data/apache/test/"
WWW_USER = "www-data"
WWW_GROUP = "www-data"
APACHE_SITES_PATH = "/etc/apache2/sites-enabled/"
APACHE_INIT_SCRIPT = "/etc/init.d/apache2 "
 
def remote_server():
  env.hosts = ['127.0.0.1']
  env.user = prompt('Enter user name: ')
  env.password = getpass('Enter your system password: ')
 
 
def setup_vhost():
  """ Setup a test website """
  print "Preparing the Apache vhost setup..."
  
  print "Setting up the document root..."
  if exists(WWW_DOC_ROOT):
    sudo("rm -rf %s" %WWW_DOC_ROOT)
  sudo("mkdir -p %s" %WWW_DOC_ROOT)
  
  # setup file permissions
  sudo("chown -R %s.%s %s" %(env.user, env.user, WWW_DOC_ROOT))

  # upload a sample index.html file
  put(local_path="index.html", remote_path=WWW_DOC_ROOT)
  sudo("chown -R %s.%s %s" %(WWW_USER, WWW_GROUP, WWW_DOC_ROOT))
  
  print "Setting up the vhost..."
  sudo("chown -R %s.%s %s" %(env.user, env.user, 
APACHE_SITES_PATH))
  
  # upload a pre-configured vhost.conf
  put(local_path="vhost.conf", remote_path=APACHE_SITES_PATH)
  sudo("chown -R %s.%s %s" %('root', 'root', APACHE_SITES_PATH))
  
  # restart Apache to take effect
  sudo("%s restart" %APACHE_INIT_SCRIPT)
  print "Setup complete. Now open the server path 
http://abc.remote-server.org/ in your web browser."

In order to run this script, the following line should be appended on your host file, for example,. /etc/hosts:

127.0.0.1 abc.remote-server.org abc 

You should also create a shortcut, fabfile.py. From the command line, you can do this by typing the following command:

$ ln -sfn 7_7_configure_Apache_for_hosting_website_remotely.py 
fabfile.py

Then, you can call the fab executable in various forms.

First, to log on to a remote server using your script, you can run the following Fabric function. This will result in the following output:

$ fab remote_server setup_vhost
[127.0.0.1] Executing task 'setup_vhost'
Preparing the Apache vhost setup...
Setting up the document root...
[127.0.0.1] sudo: rm -rf /data/apache/test/
[127.0.0.1] sudo: mkdir -p /data/apache/test/
[127.0.0.1] sudo: chown -R faruq.faruq /data/apache/test/
[127.0.0.1] put: index.html -> /data/apache/test/index.html
[127.0.0.1] sudo: chown -R www-data.www-data /data/apache/test/
Setting up the vhost...
[127.0.0.1] sudo: chown -R faruq.faruq /etc/apache2/sites-enabled/
[127.0.0.1] put: vhost.conf -> /etc/apache2/sites-enabled/vhost.conf
[127.0.0.1] sudo: chown -R root.root /etc/apache2/sites-enabled/
[127.0.0.1] sudo: /etc/init.d/apache2 restart
[127.0.0.1] out: Restarting web server: apache2apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[127.0.0.1] out:  ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
[127.0.0.1] out: .
[127.0.0.1] out: 
 
Setup complete. Now open the server path http://abc.remote-server.org/ in your web browser.

Done.
Disconnecting from 127.0.0.1... done.

After you run this recipe, you can open your browser and try to access the path you set up on the host file (for example, /etc/hosts). It should show the following output on your browser:

It works! 
This is the default web page for this server.
The web server software is running but no content has been added, 
yet.

How it works...

This recipe sets up the initial Apache configuration parameters as constants and then defines two functions. In the remote_server() function, the usual Fabric environment parameters, for example, hosts, user, password, and so on, are placed.

The setup_vhost() function executes a series of privileged commands. First, it checks whether the website's document root path is already created using the exists() function. If it exists, it removes that path and creates it in the next step. Using chown, it ensures that the path is owned by the current user.

In the next step, it uploads a bare bone HTML file, index.html, to the document root path. After uploading the file, it reverts the permission of the files to the web server user.

After setting up the document root, the setup_vhost() function uploads the supplied vhost.conf file to the Apache site configuration path. Then, it sets its owner as the root user.

Finally, the script restarts the Apache service so that the configuration is activated. If the configuration is successful, you should see the sample output shown earlier when you open the URL, http://abc.remote-server.org/, in your browser.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.66