A simple PHP-based application to create short URLs from long URLs. I used XAMPP for initial setup. This project includes Docker support for easy development and deployment, with an optional Visual Studio Code devcontainer setup.
- PHP
- MySQL/ PostgreSQL
- Docker
- Docker and Docker Compose:
- Install Docker Desktop from Docker official website.
- Ensure WSL 2 is installed and Docker integration is enabled for your distribution.
- Visual Studio Code (Optional):
- Install Visual Studio Code.
- Install the Dev Containers extension for VS Code.
- XAMPP (Optional):
If running locally (not using Docker), install XAMPP.
- Clone The Repository
git clone https://github.com/your-username/url-shortener.git
cd url-shortener
- Run Locally with Docker
This project includes a docker-compose.yml
file to set up the application.
- Start the Docker containers
docker-compose up --build
- Access the app
Visit: http://localhost:8080
in your browser.
- Stop the Docker containers
docker-compose down
- Run Locally with XAMPP
If you prefer using XAMPP:
- Copy the project folder into the htdocs directory (e.g.,
C:\xampp\htdocs\url-shortener
). - Import the
url_shortener.sql
file in thedatabase
folder into your MySQL database. - Configure the
shorten.php
file with your database credentials:
$host = "localhost";
$username = "root";
$password = "";
$database = "url_shortener";
- Start Apache and MySQL services in XAMPP.
- Access the app at
http://localhost/url-shortener
.
- Enter a long URL in the input field and submit the form.
- The app will generate a short URL.
- Use the short URL to redirect to the original URL.
This project includes a .devcontainer/devcontainer.json
file for developing in a consistent, containerized environment in Visual Studio Code.
- Setup the Devcontainer
-
Install the Dev Containers extension in Visual Studio Code.
-
Open the project folder in VS Code
-
When prompted, reopen the projet in the container:
- If not prompted, press Ctrl+Shift+P, type Dev Containers: Reopen in Container, and select it.
- Runs the application in a Dockerized environment.
- Pre-configures MySQL, PHP, and Apache.
- Provides a consistent development environment across all systems.
The
devcontainer.json
file contains the following setup:
{
"name": "URL Shortener Dev Environment",
"dockerComposeFile": ["../docker-compose.yml"],
"service": "php",
"workspaceFolder": "/workspace",
"settings": {
"terminal.integrated.defaultProfile.linux": "bash"
},
"extensions": [
"ms-azuretools.vscode-docker",
"esbenp.prettier-vscode",
"bmewburn.vscode-intelephense-client"
]
}
Inside the devcontainer folder terminal:
composer install
npm install
- Using Docker
The docker-compose.yml file sets up a MySQL container with the following details:
Host: mysql
Port: 3306
Database: url_shortener
Username: root
Password: password
- Manually Import Database
- Import the
url_shortener.sql
file into MySQL. - Ensure the urls table exists with the following schema:
CREATE TABLE urls (
id INT AUTO_INCREMENT PRIMARY KEY,
long_url TEXT NOT NULL,
short_code VARCHAR(6) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
- Shortens long URLs into short, shareable URLs.
- Stores URLs in a MySQL database with timestamps.
- Built-in URL validation.
- Simple, clean PHP and MySQL implementation.
- Dockerized environment for consistent development.
- Install Daytona: Follow the Daytona Installation Guide.
- Create the Workspace:
daytona create https://github.com/daytonaio/sample-php-url-shortener
The easiest way to deploy your PHP app is via Heroku with an inbuilt database tool. Learn more in the deployment documentation.