การขึ้น Laravel Project บน Server

Laravel Project Deployment on Ubuntu Server

Prerequisites

  • Ubuntu Server (20.04 or 22.04 LTS recommended)
  • SSH access to the server
  • Basic understanding of Linux commands

1. Update Server and Install Dependencies

sudo apt update
sudo apt upgrade -y
sudo apt install -y nginx mysql-server php-fpm php-cli php-mbstring php-xml php-bcmath php-json php-mysql php-zip unzip composer

2. Configure MySQL Database

sudo mysql
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

3. Clone Project and Set Permissions

cd /var/www
sudo git clone https://github.com/yourusername/your-laravel-project.git
cd your-laravel-project

# Set proper permissions
sudo chown -R www-data:www-data /var/www/your-laravel-project
sudo chmod -R 755 /var/www/your-laravel-project
sudo chmod -R 777 /var/www/your-laravel-project/storage
sudo chmod -R 777 /var/www/your-laravel-project/bootstrap/cache

4. Composer Dependencies

cd /var/www/your-laravel-project
composer install --optimize-autoloader --no-dev

5. Environment Configuration

cp .env.example .env
nano .env

# Update database and other configurations
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=your_strong_password

6. Generate Application Key

php artisan key:generate
php artisan config:cache
php artisan route:cache
php artisan view:cache

7. Nginx Configuration

sudo nano /etc/nginx/sites-available/laravel

Paste the following configuration:

server {
    listen 80;
    server_name your-domain.com;
    root /var/www/your-laravel-project/public;

    index index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

8. Enable Nginx Site and Restart Services

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm

9. Optional: SSL with Certbot

sudo snap install --classic certbot
sudo certbot --nginx -d your-domain.com

Additional Deployment Tips

  • Use Git for version control
  • Set up GitHub Actions or GitLab CI for automated deployments
  • Consider using Supervisor for queue workers
  • Implement proper backup strategies

## Troubleshooting
- Check Nginx logs: `sudo tail -f /var/log/nginx/error.log`
- Check PHP-FPM logs: `sudo tail -f /var/log/php8.1-fpm.log`
- Verify permissions and file ownership

Leave a Reply

Your email address will not be published. Required fields are marked *