Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

Deployment & Server Launch

Local Server vs Live Server

In PHP development, we usually start on a local server, such as XAMPP, MAMP, or Laragon. This environment allows us to:

On the other hand, a live server (hosting) is a publicly accessible environment where the application is available to real users. Key characteristics:

Concrete Example

On the local server (XAMPP), we access the application via the URL http://localhost/project-name and the database is managed with local phpMyAdmin. On the live server, access is through your domain, e.g., https://www.example.com, and the database is imported via cPanel or another admin panel.

Thus, the main difference is that the local server is a safe and fast development environment, while the live server is the production environment, accessible to end users, where the code must be robust.

Uploading Project & Importing DB on Hosting

Once the PHP project is ready on the local server, the next step is to upload it to the live server and import the database.

1. Upload Files

There are several methods to upload files to the live server:

2. Export Local Database

On the local server (XAMPP/MAMP/Laragon), open phpMyAdmin, select the database, and export it:

3. Import Database on Hosting

On the live server, open phpMyAdmin or another admin panel, create a database, and then import the SQL file:

4. Configure Connection File

Update the PHP DB connection file with the live server credentials (host, user, password, DB name):

<?php
$host = 'localhost'; // or the DB address provided by hosting
$db   = 'database_name';
$user = 'username';
$pass = 'password';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
    $pdo = new PDO($dsn, $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
    echo "✅ Successfully connected to the database!";
} catch (PDOException $e) {
    echo "❌ Connection error: " . $e->getMessage();
}
?>

After these steps, the project is fully functional on the live server.

Environment Variables & .env Files

For security and flexibility, sensitive data such as database credentials, API keys, or production settings should not be hardcoded in the source code. In PHP, we use environment variables and .env files.

1. What are environment variables?

Environment variables are values set in the server environment that can be accessed by the PHP application using getenv() or special libraries. They allow separating configurations between local and live environments.

2. The .env file

A .env file contains key=value pairs and should not be uploaded to a public repository (it should be added to .gitignore).

# .env
DB_HOST=localhost
DB_NAME=traffice-test
DB_USER=root
DB_PASS=password123
APP_ENV=production
APP_DEBUG=false

3. Accessing variables in PHP

We can use libraries like vlucas/phpdotenv or the native getenv() function to load these values.

<?php
require 'vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$host = getenv('DB_HOST');
$db   = getenv('DB_NAME');
$user = getenv('DB_USER');
$pass = getenv('DB_PASS');

$dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
try {
    $pdo = new PDO($dsn, $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
    echo "✅ Successfully connected using environment variables!";
} catch (PDOException $e) {
    echo "❌ Connection error: " . $e->getMessage();
}
?>

4. Advantages

Domain Setup + Apache / Nginx

Once the PHP project is ready, to make it accessible online, we need to configure the web server and domain. The most used servers are Apache and Nginx.

1. Setting up the domain

If you have your own domain, you need to point it to your server's IP via a DNS A record. Examples:

After DNS propagation (which can take a few hours), the domain will be ready to serve the PHP files from the server.

2. Apache configuration

Example of an virtual host file for Apache (usually in /etc/apache2/sites-available/example.com.conf):

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com/public

    <Directory /var/www/example.com/public>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

Useful commands:

3. Nginx Configuration

Example of an Nginx server block (usually in /etc/nginx/sites-available/example.com):

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/example.com/public;
    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/php8.1-fpm.sock;
    }

    error_log /var/log/nginx/example.com-error.log;
    access_log /var/log/nginx/example.com-access.log;
}

After configuring the file:

4. Testing

Open your browser and go to http://example.com. If everything is configured correctly, you will see the PHP page served by the live server.

Backup & Optimization

1. Backup Databases and Files

To prevent data loss, it's recommended to perform regular backups. Examples:

2. PHP Site Optimization

Some best practices for performance:

Hosting Examples

1. Shared Hosting

2. VPS (Virtual Private Server)

3. Cloud Hosting


Congratulations on completing the PHP and MySQL course! You've built a solid foundation in modern PHP development, explored essential tools that turn code into a real, scalable, and deployable application.

But the real journey is just beginning. As you deepen your understanding and expand your skills, you'll encounter new challenges, technologies, and opportunities. Our platform continues to offer valuable resources to help you progress.

Keep learning, experimenting, and creating. Your future as a developer is built one line of code at a time.

Top