The apache-site-up.py
script is designed to manage Apache virtual hosts on a development system. It provides commands for creating, enabling, disabling, deleting sites, and updating the /etc/hosts
file. This script is useful for automating the setup and teardown of Apache virtual hosts during development.
- Create a Site: Initializes site folder and files.
- Enable a Site: Sets up Apache configurations and updates the
/etc/hosts
file. - Disable a Site: Disables the site in Apache and optionally updates the
/etc/hosts
file. - Delete a Site: Removes the site folder, configuration, and updates
/etc/hosts
. - Remove Host Entry: Removes the site entry from
/etc/hosts
without affecting Apache configurations or site files.
- Python 3.x installed on your system.
- Apache web server installed and configured.
- Sufficient permissions to modify Apache configurations and
/etc/hosts
(usesudo
as needed).
The script contains several constants that may need to be configured based on your system setup. These constants are defined at the top of the script:
-
PROJECTS_DIR
: Directory where site folders are created.- Default:
os.path.expanduser('~/projects/web')
- Modification: Set this to the directory where you want to manage your site folders.
- Default:
-
VARWWW_DIR
: Directory where symbolic links to site folders are created.- Default:
/var/www
- Modification: Change this if your symbolic links should be placed in a different directory.
- Default:
-
HOSTS_FILE
: Path to the/etc/hosts
file.- Default:
/etc/hosts
- Modification: Change this if your hosts file is located elsewhere.
- Default:
-
APACHE_SITES_AVAILABLE
: Directory for Apache site configurations.- Default:
/etc/apache2/sites-available
- Modification: Update this if your Apache configuration files are stored in a different location.
- Default:
-
APACHE_SITES_ENABLED
: Directory for enabled Apache sites.- Default:
/etc/apache2/sites-enabled
- Modification: Change this if your enabled sites directory is different.
- Default:
-
Download the Script
Save the
apache-site-up.py
script to your local machine. -
Configure the Script
Open
apache-site-up.py
in a text editor and update the constants as needed to match your system configuration. -
Make the Script Executable
chmod +x apache-site-up.py
-
Run the Script
Use the script with the appropriate flags to manage your Apache virtual hosts. For example:
-
Create a Site:
./apache-site-up.py <site_name> --create
-
Enable a Site:
sudo ./apache-site-up.py <site_name> --hosts
-
Disable a Site:
sudo ./apache-site-up.py <site_name> --disable --hosts
-
Delete a Site:
sudo ./apache-site-up.py <site_name> --remove
-
Remove Host Entry:
sudo ./apache-site-up.py <site_name> --rm-host
-
Command:
./apache-site-up.py <site_name> --create
Creates the site folder and necessary files (index.html
and info.php
), but does not configure Apache or update /etc/hosts
.
Command:
sudo ./apache-site-up.py <site_name> --hosts
Creates a symbolic link for the site in /var/www
, sets up Apache configurations, and updates /etc/hosts
.
Command:
sudo ./apache-site-up.py <site_name> --disable --hosts
Disables the site in Apache, removes the symbolic link, and optionally updates /etc/hosts
.
Command:
sudo ./apache-site-up.py <site_name> --remove
Disables the site if enabled, removes the site folder and its contents, and updates /etc/hosts
.
Command:
sudo ./apache-site-up.py <site_name> --rm-host
Removes the site entry from /etc/hosts
without affecting Apache configurations or site files.
- Create Symbolic Link:
sudo
is required to create symbolic links in/var/www
. - Update Apache Configurations:
sudo
is required to write to/etc/apache2/sites-available
and enable sites witha2ensite
. - Modify
/etc/hosts
:sudo
is required to append or remove entries from/etc/hosts
.
Why Use sudo
:
- Access Control: Ensures that only users with administrative rights can make system-wide changes.
- System Integrity: Prevents unauthorized changes that could affect system stability or security.
Avoid Using sudo
When:
- Creating Site Folders and Files: No need for
sudo
if working in a user directory. - Testing Scripts: Avoid
sudo
to prevent accidental system changes.
- Permission Issues: Ensure you have appropriate permissions. Use
sudo
where necessary. - File Exists Errors: Check for existing files or symbolic links before creating or modifying them.
This script is licensed under the MIT License. For more details, refer to the LICENSE.md file.