Content Management System Using LAMP Stack ( Linux, Apache, MySQL/MariaDB and PHP)

Tanav Bajaj
5 min readDec 30, 2021

--

My Code GitHub Repository- https://github.com/tanav2202/CMS

I recently finished making a CMS in Php and would like to share my experience. Here I will start by explaining the set-up I used for coding and the basics of SQL that are needed. Then I will be going into detail about the various functions used in the code.

I assume anyone who wishes to make a CMS using PHP already knows the basics of PHP and has made easy projects like Contact form, Login System or Calculator etc and is aware of the syntax.

PHP has thousands of in-built functions. In this article, I will be listing the ones used in the making of my CMS.

I have been using Endeavour OS (fork of Arch) as my main operating system for a while now and built my CMS on Localhost. Here are the steps I used to set up Apache Server and MariaDB on my system.

Note- Most of these commands will work by adding sudo in front because for installation of new packages and editing config files you need to be the root user. So use Sudo whenever prompted.

Apache Installation

  1. Checking if the system is up to date

2. Install Apache using

  • pacman -S apache

3. Edit /etc/httpd/conf/httpd.conf file -

  • I use VSCode to edit all files ( even though Vim/Nvim is recommended for .config files)
  • Search and comment out the following line if it is not already:
  • [...]
    #LoadModule unique_id_module modules/mod_unique_id.so
    [...]
  • save and close the file

4. Enable Apache service to start at boot and restart Apache service using commands:

  • systemctl enable httpd
  • systemctl restart httpd
  • systemctl status httpd

The last command helps check the status if Apache server is running or not.

5. You can test the Apache server by creating an Index.html file in the /srv/httpd directory and then running it in the browser by typing localhost

Now Apache server is ready and good to go.

MariaDB Installation

  1. Install MariaDB using pacman -S mysql
  2. Now initialise MariaDB using mysql_install_db — user=mysql — basedir=/usr — datadir=/var/lib/mysql
  3. Next, use the following commands to start the MariaDB service.
  • systemctl enable mysqld
  • systemctl start mysqld
  • systemctl status mysqld

4. Run the following command to set up MariaDB root user password:

  • mysql_secure_installation

PHP Installation

  1. To install PHP in Arch Linux, run:
  • pacman -S php php-apache

2. edit /etc/httpd/conf/httpd.conf file in your favourite text editor

  • Find the following line and comment it out:
  • […]
    #LoadModule mpm_event_module modules/mod_mpm_event.so
    […]
  • add the following lines at the bottom:
  • […]
    LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
    LoadModule php7_module modules/libphp7.so
    AddHandler php7-script php
    Include conf/extra/php7_module.conf
  • save and close the file

3. Type systemctl restart httpd in the terminal

4. Create a file in the location /srv/http/ with the name test.php

  • Add the following lines to this file
  • <?php
    phpinfo();
    ?>
  • save

5. Go to localhost/test.php on any web browser

Here you should find a page showing the version of Php you are using and details about it. This means PHP has been set up on your system

That’s all about the installations process. Next, I will be discussing the various functions used in the code.

The process of Writing code is explained in https://www.elated.com/cms-in-an-afternoon-php-mysql/

SQL Used

Only a few SQL commands were used for the entire process. These are-

  • DROP TABLE IF EXISTS- The DROP TABLE statement deletes the specified table, and any data associated with it, from the database. The IF EXISTS clause allows the statement to succeed even if the specified tables does not exist.
  • CREATE TABLE- Creates a new table in our database
  • PRIMARY KEY- constraint uniquely identifies each record in a table. This can never be NULL
  • SELECT- statement is used to select data from a database.
  • DELETE- the statement is used to delete existing records in a table.
  • WHERE- clause is used to filter records.

PHP Used

Besides basics also get an idea about PDO. PHP PDO is a database access layer that provides a uniform interface for working with multiple databases. PDO simplifies the common database operations including: Creating database connections. Executing queries using prepared statements. Calling stored procedures.

PHP Functions Used per file

config.php file

  • ini_set() — Helps display error messages in the browser
  • date_default_timezone_set — sets local time on the server as well. I used Asia/Kolkata in my code.
  • getmessage() — returns a description of the error or behaviour that caused the exception to be thrown.
  • set_exception_handler — Handles any exceptions raised when our code runs and also displays the error in the web server’s error log.
  • define() — Creates a constant variable that cannot be changed anywhere in the code. Used in this to set paths of various files, set number of articles on home page , username and password to access admin page

Article.php file

It is a class to handle the articles which includes storing and getting data from the database.

  • isset()- function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
  • preg_replace() — function returns a string or array of strings where all matches of a pattern or list of patterns found in the input are replaced with substrings. The context of using this function here to use Regex to eliminate stuff that is not a part of normal english
  • explode()- function breaks a string into an array.
  • mktime()- returns the Unix timestamp for a date.
  • bindvalue()- A part of PDO used to bind a value to a parameter. This function binds a value to corresponding named or question mark placeholder in the SQL which is used to prepare the statement.
  • prepare()- SQL statement template is created and sent to the database.
  • fetch()- A part of PDO which fetched rows and columns from database
  • query()- Helps connect and then send SQL queries to the database

Index.php

  • list()- used to assign values to a list of variables in one operation.
  • require()[or include]- Include other files at that location. Like creating a seperate file as header/footer that can be used in all other pages.

Admin.php

  • unset()- resets the local variables.
  • $_POST- is a PHP super global variable which is used to collect form data after submitting an HTML form
  • $_GET- is a PHP super global variable which is used to collect form data after submitting also collected data is sent to the URL.
  • $_SESSION- Session variables stores user information to be used across multiple pages (e.g. username, favorite color, etc). By default, session variables last until the user closes the browser.

These are all the pre-defined functions used in CMS that need to be understood before attempting to code it.

--

--

Tanav Bajaj

Caffeine-fueled Prompt Engineer who can say "Hello World!" and train ML models like it's nobody's business!