Files
bikeservice/deploy-apache.md
2025-09-06 14:31:22 +02:00

9.5 KiB

Apache Deployment Guide for Bike Service Application

This guide will help you deploy your Laravel + Vue.js application to Ubuntu 22.04 with Apache and MySQL.

Prerequisites

  • Ubuntu 22.04 server with root/sudo access
  • Domain name (optional but recommended)
  • SSH access to your server

Server Setup

1. Update System

sudo apt update && sudo apt upgrade -y

2. Install Required Software

# Install Apache
sudo apt install apache2 -y

# Install PHP 8.2 and extensions
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install php8.2 libapache2-mod-php8.2 php8.2-mysql php8.2-xml php8.2-mbstring php8.2-curl php8.2-zip php8.2-gd php8.2-cli php8.2-common php8.2-bcmath php8.2-json php8.2-tokenizer php8.2-fileinfo php8.2-opcache -y

# Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

# Install Node.js and npm
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs

# Install Git
sudo apt install git -y

# Install MySQL
sudo apt install mysql-server -y

# Install additional tools
sudo apt install unzip curl wget htop ufw fail2ban -y

3. Configure MySQL

# Secure MySQL installation
sudo mysql_secure_installation

# Create database and user
sudo mysql -u root -p

In MySQL console:

CREATE DATABASE bikeservice_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'bikeservice_user'@'localhost' IDENTIFIED BY 'your_secure_password';
GRANT ALL PRIVILEGES ON bikeservice_db.* TO 'bikeservice_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

4. Configure PHP

# Edit PHP configuration
sudo nano /etc/php/8.2/apache2/php.ini

# Set these values:
# memory_limit = 256M
# upload_max_filesize = 64M
# post_max_size = 64M
# max_execution_time = 300
# opcache.enable = 1
# opcache.memory_consumption = 128
# opcache.max_accelerated_files = 4000

# Restart Apache
sudo systemctl restart apache2
sudo systemctl enable apache2

5. Enable Apache Modules

sudo a2enmod rewrite
sudo a2enmod ssl
sudo a2enmod headers
sudo a2enmod deflate
sudo a2enmod expires
sudo systemctl restart apache2

Application Deployment

1. Create Application Directory

sudo mkdir -p /var/www/bikeservice
sudo chown -R $USER:$USER /var/www/bikeservice
cd /var/www/bikeservice

2. Clone Your Repository

git clone <your-repository-url> .
# Or upload your files via SCP/SFTP

3. Backend Setup (Laravel)

cd backend

# Install dependencies
composer install --no-dev --optimize-autoloader

# Set permissions
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache

# Create .env file
cp .env.example .env

# Generate application key
php artisan key:generate

# Configure database in .env file
# DB_CONNECTION=mysql
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=bikeservice_db
# DB_USERNAME=bikeservice_user
# DB_PASSWORD=your_secure_password

# Run migrations
php artisan migrate --force

# Clear and cache config
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Set production environment
echo "APP_ENV=production" >> .env
echo "APP_DEBUG=false" >> .env

4. Frontend Setup (Vue.js)

cd ../frontend

# Install dependencies
npm install

# Build for production
npm run build

