write a bash script named gen_apache_vhost.sh . You’ll have to wait until you fi
ID: 3688102 • Letter: W
Question
write a bash script named gen_apache_vhost.sh. You’ll have to wait until you finish the LAMP lab to start this one. This script will automate the process of generating a new virtual host for your Apache configuration. All of the steps are straight out of the LAMP lab, so refer back to that if you need a reminder of the specifics of each step. As you saw in that lab, creating a new virtual host is not all that difficult, but did involve editing several files to add entries for the non-SSL and SSL-enabled versions of the website. You also have to modify /etc/hosts to have the new virtual host name listed for the IP address of the system. Your script must take a single argument that is the hostname of the new virtual host. It then handles all the other details of adding the new virtual host. Here is the list of major pieces:
Add a VirtualHost entry to the bottom of httpd.conf. Be sure that your script generates a backup copy of the original file in case something gets messed up.
Add a VirtualHost entry to the bottom of ssl.conf. Be sure that your script generates a backup copy of the original file in case something gets messed up.
Create the directory for the new virtual host in /var/www and add a blank index.html file for the virtual host there.
Modify the IP address line in /etc/hosts to add the new virtual hostname to the end of the line. Be sure that your script generates a backup copy of the original file in case something gets messed up.
Restart the httpd.service service.
Clearly you will need to run the script with sudo since it will be making many administrative changes to your system. Step 4 is the only really complicated step because you have to add new information to a line in the middle of the file without destroying the rest of the file. You can do this with a loop that reads every line and prints every line back out, only modifying the output if it is the IP address line in question. Instead of a loop, you can use some sed hackery to do it in one step. You'll have to use back references to make it work which will require a bit of reading on your own to figure out.
The script should ensure that there is only one argument, the new virtual hostname, but otherwise there should be no output if things worked correctly. Here is an example run:
[user@user eclab]$ sudo ./gen_apache_vhost.sh
usage: ./gen_apache_vhost.sh vhost_name
[user@user eclab]$ sudo ./gen_apache_vhost.sh www.test.com
[user@user eclab]$ grep 10.0.2.15 /etc/hosts
10.0.2.15 username.comp315 www.comp315.private www.test.com
[user@user eclab]$
Explanation / Answer
#!/bin/sh
if [ $# -eq 1]
then
cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf_bkp
echo "<VirtualHost *:80> DocumentRoot '/var/www/' </VirtualHost>" >> /etc/httpd/conf/httpd.conf
cp /etc/ssl/ssl.conf /etc/ssl/ssl.conf_bkp
echo "<VirtualHost :443>
ProxyPreserveHost On
ProxyRequests Off
SSLProxyEngine on
SSLEngine on
SSLCertificateFile /etc/ssl/certs/server.crt
SSLCertificateKeyFile /etc/ssl/private/server.key
</VirtualHost>" >> /etc/ssl/ssl.conf
cp /etc/hosts /etc/hosts_bkp
echo "10.0.2.15 username.comp315 www.comp315.private "+$1 >> /etc/hosts
service httpd restart
fi
else
echo "usage: ./gen_apache_vhost.sh vhost_name"
fi
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.