Automated Website Deployment with Apache and GitHub Source Code Using Ansible
Here’s a project to automate the process of launching a website on a configured OS using Apache webserver, pulling the source code from GitHub. The entire process will be handled by a combination of bash scripting and Ansible to ensure automation and repeatability.
Steps:
Install and configure the required software (OS, Apache webserver).
Automatically clone the source code from a GitHub repository.
Set up the Apache virtual host to serve the website.
Run everything through Ansible for automation.
Project Structure:
Provisioning Server: We will use Ansible to set up a server with Apache, and configure it to serve a website.
Source Code Deployment: The website source code will be automatically pulled from a GitHub repository.
1. Prerequisites
Make sure the following are available:
A Linux server (e.g., Ubuntu, CentOS, etc.).
SSH access to the server.
Ansible installed on the local machine that will run the automation.
GitHub repository URL containing the website's source code.
2. Ansible Playbook
i. Inventory File (hosts
)
The inventory file lists the servers (hosts) on which the playbook will run.
Create a file called hosts
:
[webserver]
your-server-ip ansible_user=your-username ansible_ssh_private_key_file=~/.ssh/id_rsa
Replace your-server-ip
and your-username
with your server’s IP and username, respectively.
ii. Playbook (setup_webserver.yml
)
The Ansible playbook installs Apache, clones the GitHub repo, and configures the webserver.
---
- name: Configure Web Server and Deploy Website
hosts: webserver
become: yes
vars:
repo_url: "https://github.com/your-username/your-repo.git" # Replace with your GitHub repo URL
web_root: "/var/www/html"
tasks:
- name: Update APT packages
apt:
update_cache: yes
- name: Install Apache
apt:
name: apache2
state: present
- name: Install Git
apt:
name: git
state: present
- name: Clone the website from GitHub
git:
repo: "{{ repo_url }}"
dest: "{{ web_root }}"
force: yes
- name: Ensure Apache is running
service:
name: apache2
state: started
enabled: yes
- name: Set permissions for the web root
file:
path: "{{ web_root }}"
owner: www-data
group: www-data
mode: '0755'
recurse: yes
- name: Create a VirtualHost configuration
copy:
dest: /etc/apache2/sites-available/000-default.conf
content: |
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot {{ web_root }}
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- name: Enable the Apache configuration
shell: |
a2ensite 000-default.conf
systemctl reload apache2
Explanation:
Update APT Packages: The playbook updates the package list to ensure the latest versions are installed.
Install Apache and Git: It installs the Apache webserver and Git, which are needed to host the website and clone the source code from GitHub.
Clone the GitHub Repo: The source code is cloned into the Apache web root (
/var/www/html
).Ensure Apache is Running: The webserver will be started and enabled to run on boot.
Set Permissions: File permissions are adjusted for Apache to serve the files correctly.
Configure Apache: A basic virtual host configuration for Apache is created and applied.
3. Run the Playbook
Once you have set up the playbook and inventory, run the playbook using the following command:
ansible-playbook -i hosts setup_webserver.yml
This will:
SSH into the server.
Install Apache and Git.
Clone your GitHub repository into the web server’s root directory.
Configure Apache to serve the site.
Start Apache and ensure the website is live.
4. Optional: Customizing the Apache Configuration
You can modify the virtual host configuration to match your project’s needs, such as adding a domain name, SSL certificates, or modifying the directory structure.
For example, to add a custom domain:
- name: Create a VirtualHost configuration
copy:
dest: /etc/apache2/sites-available/my-website.conf
content: |
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot {{ web_root }}
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
- name: Enable the Apache configuration
shell: |
a2ensite my-website.conf
systemctl reload apache2
Ensure to update the hosts
file in DNS to point to your server.
5. Conclusion
This project automates the entire process of launching a website with an Apache web server, pulling source code from GitHub. The use of Ansible makes it easy to configure the system with minimal manual intervention and allows you to repeat the process across multiple servers if needed.
The setup can be extended to include features like SSL certificates, additional security configurations, or multi-environment deployments (e.g., staging, production).