Here’s a basic guide to deploy a simple web application with a MySQL database on an Azure Virtual Machine (VM):
- Create Azure VM
- Install Web Server (Apache or Nginx)
- Install MySQL Server
- Deploy the Web Application
- Test Application
- Log in to the Azure Portal.
- Navigate to Virtual Machines > Create.
- Configure the VM:
- OS: Ubuntu Server 20.04 (or another OS of your choice).
- Size: Select a size suitable for a web app, e.g.,
B1ms
. - Authentication: Use SSH or password for login.
- Open the necessary ports:
- HTTP: 80
- MySQL: 3306 (if needed externally)
- Create and start the VM.
- SSH into the VM:
ssh <username>@<VM_Public_IP>
- Update the package list and install Apache (or Nginx):
sudo apt update sudo apt install apache2 -y # For Apache sudo apt install nginx -y # For Nginx
- Verify the server:
- Open the public IP in a browser. You should see the default web page.
- Install MySQL Server:
sudo apt install mysql-server -y
- Secure the installation:
sudo mysql_secure_installation
- Log in to MySQL and create a database:
sudo mysql -u root -p CREATE DATABASE appdb; CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON appdb.* TO 'appuser'@'localhost'; FLUSH PRIVILEGES; EXIT;
- Clone or upload your app:
- Use
scp
or tools like Filezilla.
scp -r /path/to/app <username>@<VM_Public_IP>:/var/www/html/
- Use
- Configure the application:
- Update database connection settings in your app’s config file to use the MySQL database:
$servername = "localhost"; $username = "appuser"; $password = "password"; $dbname = "appdb";
- Update database connection settings in your app’s config file to use the MySQL database:
- Set proper permissions for the web folder:
sudo chown -R www-data:www-data /var/www/html/ sudo chmod -R 755 /var/www/html/
- Restart the web server:
sudo systemctl restart apache2 # For Apache sudo systemctl restart nginx # For Nginx
- Access the application in your browser:
http://<VM_Public_IP>
- SSL: Secure your app with Let's Encrypt.
sudo apt install certbot python3-certbot-apache sudo certbot --apache
- Firewall Configuration:
Ensure your VM firewall allows traffic to ports
80
and3306
.
This setup creates a basic environment. For scalability, you can later explore Azure App Services, Managed MySQL, or Containerized Deployment using Docker.
Here’s a basic example of a PHP web application that connects to a MySQL database, displays data from a table, and allows the insertion of new records.
First, create a table in the MySQL database:
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Save the following files in your web server directory (e.g., /var/www/html/
).
Defines the database connection.
<?php
// Database configuration
$servername = "localhost";
$username = "appuser";
$password = "password";
$dbname = "appdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Displays data from the database.
<?php
include 'config.php';
// Fetch users from the database
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html>
<head>
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Created At</th>
</tr>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['email']}</td>
<td>{$row['created_at']}</td>
</tr>";
}
} else {
echo "<tr><td colspan='4'>No users found</td></tr>";
}
?>
</table>
<br>
<a href="add_user.php">Add New User</a>
</body>
</html>
Form to add a new user to the database.
<!DOCTYPE html>
<html>
<head>
<title>Add User</title>
</head>
<body>
<h1>Add New User</h1>
<form method="POST" action="save_user.php">
<label>Name:</label>
<input type="text" name="name" required><br><br>
<label>Email:</label>
<input type="email" name="email" required><br><br>
<button type="submit">Add User</button>
</form>
<br>
<a href="index.php">Back to User List</a>
</body>
</html>
Handles user form submission and inserts data into the database.
<?php
include 'config.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
// Insert user into the database
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
if ($conn->query($sql) === TRUE) {
echo "New user added successfully.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
echo '<br><a href="index.php">Back to User List</a>';
}
$conn->close();
?>
- Navigate to
http://<VM_Public_IP>/index.php
to see the user list. - Click Add New User to add a user and verify it appears in the list.
- Use prepared statements to prevent SQL injection.
- Use environment variables for storing sensitive data like database credentials.
- Configure proper file and folder permissions.