Tech4Welfare Solution is one of the leading initiatives offering an entire range of IT products and services.


Step-by-Step Guide to Installing or Moving an Existing Laravel Application to a Subdomain on a Fresh VPS

Installing Laravel on a Subdomain on VPS

Step-by-Step Guide to Installing or Moving an Existing Laravel Application to a Subdomain on a Fresh VPS

This guide will walk you through the entire process of setting up a new user, configuring a domain, and deploying a Laravel application to a subdomain on a fresh VPS. The steps include creating a new user with sudo privileges, setting up a domain, and finally, installing or moving your Laravel application.



Step 1: Create a New User with Sudo Privileges

First, you should create a new user on your VPS to improve security. This user will have sudo privileges to execute administrative tasks.



1.1 Access Your VPS as the Root User

ssh root@your_vps_ip


1.2 Create a New User

adduser your_new_username

You'll be prompted to enter a password and some basic information about the user. Once completed, the new user will be created.



1.3 Grant Sudo Privileges to the New User

usermod -aG sudo your_new_username

This command adds the new user to the sudo group, giving them administrative privileges.



1.4 Switch to the New User

su - your_new_username


1.5 Test Sudo Access

sudo whoami

If the setup is correct, the command should return root.



Step 2: Set Up Your Domain and Subdomain

Before deploying your Laravel application, you need to set up your domain and subdomain. This includes pointing your domain to your VPS and creating the necessary directories.



2.1 Point Your Domain to Your VPS

In your domain registrar's control panel, set the A record of your domain to point to your VPS's IP address.



Note: DNS changes can take some time to propagate. It may take up to 48 hours, but it's usually quicker.



2.2 Create a Subdomain Directory

sudo mkdir -p /var/www/subdomain.yourdomain.com


2.3 Assign Ownership and Set Permissions

sudo chown -R your_new_username:your_new_username /var/www/subdomain.yourdomain.com
sudo chmod -R 755 /var/www/subdomain.yourdomain.com


Step 3: Install Necessary Packages

With your domain and subdomain set up, install the necessary packages to run your Laravel application.



3.1 Update Your Server and Install Packages

sudo apt update && sudo apt upgrade -y
sudo apt install nginx git unzip curl -y


3.2 Install PHP and Composer

sudo apt install php-fpm php-cli php-mysql php-zip php-gd php-mbstring php-curl php-xml php-bcmath php-json -y
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer


3.3 Verify the Installation

php -v
composer -v


Step 4: Configure Nginx for the Subdomain

Next, configure Nginx to serve your Laravel application from the subdomain.



4.1 Create an Nginx Configuration File

sudo nano /etc/nginx/sites-available/subdomain.yourdomain.com

Add the following content:

server {
    listen 80;
    server_name subdomain.yourdomain.com;
    root /var/www/subdomain.yourdomain.com/public;

    index index.php index.html index.htm index.nginx-debian.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}


4.2 Enable the Configuration and Restart Nginx

sudo ln -s /etc/nginx/sites-available/subdomain.yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 5: Install or Move Your Laravel Application

Now you can install a new Laravel application or move an existing one to your VPS.



5.1 Install a New Laravel Application

cd /var/www/subdomain.yourdomain.com
composer create-project --prefer-dist laravel/laravel .


5.2 Move an Existing Laravel Application

If you are moving an existing application, upload your project files to the VPS:

scp -r /path/to/your/laravel/project/* your_new_username@your_vps_ip:/var/www/subdomain.yourdomain.com


Step 6: Configure Environment Variables

Update your environment variables to match your server's configuration.



6.1 Copy the .env File

scp /path/to/your/.env your_new_username@your_vps_ip:/var/www/subdomain.yourdomain.com


6.2 Update the .env File

Update the .env file with your server settings, including database credentials and the APP_URL:

APP_URL=http://subdomain.yourdomain.com
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=password

Step 7: Set File Permissions

Ensure that the correct file permissions are set for the Laravel application.



7.1 Set Permissions for the storage and bootstrap/cache Directories

sudo chown -R www-data:www-data /var/www/subdomain.yourdomain.com/storage /var/www/subdomain.yourdomain.com/bootstrap/cache
sudo chmod -R 775 /var/www/subdomain.yourdomain.com/storage /var/www/subdomain.yourdomain.com/bootstrap/cache

Step 8: Set Up the Database

Configure your database and update the Laravel application settings.



8.1 Create a New Database

CREATE DATABASE laravel_db;


8.2 Create a Database User

CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;

8.3 Update the .env File

DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=password


8.4 Run Migrations

php artisan migrate


Step 9: Test Your Application

Visit http://subdomain.yourdomain.com in your browser to confirm that your Laravel application is running.



Conclusion

By following these steps, you've successfully set up a secure environment on your VPS and deployed a Laravel application to a subdomain. This guide ensures that your application is correctly installed and configured for smooth operation.

Newest
Previous
Next Post »

Quote

"Educationists should build the capacities of the spirit of inquiry, creativity, entrepreneurial and moral leadership among students and become their role model."

Dr. Abdual Kalam