Lesson 22: Deployment

Deploying a PHP application involves moving it from your local development environment to a live server so users can access it. This lesson covers deploying PHP applications to a live server and using Composer for dependency management.

 


Lesson Outline

  1. Preparing for Deployment
  2. Deploying PHP Applications to a Live Server
  3. Using Composer for Dependency Management
  4. Best Practices for Deployment

22.1 Preparing for Deployment

Checklist for Deployment

  1. Update Configuration:
    • Ensure the .env file contains live database and environment settings.
    • Example:
      env
      APP_ENV=production
      APP_DEBUG=false
      APP_URL=https://yourdomain.com
      DB_CONNECTION=mysql
      DB_HOST=your_live_db_host
      DB_DATABASE=your_live_db_name
      DB_USERNAME=your_live_db_user
      DB_PASSWORD=your_live_db_password
  2. Optimize Code:
    • Remove debug tools, test data, and unnecessary files.
    • Minify assets (CSS, JavaScript).
  3. Backup Data:
    • Backup your database and application files before deployment.
  4. Check PHP Version:
    • Ensure the live server has the required PHP version and extensions.
  5. Use Version Control:
    • Store your project in a version control system like Git for easier deployment and rollback.

22.2 Deploying PHP Applications to a Live Server

1. Deploying to a Shared Hosting Server

Step 1: Upload Files

  1. Use an FTP client like FileZilla to upload your files to the server.
  2. Place your files in the public_html directory (or equivalent).

Step 2: Configure the Environment

  1. Upload the .env file with the live environment variables.
  2. Ensure the public directory is set as the document root in the hosting control panel.

Step 3: Configure File Permissions

  1. Ensure the following directories are writable:
    • storage
    • bootstrap/cache

Step 4: Set Up the Database

  1. Import your local database to the live database using tools like phpMyAdmin.
  2. Update the .env file with the live database credentials.

Step 5: Test the Application

  1. Open your website in a browser to verify functionality.
  2. Check logs for errors in the storage/logs directory.

2. Deploying to a Virtual Private Server (VPS)

Step 1: Set Up the Server

  1. Install required software:
    • Apache or Nginx:
      bash
      sudo apt install apache2
      sudo apt install nginx
    • PHP and Extensions:
      bash
      sudo apt install php php-mysql php-cli php-mbstring php-xml php-curl
    • MySQL:
      bash
      sudo apt install mysql-server
  2. Secure the server:
    • Set up a firewall:
      bash
      sudo ufw allow OpenSSH
      sudo ufw allow 'Apache Full'
      sudo ufw enable

Step 2: Upload Files

  1. Use SCP or rsync to transfer files from your local machine:
    bash
    scp -r /path/to/project user@your-server:/var/www/html
  2. Set file permissions:
    bash
    sudo chown -R www-data:www-data /var/www/html
    sudo chmod -R 755 /var/www/html

Step 3: Configure Virtual Hosts

  1. Create a virtual host file for Apache:
    bash
    sudo nano /etc/apache2/sites-available/yourdomain.conf

    Add the following configuration:

    apache
    <VirtualHost *:80>
    ServerName yourdomain.com
    DocumentRoot /var/www/html/public
    <Directory /var/www/html/public>
    AllowOverride All
    </Directory>

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

  2. Enable the site and restart Apache:
    bash
    sudo a2ensite yourdomain.conf
    sudo systemctl restart apache2

Step 4: Set Up SSL

  1. Install Certbot for free SSL certificates:
    bash
    sudo apt install certbot python3-certbot-apache
  2. Run Certbot to configure SSL:
    bash
    sudo certbot --apache

22.3 Using Composer for Dependency Management

What is Composer?

Composer is a dependency manager for PHP that simplifies the management of libraries and packages.


1. Installing Composer

Linux/Mac

  1. Run the following commands:
    bash
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    php composer-setup.php
    sudo mv composer.phar /usr/local/bin/composer
  2. Verify installation:
    bash
    composer --version

Windows

  1. Download the installer from getcomposer.org.
  2. Follow the installation wizard.

2. Using Composer in Your Project

Install Dependencies

Run the following command in your project directory:

bash
composer install

This installs all the dependencies listed in composer.json.

Update Dependencies

bash
composer update

Autoload Classes

Use Composer’s autoloader for your classes:

php

require 'vendor/autoload.php';

use App\MyClass;

$object = new MyClass();


3. Deploying with Composer

1. Install Dependencies on the Live Server

  1. SSH into the server and navigate to your project directory.
  2. Run:
    bash
    composer install --no-dev --optimize-autoloader
    • --no-dev: Excludes development dependencies.
    • --optimize-autoloader: Optimizes class autoloading.

22.4 Best Practices for Deployment

1. Use a Version Control System

  • Use Git to track changes and deploy code to your live server.
  • Example deployment using Git:
    bash
    git pull origin main

2. Automate Deployments

  • Use CI/CD tools like GitHub Actions, Jenkins, or GitLab CI/CD to automate testing and deployment.

3. Monitor the Application

  • Set up monitoring tools like New Relic or LogRocket to track performance and errors.

4. Use Environment Variables

  • Store sensitive data like API keys in environment variables instead of hardcoding them.

5. Backup Regularly

  • Schedule regular backups of your database and files using tools like cron.

Activities and Exercises

  1. Deploy a Laravel application to a shared hosting server.
  2. Set up a VPS with Apache or Nginx and deploy a PHP application.
  3. Use Composer to add and manage a library (e.g., PHPMailer).

Assignment

  1. Create a PHP project that uses Composer to manage dependencies (e.g., Guzzle for HTTP requests).
  2. Deploy the project to a live server and secure it with HTTPS.

Summary

In this lesson, you learned:

  1. How to deploy PHP applications to shared hosting and VPS.
  2. How to use Composer for dependency management.
  3. Best practices for secure and efficient deployments.

Deploying applications is a critical skill for launching web projects. Let me know if you need help with specific deployment scenarios!