Git Product home page Git Product logo

deploy-laravel-next.js-application's Introduction

BlueHolding-Technical-Test

Table of Contents

  1. BlueHolding-Technical-Test
  2. Setting Up Ubuntu 20.04 on a Virtual Machine
    1. Prerequisites
    2. Installation Steps
  3. Docker Engine Installation
  4. Git Installation
  5. Generate SSH Key Pair
  6. Clone the Repository
  7. Next.js Application Dockerization
  8. PHP Laravel Application Dockerization
  9. Environment Settings File Creation
  10. Docker Compose File Creation
  11. Applications Deployment
  12. Configure The PHP Container And Configure The App’s Virtual Host in Apache
  13. Access the Applications in The Browser
  14. Dockerize PHP Container Again
  15. Author

Setting Up Ubuntu 20.04 on a Virtual Machine

Prerequisites

Before you begin, ensure you have the following:

Installation Steps

  1. Create a New Virtual Machine:

    • Open your virtualization software.
    • Click "New" to create a new virtual machine.
    • Choose "Linux" as the type and "Ubuntu (64-bit)" as the version.
    • Allocate 5GB ram and processor configuration .
    • Create a new virtual hard disk with 30GB space .
  2. Configure VM Settings:

    • Select the newly created virtual machine in your virtualization software.
    • Go to "Settings."
    • Under the "CD/DVD" tab, select the Ubuntu 20.04 ISO image.
  3. Start the Virtual Machine:

    • Click "Start" to launch the virtual machine.
    • The Ubuntu installation menu will appear. Choose "Install Ubuntu" and press Enter.
  4. Choose Installation Options:

    • Select your language and click "Continue."
    • For "Updates and Other Software," choose to "Download updates while installing Ubuntu" .
  5. Configure Time Zone and User:

    • Select cairo as time zone on the map and click "Continue."
    • Create a user account by providing name, username, password, and computer name.
    • Click "Continue."
  6. Install Ubuntu:

    • The installation process will begin. Wait for it to complete.
    • Once installation is done, click "Restart Now."
  7. Post-Installation Setup:

    • update the system and install additional software using the package manager:

      sudo apt update && sudo apt upgrade

    • install curl

      sudo apt-get install curl

Docker engine installation

  1. Install docker:

curl -fsSL https://get.docker.com -o get-docker.sh

sudo sh ./get-docker.sh

  1. Start and enable the Docker service:

sudo systemctl start docker

sudo systemctl enable docker

Git installation

  1. Install git:

sudo apt install git

  1. configure git:

git config --global user.name "mariohany98"

git config --global user.email "[email protected]"

Generate SSH Key Pair To securely connect to Git repositories using SSH

  1. install ssh:

sudo apt install ssh

  1. generate a private and public key pair:

ssh-keygen -t rsa

  1. copy the public key to git server:

copy the content of the following file: /home/mario/.ssh/id_rsa.pub

navigate to GitHub settings on the UI >>>>>> SSH and GPG keys >>>>>> Add new SSh key

paste the public key and save

Clone the repository to my local machine

git clone [email protected]:mariohany98/BlueHolding-Technical-Test.git

Next.js application Dockerization

  1. Navigate to the code repository:

cd ./BlueHolding-Technical-Test/intranet-front-main

  1. Create Dockerfile for Next.js App

vim Dockerfile

Screenshot 2023-09-21 213146

  1. Build docker image

sudo docker build -t next-app .

  1. login to DockerHub repository to push the image

sudo docker login

  1. Tag the Docker image

note:Before pushing the image to Docker Hub, I need to tag it with my Docker Hub username and the desired repository name

sudo docker tag next-app mariohany98/nextjs-app:latest

  1. Push the Docker Image to DockerHub:

sudo docker push mariohany98/nextjs-app:latest

PHP Laravel application Dockerization

  1. Navigate to the code repository:

cd ./BlueHolding-Technical-Test/blue-internal-back-master

  1. Create Dockerfile for PHP App

vim Dockerfile

