Skip to main content
PHP Backend Development Tutorial
CHAPTER 18 Beginner

Deploying PHP Applications

Updated: May 14, 2026
20 min read

# CHAPTER 18

Deploying PHP Applications

1. Introduction

Building a web application on http://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. 1. Open local http://localhost/phpmyadmin.
  1. 2. Click your taskapp database.
  1. 3. Click the Export tab at the top.
  1. 4. Click "Go". This downloads a .sql file containing all your tables and data.

6. Step 2: Setting up the Live Server

  1. 1. Purchase a domain (mycoolapp.com) and Shared Hosting (like Hostinger).
  1. 2. Log into the hosting dashboard (cPanel or hPanel).
  1. 3. Find the MySQL Databases section. Create a new database (e.g., user123taskdb), a new username, and a strong password. *Write these down!*
  1. 4. Open the live phpMyAdmin in your hosting dashboard. Select your new database, click Import, and upload the .sql file you downloaded earlier.

7. Step 3: Securing Credentials (Environment Variables)

Before uploading your code, you must update config.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
1234567
<?php
// This file holds the LIVE credentials
define(&#039;DB_HOST', 'localhost'); // Often remains localhost on shared servers
define(&#039;DB_NAME', 'user123_taskdb');
define(&#039;DB_USER', 'user123_admin');
define(&#039;DB_PASS', 'SuperSecretPassword!');
?>

Update your main config.php:

php
123456789
<?php
require_once &#039;db_config.php';

try {
    $pdo = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS);
} catch (PDOException $e) {
    die("Database connection failed."); // Do not print the error on live!
}
?>

8. Step 4: Transferring Files (FTP)

To move your PHP files from your laptop to the server, use FTP (File Transfer Protocol).
  1. 1. Download a free FTP client like FileZilla.
  1. 2. Enter your hosting FTP IP address, username, and password (found in your hosting dashboard).
  1. 3. Connect to the server. You will see a folder called publichtml (or htdocs). This is the live version of your local htdocs folder.
  1. 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 displayerrors is 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 .env or Config File to Public Repos: If you push your dbconfig.php file 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 .gitignore file.

12. Exercises

  1. 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.php file 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 your config.php file with the new database username and password provided by your web host.

16. Summary

In Chapter 18, we went live. Deployment is the bridge between development and production. By utilizing phpMyAdmin export/import tools, we successfully migrated our relational database to a live server. By using FTP, we transferred our PHP logic. By updating our configuration files with secure environment variables and turning off error displays, we ensured our application is public, functional, and secure.

17. Next Chapter Recommendation

Your app is live, and suddenly 10,000 people visit it at once. It crashes. Proceed to Chapter 19: Scaling and Optimizing PHP Applications to learn how to handle massive traffic.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·