CHAPTER 18
Beginner
Deploying PHP Applications
Updated: May 14, 2026
20 min read
# CHAPTER 18
Deploying PHP Applications
1. Introduction
Building a web application onhttp://localhost is a great start, but until it is accessible to the public on www.yoursite.com, it is just a local experiment. Deploying a backend application involves renting server space, transferring your files, migrating your database, and configuring environment variables. In this chapter, we will learn how to take your local PHP project and deploy it live to the internet.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the difference between Shared Hosting, VPS, and Cloud servers.
- Use an FTP client (like FileZilla) to transfer files.
- Export a local MySQL database and import it to a live server.
- Configure environment variables to secure database credentials.
3. Beginner-Friendly Explanation
Right now, your laptop is acting as the restaurant (Server). You are cooking meals and eating them in your own kitchen. No one else is allowed inside. To open your restaurant to the public, you need to rent a building on a busy street. This is called Hosting. You must pack up your recipe books (PHP files) and your pantry (MySQL database) into moving boxes, drive them to the new building, unpack them, and hang up a sign with your name on it (Domain Name). Once unpacked, anyone in the world can walk in and place an order.4. Types of PHP Hosting
- Shared Hosting (cPanel): (e.g., Hostinger, GoDaddy, Bluehost). You share a large server with hundreds of other websites. Very cheap, very easy. Best for beginners and small projects.
- VPS (Virtual Private Server): (e.g., DigitalOcean, Linode). You get your own private chunk of a server. You must install Apache and PHP via command line yourself. Best for intermediate developers.
- Cloud/PaaS: (e.g., AWS, Heroku, Laravel Forge). Auto-scaling enterprise infrastructure. Highly complex.
5. Step 1: Exporting the Database
Your live website cannot talk to the MySQL database on your laptop. You must copy it to the live server.-
1.
Open local
http://localhost/phpmyadmin.
-
2.
Click your
taskappdatabase.
- 3. Click the Export tab at the top.
-
4.
Click "Go". This downloads a
.sqlfile containing all your tables and data.
6. Step 2: Setting up the Live Server
-
1.
Purchase a domain (
mycoolapp.com) and Shared Hosting (like Hostinger).
- 2. Log into the hosting dashboard (cPanel or hPanel).
-
3.
Find the MySQL Databases section. Create a new database (e.g.,
user123taskdb), a new username, and a strong password. *Write these down!*
-
4.
Open the live phpMyAdmin in your hosting dashboard. Select your new database, click Import, and upload the
.sqlfile you downloaded earlier.
7. Step 3: Securing Credentials (Environment Variables)
Before uploading your code, you must updateconfig.php with the live database credentials. However, hardcoding them is bad practice. We use a separate file to hold secrets.
Create a file named db_config.php:
php
Update your main config.php:
php
8. Step 4: Transferring Files (FTP)
To move your PHP files from your laptop to the server, use FTP (File Transfer Protocol).- 1. Download a free FTP client like FileZilla.
- 2. Enter your hosting FTP IP address, username, and password (found in your hosting dashboard).
-
3.
Connect to the server. You will see a folder called
publichtml(orhtdocs). This is the live version of your localhtdocsfolder.
-
4.
Drag and drop your project files (except local test files) into
publichtml.
9. Step 5: Final Configuration
-
Permissions: If your app allows users to upload files (like profile pictures), ensure the
uploads/directory on the live server has write permissions (chmod 755).
-
Error Reporting: Double-check that
displayerrorsis turned OFF in your live code to prevent hackers from seeing debug traces.
10. Best Practices
- Version Control (Git): While FTP is great for beginners, professional developers deploy using Git. They push their code to GitHub, and a deployment script automatically pulls the new code to the live server. This is faster and prevents accidental file deletion.
11. Common Mistakes
-
Uploading the
.envor Config File to Public Repos: If you push yourdbconfig.phpfile to a public GitHub repository, hackers run automated bots that scrape GitHub for passwords. They will steal your database in 5 minutes. Always add configuration files to your.gitignorefile.
12. Exercises
- 1. Explain the multi-step process of migrating a local MySQL database to a live hosting environment.
13. MCQs with Answers
Question 1
When migrating a PHP application from a local computer to a live shared hosting environment, what tool is primarily used to manage and import the .sql database backup?
Question 2
What is the standard protocol (and software like FileZilla) used to securely drag-and-drop PHP files from a local computer to a live web server?
14. Interview Questions
- Q: Describe the deployment pipeline from a local XAMPP environment to a live Shared Hosting server. How do database credentials change?
-
Q: Why is it considered a critical security vulnerability to push hard-coded database passwords in a
config.phpfile to a version control system like GitHub?
15. FAQs
Q: My website works locally, but on the live server I get a "Database Connection Failed" message. Why? A: This happens 99% of the time because you forgot to update yourconfig.php file with the new database username and password provided by your web host.