Skip to content

akashu96/AzureVM-Apache2-MySQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Azure VM with Apache2 & MySQL - Complete Guide

This project demonstrates how to deploy a web server on Azure Virtual Machine with Apache2 and MySQL, both manually and using Terraform. The setup includes a sample PHP website that connects to MySQL database.

πŸ“‹ Table of Contents

🎯 Overview

This project sets up:

  • Azure Virtual Machine (Ubuntu 22.04 LTS)
  • Apache2 web server
  • MySQL database server
  • PHP with MySQL support
  • Sample website with database connectivity test
  • Complete User Management System with CRUD operations (Create, Read, Update, Delete)

πŸ†• New Features - User Management System

This project now includes a full-featured CRUD application for managing users:

  • βœ… Create: Add new users via web form
  • βœ… Read: View all users in a formatted table
  • βœ… Update: Edit existing user information
  • βœ… Delete: Remove users with confirmation dialog
  • βœ… Modern UI: Responsive design with gradient backgrounds
  • βœ… Statistics Dashboard: Real-time user count and database status

New Pages:

  • add_user.php - Add new users
  • manage_users.php - View and manage all users
  • edit_user.php - Edit existing users
  • delete_user.php - Delete users

πŸ“š Documentation:

πŸ“¦ Prerequisites

For Both Methods:

  • Azure account with an active subscription
  • SSH key pair (will be generated if not exists)

For Terraform:

For Manual Setup:

  • Azure CLI
  • Basic knowledge of Linux commands

πŸ“ Project Structure

AzureVM-Apache2-MYSQL/
β”œβ”€β”€ terraform/
β”‚   β”œβ”€β”€ main.tf                    # Main Terraform configuration
β”‚   β”œβ”€β”€ variables.tf               # Variable definitions
β”‚   β”œβ”€β”€ outputs.tf                 # Output values
β”‚   β”œβ”€β”€ cloud-init.yaml           # Cloud-init configuration
β”‚   └── terraform.tfvars.example  # Example variables file
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ create_azure_vm.sh        # Azure VM creation script (CLI)
β”‚   β”œβ”€β”€ install_apache_mysql.sh   # Apache2 & MySQL installation
β”‚   β”œβ”€β”€ configure_mysql.sh        # MySQL configuration
β”‚   └── deploy_website.sh         # Website deployment
β”œβ”€β”€ website/
β”‚   β”œβ”€β”€ index.php                 # Main page
β”‚   β”œβ”€β”€ db_test.php              # Database test page
β”‚   └── info.php                 # PHP info page
└── README.md                     # This file

πŸš€ Method 1: Terraform Deployment

Step 1: Prepare Terraform Configuration

  1. Navigate to the terraform directory:

    cd terraform
  2. Create terraform.tfvars file:

    cp terraform.tfvars.example terraform.tfvars
  3. Edit terraform.tfvars with your values:

    nano terraform.tfvars

    Example content:

    resource_group_name = "rg-webserver-demo"
    location            = "East US"
    prefix              = "webserver"
    environment         = "Development"
    vm_size             = "Standard_B2s"
    admin_username      = "azureuser"
    ssh_public_key_path = "~/.ssh/id_rsa.pub"
    mysql_root_password = "YourSecurePassword123!"

Step 2: Login to Azure

az login

Step 3: Initialize Terraform

terraform init

Step 4: Review the Deployment Plan

terraform plan

Step 5: Deploy the Infrastructure

terraform apply

Type yes when prompted to confirm.

Step 6: Get Output Information

terraform output

You'll see output like:

public_ip_address = "20.X.X.X"
ssh_command = "ssh azureuser@20.X.X.X"
website_url = "http://20.X.X.X"

Step 7: Wait for Setup to Complete

Cloud-init will automatically install and configure everything. Wait about 5-10 minutes, then check:

# SSH into the VM
ssh azureuser@<PUBLIC_IP>

# Check if services are running
sudo systemctl status apache2
sudo systemctl status mysql

# Exit the VM
exit

Step 8: Access Your Website

Open your browser and navigate to:

  • Main page: http://<PUBLIC_IP>
  • Database test: http://<PUBLIC_IP>/db_test.php
  • PHP info: http://<PUBLIC_IP>/info.php