Screenshot 2023-09-23 232440

  1. Build docker image

sudo docker build -t mariohany98/php-app:2 .

  1. Push the Docker Image to DockerHub:

sudo docker push mariohany98/php-app:2

Environment Settings File Creation

  1. Navigate to the code repository:

cd ./BlueHolding-Technical-Test/blue-internal-back-master

  1. Create the file:

cat .env.example > .env

  1. Only modify the highlighted lines in .env file as the following:

Screenshot 2023-09-23 234738

Docker Compose File Creation

  1. Navigate to the code repository:

cd ./BlueHolding-Technical-Test/blue-internal-back-master

  1. Create The Docker compose file:

sudo vim docker-compose.yml

Screenshot 2023-09-23 235626

Important Notes:

1- Both the .env file and the docker-compose.yaml file must be in the same directory.

2- The service name of the mysql-db container in the docker-compose.yaml file must be the same as the value of DB_HOST in the .env file.

Applications Deployment

  1. Navigate to the code repository:

cd ./BlueHolding-Technical-Test/blue-internal-back-master

  1. Run the containers:

sudo docker compose up -d

Configure The PHP Container And Configure The App’s Virtual Host in Apache

  1. Execute an interactive shell session (/bin/sh) inside the PHP Docker container:

sudo docker exec -it php /bin/sh

  1. Navigate to /var/www/html/ :

cd /var/www/html

  1. Run the following commands:

chown -R www-data:www-data /var/www/html

  • This command changes the owner and group of the directory and everything in the directory to www-data. www-data is the default user web servers like Apache.

chmod -R 755 /var/www/html

  • This command gives full permission to the user on the directory and everything in the directory. Also it gives only read and execute permission to group and others.

chmod -R 777 /var/www/html/storage

  • This command gives full permissions on these directory, so that no matter what process executes the Laravel application, it will be able to read from and write to them.
  1. Install the dependencies specified in the composer.json

composer install

  1. Install Vim editor:

apt-get install vim

  1. Copy the 000-default.conf file and give it a new name (laravel.conf):

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/laravel.conf

  1. Edit laravel.conf as follows:

vim /etc/apache2/sites-available/laravel.conf

Screenshot 2023-09-23 193502

  1. Disable the default Apache site:

a2dissite 000-default.conf

  1. Enable the Laravel App’s site and mod_rewrite:

a2enmod rewrite

a2ensite laravel.conf

  1. Reload apache service:

service apache2 reload

  1. Install Node.JS and npm:

apt update

apt install nodejs

apt install npm

  1. Check that the installation was successful:

node -v

  1. Navigate to /var/www/html/ :

cd /var/www/html

  1. Apply database migrations and seed the database:

php artisan migrate --seed

  1. Generate JWT secret key for JWT authentication:

php artisan jwt:secret

  1. Install all the dependencies listed in package.json and prepare the application for production use:

npm install && npm run production

  1. Start the web server:

php artisan serve

Access the Applications in The Browser

1.NextJS Application

http://localhost:3000

Screenshot 2023-09-23 192352

  1. PHP Application

http://localhost:8000

Screenshot 2023-09-23 192423

Screenshot 2023-09-23 192616

I have Dockerized the PHP container again so that i don't lose all the previous changes when the container stops

  1. Commit the changes to a new image:

sudo docker commit e0325 mariohany98/php-vuejs-app

  1. Push the image to a GitHub repository:

sudo docker push mariohany98/php-vuejs-app

  1. If the running PHP container has stopped unexpectedly or intentionally. Set the new Docker image (mariohany98/php-vuejs-app) in Docker Compose file before running it

Important Note

Running containers on a single docker host may be good for development and test environment, but in production environment this is not a good idea as this is a single point of failure and to avoid it we should use an orchestration tools like Kubernetes or Docker swarm for high availability and load balancing.

Author

Mario Hany

Feel free to contribute, report issues, or suggest improvements.

deploy-laravel-next.js-application's People

Contributors

mariohany98 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.