laravel-code-base-on-an-ubuntu-server-2100-x-700

How to Set Up a Laravel Code Base on an Ubuntu Server

Setting up a Laravel code base on an Ubuntu server can be straightforward if you follow the steps carefully. This guide will walk you through the entire process, from cloning your repository to securing your site with SSL. Let’s get started!

Prerequisites

Before you begin, ensure you have the following:

  • An Ubuntu server
  • SSH access to your server
  • A registered domain name pointed to your server

Step-by-Step Guide

1. Clone Your Repository

First, SSH into your server and navigate to your web root directory.

ssh your_username@your_server_ip
cd /var/www/html/

Clone your Laravel project from your Git repository.

git clone YOUR_GIT_REPO

Navigate to your project directory.

cd /var/www/html/ai-arena

2. Set Up Environment Variables

Copy your .env file to the root of your project.

cp /path/to/your/.env /var/www/html/ai-arena/.env

3. Install Composer

Install Composer, a dependency manager for PHP. Follow the instructions at Composer’s official website.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'e4d9b25c6359b25925fa897ad96fa212f63b4e1a4e44b0a1b7d735eb4f6a2e4b') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Move Composer to a globally accessible location.

sudo mv composer.phar /usr/local/bin/composer

4. Install Laravel Dependencies

Navigate to your project directory and install the necessary dependencies.

cd /var/www/html/ai-arena
composer install

5. Set Up Laravel

Generate the application key.

php artisan key:generate

Run the migrations to set up your database.

php artisan migrate

6. Set Permissions

Ensure the storage and bootstrap/cache directories are writable by the web server.

sudo chmod -R 777 /var/www/html/ai-arena/storage/logs
sudo chown -R www-data:www-data /var/www/html/ai-arena/storage
sudo chown -R www-data:www-data /var/www/html/ai-arena/bootstrap/cache

Create a symbolic link for storage.

php artisan storage:link

7. Configure Apache

Create a new Apache configuration file for your Laravel project.

sudo vim /etc/apache2/sites-available/aiarena.zahiralam.com.conf

Add the following configuration to the file:

<VirtualHost *:80>
    ServerName aiarena.zahiralam.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/ai-arena/public

    <Directory /var/www/html/ai-arena/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>

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

Enable the site and restart Apache.

sudo a2ensite aiarena.zahiralam.com.conf
sudo service apache2 restart

8. Secure Your Site with SSL

Install Certbot, a tool to obtain SSL certificates from Let’s Encrypt.

sudo apt install certbot python3-certbot-apache

Run Certbot to obtain and install your SSL certificate.

sudo certbot --apache -d aiarena.zahiralam.com

Follow the prompts to complete the SSL installation.

Conclusion

By following these steps, you will have successfully set up your Laravel code base on an Ubuntu server. Your site will be running securely with SSL, ensuring that your users’ data is protected. If you encounter any issues, refer to the Laravel and Apache documentation for additional troubleshooting tips.

Leave a Reply

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