Documentation for Server

Here’s a step-by-step guide to set up your domain with subdomains using GoDaddy and DigitalOcean:


1. Create an Account on DigitalOcean

  1. Sign up for DigitalOcean at https://www.digitalocean.com.

  2. Complete the registration process and verify your account using the payment method.


2. Create a Droplet on DigitalOcean

  1. Log in to your DigitalOcean dashboard.

  2. Click on the "Create" button and select "Droplets".

  3. Choose an image for the server:

    • Select Ubuntu 22.04 (or the latest stable version).

  4. Choose the plan (Basic/Standard is usually sufficient).

  5. Select the data center region closest to your audience.

  6. Add SSH keys (or choose a password-based login if no SSH key is available).

  7. Assign a hostname (e.g., example-droplet).

  8. Click "Create Droplet".


3. Get the Nameserver Information from DigitalOcean

  1. Go to the "Networking" section in the DigitalOcean dashboard.

  2. Click on "Domains" and add your domain (example.com).

  3. DigitalOcean will provide nameservers:


4. Update Nameservers on GoDaddy

  1. Log in to your GoDaddy account.

  2. Navigate to "My Products" and find your domain (example.com).

  3. Click "DNS" next to the domain.

  4. Under Nameservers, click Change.

  5. Choose Custom Nameservers and add:

  6. Save changes (it may take up to 48 hours for the DNS propagation, though usually faster).


