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 6.7 gives the code for configuring Apache remotely to host a website as follows:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition -- Chapter - 6 
# This program is optimized for Python 2.7.12 and Python 3.5.2. 
# It may run on any other version with/without modifications. 
 
from getpass import getpass 
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) 
    sudo("chown -R %s.%s %s" %(env.user, env.user, WWW_DOC_ROOT)) 
    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)) 
    put(local_path="vhost.conf", remote_path=APACHE_SITES_PATH) 
    sudo("chown -R %s.%s %s" %('root', 'root', APACHE_SITES_PATH)) 
    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 6_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 execute 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:

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

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