A simple Blog Content Management System (CMS) built using PHP and MySQL. This application provides a basic web interface for creating, reading, updating, and deleting (CRUD) blog posts. It is designed for learning and development purposes, and can be extended with features like user authentication, comments, and an administrative dashboard.
The PHP Blog CMS is a lightweight content management system that allows users to manage blog posts through a simple web interface. It demonstrates the basics of PHP with SQL persistence using PDO and MySQL. The application covers core CRUD operations and provides a foundation to expand with advanced features like user authentication, search, and SEO-friendly URLs.
Frontend:
HTML, CSS, JavaScript (with Bootstrap) for a responsive and clean user interface.
Backend:
PHP scripts that handle business logic and routing, using PDO for database interactions.
Database Layer:
A MySQL database stores blog posts and (optionally) user and comment data.
User Module:
(Future Enhancement) Manage user authentication and roles.
Blog Module:
Manages blog posts with operations to create, edit, view, and delete content.
Admin Dashboard:
(Optional) Interface for site administrators to manage posts and view statistics.
Clone the Repository
git clone https://github.com/your-username/php-blog-cms.git
cd php-blog-cms
Configure Environment
Create a file named config.php
in the project root with your database and application settings. Example:
<?php
// config.php
define('DB_HOST', 'localhost');
define('DB_NAME', 'blog_cms');
define('DB_USER', 'your_db_username');
define('DB_PASS', 'your_db_password');
define('BASE_URL', 'http://localhost/blog-cms/');
date_default_timezone_set('UTC');
try {
$pdo = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Could not connect to the database " . DB_NAME . ": " . $e->getMessage());
}
?>
Set Up the Database
Create a new database called blog_cms
(or update DB_NAME
accordingly):
CREATE DATABASE blog_cms CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Import the provided schema located in database/schema.sql
:
mysql -u your_db_username -p blog_cms < database/schema.sql
The SQL schema in database/schema.sql
sets up the following tables:
users (optional for future authentication):
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
posts (for blog posts):
CREATE TABLE IF NOT EXISTS posts (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(150) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
comments (optional for future use):
CREATE TABLE IF NOT EXISTS comments (
id INT AUTO_INCREMENT PRIMARY KEY,
post_id INT NOT NULL,
author VARCHAR(100),
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (post_id) REFERENCES posts(id)
);
php-blog-cms/
├── config.php
├── index.php
├── create_post.php
├── view_post.php
├── edit_post.php
├── delete_post.php
├── inc/
│ ├── header.php
│ └── footer.php
└── database/
└── schema.sql
Homepage:
Visit http://localhost/blog-cms/
to see a list of blog posts.
Create Post:
Navigate to http://localhost/blog-cms/create_post.php
to create a new post.
View Post:
Click on “Read More” from any post card on the homepage to view full content.
Edit/Delete Post:
Use the corresponding buttons on each post to edit or delete posts.
From the project directory, run:
php -S localhost:8000
Then, open your browser at http://localhost:8000.
Apache:
Configure a virtual host and ensure mod_rewrite
is enabled for clean URLs.
Nginx:
Set up a server block similar to the following:
server {
listen 80;
server_name your-domain.com;
root /path/to/php-blog-cms;
index index.php index.html;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
A sample Dockerfile
and docker-compose.yml
are provided (see documentation below for details):
Dockerfile:
FROM php:7.4-apache
RUN a2enmod rewrite
COPY . /var/www/html/
RUN docker-php-ext-install pdo pdo_mysql
EXPOSE 80
CMD ["apache2-foreground"]
docker-compose.yml:
version: '3.7'
services:
web:
build: .
ports:
- "80:80"
volumes:
- .:/var/www/html
depends_on:
- db
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: your_root_password
MYSQL_DATABASE: blog_cms
MYSQL_USER: your_db_username
MYSQL_PASSWORD: your_db_password
volumes:
- db_data:/var/lib/mysql
volumes:
db_data:
htmlspecialchars()
and prepared statements via PDO.password_hash()
for storing user passwords securely.config.php
has the correct database credentials and that the MySQL server is running.Contributions are welcome! Follow these steps:
git checkout -b feature/your-feature
.Please adhere to the coding standards and include tests where applicable.
This project is licensed under the MIT License.
The UNC-CH Google Developer Student Club (GDSC) team.