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:
- Test the application without being connected to the internet;
- Make changes quickly without affecting users;
- Experiment with databases, sessions, and PHP configurations;
- Debug code safely.
On the other hand, a live server (hosting) is a publicly accessible environment where the application is available to real users. Key characteristics:
- The project must be secure and optimized;
- Databases and files must be imported correctly;
- PHP, Apache/Nginx, and domain configuration must be correct;
- Regular backups and monitoring are required.
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:
- FTP/SFTP: Use an FTP client (FileZilla, WinSCP) to transfer the project files
to the server's public directory (usually
public_htmlorwww). - cPanel File Manager: Most hosts have an integrated File Manager where you can upload and extract zip archives of the project.
- Git Deployment: Some hosting providers allow deployment directly from a Git repository.
2. Export Local Database
On the local server (XAMPP/MAMP/Laragon), open phpMyAdmin, select the database, and export it:
- Click on the database → Export
- Choose the Quick method and SQL format
- Download the resulting SQL file
3. Import Database on Hosting
On the live server, open phpMyAdmin or another admin panel, create a database, and then import the SQL file:
- Create the database, user, and associated password;
- Select the database and go to Import → choose the exported SQL file;
- Run the import and verify that all tables and data have been added correctly.
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
- Separates sensitive data from the source code.
- Allows changing configuration without modifying code.
- Facilitates deployment on different servers (local, staging, live).
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:
- Domain:
example.com - Server IP:
123.45.67.89 - Record type: A record
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:
- Enable site:
sudo a2ensite example.com.conf - Reload Apache:
sudo systemctl reload apache2
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:
- Create a symbolic link:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ - Check the configuration:
sudo nginx -t - Restart Nginx:
sudo systemctl reload nginx
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:
- MySQL Databases:
mysqldump -u root -p traffice-test > test_db_backup.sql - Website files: copy the project folder to another server or cloud storage (e.g., Google Drive, Dropbox, AWS S3)
2. PHP Site Optimization
Some best practices for performance:
- Enable PHP caching:
opcache.enable=1 - Minimize CSS and JS files
- Proper indexing of MySQL tables for faster queries
- Limit complex queries and use prepared statements
Hosting Examples
1. Shared Hosting
- Cheapest and simplest type of hosting.
- Server resources are shared among multiple clients.
- Ideal for small websites or personal blogs.
2. VPS (Virtual Private Server)
- Dedicated resources in a virtualized environment.
- You can configure Apache/Nginx, PHP, and databases as needed.
- Ideal for medium-sized or commercial applications.
3. Cloud Hosting
- Scalable and reliable; resources increase automatically based on traffic.
- Examples: AWS, Google Cloud, DigitalOcean.
- Ideal for large applications, e-commerce, and projects with variable traffic.
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.