5. Add Subdomains to DigitalOcean

  1. Go to Networking -> Domains in the DigitalOcean dashboard.

  2. Select your domain (example.com).

  3. Add CNAME records for the subdomains:

    • Subdomain 1 (e.g., api.example.com):

      • Type: CNAME

      • Name: api

      • Hostname: example.com (or the droplet's IP if using A records).

      • Will be use for host the laravel application which will serve api

    • Subdomain 2 (e.g., phpmyadmin.example.com):

      • Type: CNAME

      • Name: phpmyadmin

      • Hostname: example.com.

      • Will use for phpmyadmin installations path.

    • Subdomain 3 (e.g., adminpanel.example.com):

      • Type: CNAME

      • Name: adminpanel

      • Hostname: example.com.

      • Will be use for host the react application which is required for adminpanel frontend

  4. Save each record.



7. Test and Troubleshoot

  • Use a tool like https://dnschecker.org to confirm DNS propagation.

  • If the subdomains don't resolve, ensure the CNAME records are correct and the nameservers are pointing to DigitalOcean.


Here's a comprehensive guide for setting up your server on DigitalOcean with Nginx for hosting the specified applications and subdomains:


1. Initial Server Setup

Update and upgrade system packages:

sudo apt update

Install essential tools:

sudo apt install -y curl wget unzip git build-essential ufw software-properties-common

Install Nginx:

sudo apt install -y nginx

Enable and start Nginx:

sudo systemctl enable nginx
sudo systemctl start nginx

Configure the firewall:

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

Configure Nginx for Applications

Step for running Laravel app which will required for API of this application:

Here's a step-by-step guide to set up and run a Laravel application for the API, assuming it will be hosted on api.example.com with PHP 8.1:


Step 1: Install Prerequisites

  1. Update your package list and install required dependencies:

sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx php8.1 php8.1-fpm php8.1-mbstring php8.1-xml php8.1-bcmath php8.1-curl php8.1-zip php8.1-mysql unzip curl mysql-client composer
  1. Verify PHP version:

php -v

3. Install MySQL

Install MySQL server:

sudo apt install -y mysql-server

Secure MySQL installation:

sudo mysql_secure_installation

Create a database and user for Laravel:

Here’s how to create a MySQL user named admin with the password 123456123Password and grant it full access to all databases:


Step-by-Step Instructions

  1. Log in to MySQL as the root user:

sudo mysql -u root -p
  1. Create the admin user with the specified password:

CREATE USER 'admin'@'%' IDENTIFIED BY '123456123Password';
  1. Grant full access to all databases:

GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION;
  1. Apply the changes:

FLUSH PRIVILEGES;
  1. Exit MySQL:

EXIT;

Verify the User

To test the admin user's access:

  1. Log in as the admin user:

mysql -u admin -p
  1. Enter the password (123456123Password) when prompted.

  2. Check access by listing the databases:

SHOW DATABASES;

Important Security Note

  • Ensure the password is stored securely and not shared publicly.

  • Restrict access to specific IPs if needed by modifying the % wildcard in the CREATE USER command:

CREATE USER 'admin'@'localhost' IDENTIFIED BY '123456123Password';

Let me know if you need further assistance!


Step 3: Set Up Laravel Application

  1. Navigate to your project directory:

cd /var/www/
  1. Download or clone your Laravel project:

git clone https://github.com/your-repo/laravel-api.git
cd laravel-api
  1. Here’s how to set up Composer and use it to install dependencies for a Laravel application:



Step 1: Install Composer

  1. Download the Composer Installer:

curl -sS https://getcomposer.org/installer | php
  1. Move Composer to a Global Location:

sudo mv composer.phar /usr/local/bin/composer
  1. Verify Installation:

composer --version
  1. Step 2: Install Dependencies

  2. Navigate to the Laravel project directory:

cd /path/to/your/laravel-project
  1. Run the composer install command to install dependencies:

composer update
  1. Ensure permissions are set correctly for the vendor and storage directories:

sudo chown -R $USER:$USER /path/to/your/laravel-project
sudo chmod -R 775 storage bootstrap/cache


Step 3: Common Composer Commands

  • Update Dependencies:

composer update
  • Check Dependencies for Issues:

composer check-platform-reqs
  • Clear Composer Cache:

composer clear-cache
  1. Set permissions for storage and cache:

sudo chown -R www-data:www-data /var/www/laravel-api
sudo chmod -R 775 /var/www/laravel-api/storage /var/www/laravel-api/bootstrap/cache
  1. Set up the .env file:

    • Copy the .env file:

  cp .env.example .env
E
    • it .env with your database credentials:

  DB_CONNECTION=mysql
  DB_
OST=127.0.0.1
  DB_PO
T=3306
  DB_DATA
ASE=laravel_api
  DB_USERNA
E=laravel_user
  DB_PASSWORD
secure_password



Step 4: Con

igure Nginx for api.example.com

  1. Create a new Nginx configuration file:

sudo nano /etc/nginx/sites-available/api.example.com
  1. Add the following configuration:

server {
    listen 80;
    server_name api.example.com;

    root /var/www/laravel-api/public;
    index index.php index.html;

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

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

    location ~ /\.ht {
        deny all;
    }

    error_log /var/log/nginx/api.example.com-error.log;
    access_log /var/log/nginx/api.example.com-access.log;
}
  1. Enable the site:

sudo ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/
  1. Test Nginx configuration:

sudo nginx -t
  1. Reload Nginx:

sudo systemctl reload nginx


Step 5: Secure with SSL

  1. Install Certbot for Nginx:

sudo apt install -y certbot python3-certbot-nginx
  1. Obtain and apply the SSL certificate:

sudo certbot --nginx -d api.example.com
  1. Test automatic renewal:

sudo certbot renew --dry-run


Step 6: Run Laravel Application

  1. Ensure the PHP-FPM service is running:

sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm


Step 7: Verify Everything

  1. Open http://api.example.com or https://api.example.com in your browser to confirm the API is running.

  2. Check Laravel logs for errors:

tail -f /var/www/laravel-api/storage/logs/laravel.log

Let me know if you need assistance with any of these steps!



Step for running Next.js app which is Frontned of this application:


Here's how to configure Nginx separately for the Next.js application hosted on example.com and set up the root path /var/www/projectNameFolder. We'll also configure Node.js with nvm to use the highest Node.js version available.


Step 1: Install Node.js Using NVM

  1. Install nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
source ~/.bashrc
  1. Verify nvm installation:

command -v nvm
  1. Install the latest Node.js version:

nvm install node
  1. Use the highest version:

nvm use node
  1. Verify the installed version:

node -v
npm -v

Step 2: Set Up Next.js Application

  1. Create the project directory:

sudo mkdir -p /var/www/projectNameFolder
sudo chown -R $USER:$USER /var/www/projectNameFolder
  1. Navigate to the directory and initialize a Next.js application:

cd /var/www/projectNameFolder


Step 3: Configure Nginx for example.com

  1. Create a new Nginx configuration file:

sudo nano /etc/nginx/sites-available/example.com
  1. Add the following configuration:

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/projectName;
    index index.html;

    location / {
        proxy_pass http://127.0.0.1:3001; # Default Next.js port
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    error_page 404 /404.html;

  
}
  1. Enable the configuration:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
  1. Test the Nginx configuration:

sudo nginx -t
  1. Reload Nginx:

sudo systemctl reload nginx


Step 4: Secure with SSL

(Optional) If you'd like to secure your site with SSL:

  1. Install Certbot:

sudo apt install -y certbot python3-certbot-nginx
  1. Obtain and apply an SSL certificate:

sudo certbot --nginx -d example.com -d www.example.com
  1. Verify automatic renewal:

sudo certbot renew --dry-run


Start the Next.js app with PM2:

Go to project folder and run this command one by one:

npm install -g pm2
pm2 start npm --name "projectNameFolder" -- start
pm2 save


Step 5: Verify Everything

  1. Visit http://example.com to ensure the application is running.

  2. Check https://example.com if SSL is applied.




Here's a step-by-step guide to set up your React Admin Panel at adminpanel.example.com.



This setup assumes you're cloning the React app from a GitHub repository, running npm install, building the project with npm run build, and serving it with Nginx.


Step 1: Install Prerequisites on Your Server

  1. Update your server and install dependencies:

sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx git curl build-essential
  1. Install Node.js: Use nvm (Node Version Manager) to install the latest stable version of Node.js:

curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs
  1. Check Node.js and npm versions:

node -v
npm -v

Step 2: Clone the React Admin Panel Repository

  1. Navigate to the directory where you want to store the React app:

cd /var/www/
  1. Clone your React admin panel repository:

git clone https://github.com/your-repo/react-admin-panel.git
cd react-admin-panel
  1. Install the required npm packages:

npm install

Step 3: Build the React Admin Panel

  1. Build the React project for production:

npm run build
  1. Confirm the build directory is created inside the project:

ls build

Step 4: Configure Nginx for Serving the React App

  1. Create a new Nginx configuration file for adminpanel.example.com:

sudo nano /etc/nginx/sites-available/adminpanel.example.com
  1. Add the following configuration:

server {
    listen 80;
    server_name adminpanel.example.com;

    root /var/www/react-admin-panel/build;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    error_log /var/log/nginx/adminpanel.example.com-error.log;
    access_log /var/log/nginx/adminpanel.example.com-access.log;
}
  1. Create a symlink to enable the site:

sudo ln -s /etc/nginx/sites-available/adminpanel.example.com /etc/nginx/sites-enabled/
  1. Test the Nginx configuration for syntax errors:

sudo nginx -t
  1. Reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 5: Set Up SSL (Optional but Recommended)

  1. Obtain an SSL certificate:

sudo certbot --nginx -d adminpanel.example.com
  1. Test SSL Renewal:

sudo certbot renew --dry-run

Step 6: Verify Everything

  1. Open http://adminpanel.example.com or https://adminpanel.example.com in your browser to verify the React admin panel is running correctly.

  2. Check the Nginx error logs if you face issues:

tail -f /var/log/nginx/adminpanel.example.com-error.log

Step 7: Additional Configurations (Optional)

  1. To serve the app using a custom domain with Nginx: Make sure the domain adminpanel.example.com is correctly configured on your DNS provider (GoDaddy or others) by pointing the A record to your server’s IP address.

  2. To set up Nginx caching (optional): You can enable caching for the static assets in the React app:

location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|eot)$ {
    expires 30d;
    access_log off;
}

Step 8: Check for Errors

If you face issues, check these:

  1. Nginx logs:

tail -f /var/log/nginx/adminpanel.example.com-error.log
  1. React build output errors: If the React build fails, try running npm run build again and check for errors in the terminal.

  2. React app not loading correctly: Make sure you have the correct build path and that Nginx is correctly pointing to /build in the project.