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
- Preparing for Deployment
- Deploying PHP Applications to a Live Server
- Using Composer for Dependency Management
- Best Practices for Deployment
22.1 Preparing for Deployment
Checklist for Deployment
- 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
- Ensure the
- Optimize Code:
- Remove debug tools, test data, and unnecessary files.
- Minify assets (CSS, JavaScript).
- Backup Data:
- Backup your database and application files before deployment.
- Check PHP Version:
- Ensure the live server has the required PHP version and extensions.
- 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
- Use an FTP client like FileZilla to upload your files to the server.
- Place your files in the
public_html
directory (or equivalent).
Step 2: Configure the Environment
- Upload the
.env
file with the live environment variables. - Ensure the
public
directory is set as the document root in the hosting control panel.
Step 3: Configure File Permissions
- Ensure the following directories are writable:
storage
bootstrap/cache
Step 4: Set Up the Database
- Import your local database to the live database using tools like phpMyAdmin.
- Update the
.env
file with the live database credentials.
Step 5: Test the Application
- Open your website in a browser to verify functionality.
- Check logs for errors in the
storage/logs
directory.
2. Deploying to a Virtual Private Server (VPS)
Step 1: Set Up the Server
- 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
- Apache or Nginx:
- Secure the server:
- Set up a firewall:
bash
sudo ufw allow OpenSSH
sudo ufw allow 'Apache Full'
sudo ufw enable
- Set up a firewall:
Step 2: Upload Files
- Use SCP or rsync to transfer files from your local machine:
bash
scp -r /path/to/project user@your-server:/var/www/html
- 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
- Create a virtual host file for Apache:
bash
sudo nano /etc/apache2/sites-available/yourdomain.conf
Add the following configuration:
apache
<Directory /var/www/html/public><VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/html/public
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost> - Enable the site and restart Apache:
bash
sudo a2ensite yourdomain.conf
sudo systemctl restart apache2
Step 4: Set Up SSL
- Install Certbot for free SSL certificates:
bash
sudo apt install certbot python3-certbot-apache
- 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
- 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
- Verify installation:
bash
composer --version
Windows
- Download the installer from getcomposer.org.
- Follow the installation wizard.
2. Using Composer in Your Project
Install Dependencies
Run the following command in your project directory:
composer install
This installs all the dependencies listed in composer.json
.
Update Dependencies
composer update
Autoload Classes
Use Composer’s autoloader for your classes:
require 'vendor/autoload.php';
use App\MyClass;
$object = new MyClass();
3. Deploying with Composer
1. Install Dependencies on the Live Server
- SSH into the server and navigate to your project directory.
- 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
- Deploy a Laravel application to a shared hosting server.
- Set up a VPS with Apache or Nginx and deploy a PHP application.
- Use Composer to add and manage a library (e.g., PHPMailer).
Assignment
- Create a PHP project that uses Composer to manage dependencies (e.g., Guzzle for HTTP requests).
- Deploy the project to a live server and secure it with HTTPS.
Summary
In this lesson, you learned:
- How to deploy PHP applications to shared hosting and VPS.
- How to use Composer for dependency management.
- 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!