To deploy your Laravel project on an Apache or Nginx server, follow these steps. We’ll use PuTTY for server connection and Git to pull the project from a remote repository. We’ll also cover database setup and file permissions configuration.
Step 1: Connect to the Server via SSH
First, use PuTTY or any other SSH client to connect to your server.
Host: Your server’s IP address or domain.
User: root or another privileged user.
Once connected, navigate to your project directory:
cd /var/www/html/your-domain
or
cd /var/www/html/your-domain/your-sub-domain
Step 2: Initialize Git and Pull Your Project
Set up Git in the project directory and pull the latest code from the remote repository:
git init
git remote add origin https://github.com/gsoroar/test.git
git pull origin main
Step 3: Install Dependencies
If you haven’t installed Composer, you can do so with the following command:
apt install composer
Next, install project dependencies:
composer install # Use ‘composer update’ if updating existing dependencies
Step 4: Environment Setup
Copy the example environment file and generate a new application key:
cp .env.example .env
php artisan key:generate
Step 5: Database Setup
Create a new database on your server using your preferred database management tool (e.g., MySQL or phpMyAdmin).
Update the .env file with your database credentials:
DB_DATABASE=your_database_name
DB_USERNAME=your_db_username
DB_PASSWORD=your_db_password
Run database migrations:
php artisan migrate
Step 6: Handle File and Directory Permissions
Laravel requires specific permissions on certain directories to allow writing. If you’re encountering permission issues, adjust permissions as follows (especially for Apache servers):
sudo chown -R www-data:www-data /var/www/html/your-domain/storage
sudo chown -R www-data:www-data /var/www/html/your-domain/bootstrap/cache
sudo chmod -R 775 /var/www/html/your-domain/storage
sudo chmod -R 775 /var/www/html/your-domain/bootstrap/cache
or
sudo chown -R www-data:www-data /var/www/html/your-domain/your-sub-domain/storage
sudo chown -R www-data:www-data /var/www/html/your-domain/your-sub-domain/bootstrap/cache
sudo chmod -R 775 /var/www/html/your-domain/your-sub-domain/storage
sudo chmod -R 775 /var/www/html/your-domain/your-sub-domain/bootstrap/cache