πŸ› οΈ Method 2: Manual Deployment

Step 1: Create Azure VM Using Azure CLI

  1. Navigate to the scripts directory:

    cd scripts
  2. Make the script executable:

    chmod +x create_azure_vm.sh
  3. Login to Azure:

    az login
  4. Run the VM creation script:

    ./create_azure_vm.sh

    This will:

    • Create a resource group
    • Set up virtual network and subnet
    • Configure network security group (NSG) with rules for SSH, HTTP, HTTPS
    • Create public IP address
    • Create and configure the VM
  5. Note the output: Save the public IP address displayed at the end.

Step 2: Connect to the VM

ssh azureuser@<PUBLIC_IP>

Step 3: Install Apache2 and MySQL

  1. Copy the installation scripts to the VM:

    From your local machine:

    scp install_apache_mysql.sh azureuser@<PUBLIC_IP>:~/
    scp configure_mysql.sh azureuser@<PUBLIC_IP>:~/
    scp deploy_website.sh azureuser@<PUBLIC_IP>:~/
  2. SSH into the VM:

    ssh azureuser@<PUBLIC_IP>
  3. Make scripts executable:

    chmod +x install_apache_mysql.sh configure_mysql.sh deploy_website.sh
  4. Run the installation script:

    sudo ./install_apache_mysql.sh

    This will:

    • Update system packages
    • Install Apache2
    • Install MySQL Server
    • Install PHP and required modules
    • Enable and start services

Step 4: Configure MySQL

sudo ./configure_mysql.sh

When prompted:

  • Enter a secure MySQL root password
  • Confirm the password

This will:

  • Set MySQL root password
  • Create database webdb
  • Create user webuser with password webpass123
  • Create sample users table with test data
  • Secure MySQL installation

Step 5: Deploy the Website

sudo ./deploy_website.sh

This will:

  • Backup existing website files
  • Deploy sample PHP website
  • Set proper permissions
  • Display access URLs

Step 6: Access Your Website

Open your browser and navigate to:

  • Main page: http://<PUBLIC_IP>
  • Database test: http://<PUBLIC_IP>/db_test.php
  • PHP info: http://<PUBLIC_IP>/info.php

πŸ§ͺ Testing the Setup

1. Test Apache2

# Check Apache status
sudo systemctl status apache2

# Test configuration
sudo apache2ctl configtest

# Check if Apache is listening
sudo netstat -tulpn | grep :80

2. Test MySQL

# Login to MySQL
sudo mysql -u root -p

# Show databases
SHOW DATABASES;

# Use webdb
USE webdb;

# Show tables
SHOW TABLES;

# Query users
SELECT * FROM users;

# Exit MySQL
EXIT;

3. Test PHP

# Check PHP version
php -v

# Test PHP modules
php -m | grep mysql

4. Test Website Connectivity

# Test from VM
curl http://localhost

# Test from outside (from your local machine)
curl http://<PUBLIC_IP>

🌐 Accessing the Website

After deployment, you can access:

  1. Homepage - http://<PUBLIC_IP>/

    • Displays server information
    • Shows PHP version
    • Links to test pages
  2. Database Test - http://<PUBLIC_IP>/db_test.php

    • Tests MySQL connectivity
    • Displays users from database
    • Shows connection details
  3. PHP Info - http://<PUBLIC_IP>/info.php

    • Complete PHP configuration
    • Loaded modules
    • System information

πŸ”§ Troubleshooting

Issue: Cannot access website

Check firewall rules:

# On Azure portal, verify NSG rules allow port 80
az network nsg rule list --resource-group rg-webserver-demo --nsg-name webserver-nsg --output table

Check Apache status:

sudo systemctl status apache2
sudo journalctl -u apache2 -n 50

Issue: Database connection fails

Check MySQL status:

sudo systemctl status mysql

Verify database and user:

sudo mysql -u root -p
SHOW DATABASES;
SELECT user, host FROM mysql.user WHERE user='webuser';

Check credentials in db_test.php:

cat /var/www/html/db_test.php | grep -A 5 "servername"

Issue: 502 Bad Gateway or PHP not working

Verify PHP is installed:

php -v

Check Apache PHP module:

apache2ctl -M | grep php

Restart Apache:

sudo systemctl restart apache2

Issue: Permission denied errors

Fix file permissions:

sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html

Issue: SSH connection timeout

Verify NSG rule for SSH:

az network nsg rule show \
  --resource-group rg-webserver-demo \
  --nsg-name webserver-nsg \
  --name Allow-SSH

View Logs

# Apache error logs
sudo tail -f /var/log/apache2/error.log

# Apache access logs
sudo tail -f /var/log/apache2/access.log

# MySQL logs
sudo tail -f /var/log/mysql/error.log

# System logs
sudo journalctl -xe

🧹 Cleanup

For Terraform Deployment:

cd terraform
terraform destroy

Type yes when prompted.

For Manual Deployment:

# Delete the entire resource group
az group delete --name rg-webserver-demo --yes --no-wait

# Or delete individual resources
az vm delete --resource-group rg-webserver-demo --name webserver-vm --yes
az network nic delete --resource-group rg-webserver-demo --name webserver-nic
az network public-ip delete --resource-group rg-webserver-demo --name webserver-publicip
az network nsg delete --resource-group rg-webserver-demo --name webserver-nsg
az network vnet delete --resource-group rg-webserver-demo --name webserver-vnet

πŸ” Security Considerations

⚠️ Important Security Notes:

  1. MySQL Password:

    • Change the default MySQL root password
    • Use strong passwords for database users
    • Never commit passwords to version control
  2. SSH Access:

    • Consider restricting SSH access to your IP only
    • Use SSH keys instead of passwords
    • Disable root login via SSH
  3. Network Security:

    • Restrict MySQL port (3306) access in production
    • Consider using Azure Private Link for database
    • Enable HTTPS (SSL/TLS) for production websites
  4. Regular Updates:

    sudo apt update
    sudo apt upgrade -y
  5. Firewall Configuration:

    # Enable UFW firewall
    sudo ufw allow 22/tcp
    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    sudo ufw enable

Recommended Security Improvements:

  1. Use Azure Key Vault for storing secrets
  2. Enable Azure Disk Encryption for VM disks
  3. Configure Azure Backup for VM
  4. Set up Azure Monitor for logging and alerts
  5. Use Managed Identities for Azure service authentication
  6. Implement SSL/TLS with Let's Encrypt

πŸ“š Additional Resources


πŸ›‘οΈ Default Credentials

MySQL Database:

  • Database: webdb
  • Username: webuser
  • Password: webpass123
  • Root Password: (set during configuration)

⚠️ Change these credentials in production!


πŸ“ Quick Command Reference

Terraform Commands

terraform init          # Initialize Terraform
terraform plan          # Preview changes
terraform apply         # Apply changes
terraform destroy       # Destroy infrastructure
terraform output        # Show outputs
terraform fmt           # Format configuration files
terraform validate      # Validate configuration

Azure CLI Commands

az login                                    # Login to Azure
az account list                            # List subscriptions
az account set --subscription "<NAME>"     # Set subscription
az group list                              # List resource groups
az vm list --output table                  # List VMs
az vm start --resource-group <RG> --name <VM>    # Start VM
az vm stop --resource-group <RG> --name <VM>     # Stop VM
az vm show --resource-group <RG> --name <VM>     # Show VM details

Service Management Commands

sudo systemctl status apache2       # Check Apache status
sudo systemctl restart apache2      # Restart Apache
sudo systemctl status mysql         # Check MySQL status
sudo systemctl restart mysql        # Restart MySQL

πŸŽ“ Learning Outcomes

After completing this project, you will have learned:

  • How to deploy Azure Virtual Machines using Terraform and Azure CLI
  • How to configure network security groups and firewall rules
  • How to install and configure Apache2 web server
  • How to set up MySQL database server
  • How to deploy PHP applications
  • How to use cloud-init for automated configuration
  • Infrastructure as Code (IaC) best practices

🀝 Contributing

Feel free to submit issues, fork the repository, and create pull requests for any improvements.


πŸ“„ License

This project is licensed under the MIT License - feel free to use it for learning and development purposes.


✨ Author

Created as a demonstration project for Azure VM deployment with Apache2 and MySQL.


Happy Deploying! πŸš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published