# Copy built files to Laravel public directory
cp -r dist/* ../backend/public/

Apache Configuration

1. Create Apache Virtual Host

sudo nano /etc/apache2/sites-available/bikeservice.conf

Add this configuration:

<VirtualHost *:80>
    ServerName your-domain.com
    ServerAlias www.your-domain.com
    DocumentRoot /var/www/bikeservice/backend/public
    
    <Directory /var/www/bikeservice/backend/public>
        AllowOverride All
        Require all granted
    </Directory>
    
    # Security headers
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set X-XSS-Protection "1; mode=block"
    Header always set X-Content-Type-Options "nosniff"
    Header always set Referrer-Policy "no-referrer-when-downgrade"
    
    # Gzip compression
    LoadModule deflate_module modules/mod_deflate.so
    <Location />
        SetOutputFilter DEFLATE
        SetEnvIfNoCase Request_URI \
            \.(?:gif|jpe?g|png)$ no-gzip dont-vary
        SetEnvIfNoCase Request_URI \
            \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
    </Location>
    
    # Cache static assets
    <LocationMatch "\.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$">
        ExpiresActive On
        ExpiresDefault "access plus 1 year"
        Header append Cache-Control "public, immutable"
    </LocationMatch>
    
    # Logging
    ErrorLog ${APACHE_LOG_DIR}/bikeservice_error.log
    CustomLog ${APACHE_LOG_DIR}/bikeservice_access.log combined
</VirtualHost>

2. Enable Site and Restart Apache

sudo a2ensite bikeservice.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl restart apache2

1. Install Certbot

sudo apt install certbot python3-certbot-apache -y

2. Obtain SSL Certificate

sudo certbot --apache -d your-domain.com -d www.your-domain.com

Final Configuration

1. Set Proper Permissions

sudo chown -R www-data:www-data /var/www/bikeservice
sudo chmod -R 755 /var/www/bikeservice
sudo chmod -R 775 /var/www/bikeservice/backend/storage
sudo chmod -R 775 /var/www/bikeservice/backend/bootstrap/cache

2. Configure Queue Worker (if using queues)

# Create systemd service for queue worker
sudo nano /etc/systemd/system/bikeservice-queue.service

Add this content:

[Unit]
Description=Bike Service Queue Worker
After=network.target

[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/var/www/bikeservice/backend
ExecStart=/usr/bin/php artisan queue:work --sleep=3 --tries=3 --max-time=3600
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl enable bikeservice-queue
sudo systemctl start bikeservice-queue

3. Set up Cron for Laravel Scheduler

crontab -e

Add this line:

* * * * * cd /var/www/bikeservice/backend && php artisan schedule:run >> /dev/null 2>&1

Deployment Script

Create a deployment script for future updates:

#!/bin/bash
# deploy-apache.sh

cd /var/www/bikeservice

# Pull latest changes
git pull origin main

# Backend deployment
cd backend
composer install --no-dev --optimize-autoloader
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Frontend deployment
cd ../frontend
npm install
npm run build
cp -r dist/* ../backend/public/

# Set permissions
sudo chown -R www-data:www-data /var/www/bikeservice
sudo chmod -R 755 /var/www/bikeservice
sudo chmod -R 775 /var/www/bikeservice/backend/storage
sudo chmod -R 775 /var/www/bikeservice/backend/bootstrap/cache

# Restart services
sudo systemctl restart apache2

echo "Deployment completed!"

Make it executable:

chmod +x deploy-apache.sh

Monitoring and Maintenance

1. Check Application Status

# Check Apache status
sudo systemctl status apache2

# Check MySQL status
sudo systemctl status mysql

# Check queue worker status
sudo systemctl status bikeservice-queue

# Check Laravel logs
tail -f /var/www/bikeservice/backend/storage/logs/laravel.log

2. Regular Maintenance

# Update system packages
sudo apt update && sudo apt upgrade -y

# Clear Laravel caches
cd /var/www/bikeservice/backend
php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear

# Rebuild caches
php artisan config:cache
php artisan route:cache
php artisan view:cache

Troubleshooting

Common Issues:

  1. Permission denied errors: Check file ownership and permissions
  2. 500 Internal Server Error: Check Apache error logs and Laravel logs
  3. Database connection issues: Verify MySQL configuration in .env
  4. Static assets not loading: Check Apache configuration and file paths
  5. URL rewriting not working: Ensure mod_rewrite is enabled

Useful Commands:

# Check Apache error logs
sudo tail -f /var/log/apache2/error.log

# Check Apache access logs
sudo tail -f /var/log/apache2/access.log

# Test Apache configuration
sudo apache2ctl configtest

# Restart all services
sudo systemctl restart apache2 mysql bikeservice-queue

# Check MySQL status
sudo mysql -u root -p -e "SHOW DATABASES;"

# Check PHP version and modules
php -v
php -m

Security Considerations

  1. Firewall: Configure UFW firewall
  2. SSH: Use key-based authentication and change default port
  3. Database: Use strong passwords and limit access
  4. Updates: Keep system and packages updated
  5. Backups: Set up regular backups of your application and database
  6. Apache Security: Disable server signature and unnecessary modules

Performance Optimization

  1. OPcache: Enable PHP OPcache for better performance
  2. MySQL: Optimize MySQL configuration
  3. Apache: Enable compression and caching
  4. CDN: Use a CDN for static assets
  5. Monitoring: Set up application monitoring

Apache vs Nginx Differences

  • Configuration: Apache uses .htaccess files and virtual hosts
  • Modules: Apache modules need to be enabled with a2enmod
  • Sites: Apache sites are enabled with a2ensite
  • Logs: Apache logs are in /var/log/apache2/
  • Restart: Use sudo systemctl restart apache2