DEV Community

Cover image for Add Laravel project to EC2 server, Ubuntu / Nginx with bitbucket pipelines
vimuth
vimuth

Posted on

Add Laravel project to EC2 server, Ubuntu / Nginx with bitbucket pipelines

Hope you have already created and EC2 server and connected via putty. if not this tutorial you can found how to do it.

Deploy Laravel application with Ubuntu Nginx in EC2 server (You don't need to check full tutorial just server creation and connect is enough)

Install Nginx

sudo apt update
sudo apt install nginx -y
sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

PHP

sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.1 php8.1-fpm php8.1-mbstring php8.1-xml php8.1-mysql php8.1-curl php8.1-zip
Enter fullscreen mode Exit fullscreen mode

Install node

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
node -v
npm -v


Enter fullscreen mode Exit fullscreen mode

Clone the project with git

cd /var/www/html
sudo mkdir project
Enter fullscreen mode Exit fullscreen mode

Install composer

sudo curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
sudo chmod +x /usr/local/bin/composer
Enter fullscreen mode Exit fullscreen mode

now go inside the folder and clone the repository

cd /var/www/html/project
Enter fullscreen mode Exit fullscreen mode

Image description

But you will see you will be asked for a password

Image description

We have to do this to get an app password

  1. Log in to Bitbucket: Go to Bitbucket and sign in with your account.
  2. Access Personal Settings: Click on your profile icon in the bottom left corner, then click "Personal settings".
  3. App Passwords: Navigate to "Access Management" > "App passwords" in the left sidebar.
  4. Create App Password: Click on the "Create app password" button.
  5. Select Permissions: Give the app password the necessary permissions for your tasks. For Git operations, you typically need "Repositories" read and/or write access.
  6. Create: Name your app password and click "Create".
  7. Copy the Password: Make sure to copy the app password and store it securely; you won’t be able to see it again.

But there is a n important point here. When you are going to pull the repo you will see this,
Image description
They ask app password for username 'vimuths'. We can avoid this by removing username from clone url. That way any user will be able to clone with there user name and password
Image description

git clone https://bitbucket.org/yourgit/rourrepo.git .
Enter fullscreen mode Exit fullscreen mode

Here we have a tricky part. Think you have noticed git clone command is asking for a password here. This gives errors for pipelines. We can avoid it like this

cd /var/www/html/project/
sudo vi .git/config
Enter fullscreen mode Exit fullscreen mode

Now edit this line...

[remote "origin"]
        url = https://bitbucket.org/yourgit/yourrepo.git
Enter fullscreen mode Exit fullscreen mode

To (Add username and app password)

[remote "origin"]
        url = https://username:app_password@bitbucket.org/yourgit/yourrepo.git
Enter fullscreen mode Exit fullscreen mode

Now when you do a 'sudo git pull' there will not be any prompt to input username and password

now add this file to root
bitbucket-pipelines.yml

image: atlassian/default-image:2

pipelines:
  branches:
    main:
      - step:
          name: Deploy and Migrate
          script:
            - pipe: atlassian/ssh-run:0.2.8
              variables:
                SSH_USER: 'ubuntu'
                SERVER: 'your_ip'
                COMMAND: 'sudo chown -R ubuntu:ubuntu /var/www/html/project && git config --global --add safe.directory /var/www/html/project && cd /var/www/html/project && sudo git pull origin main && sudo composer install --no-interaction && sudo chown -R www-data:www-data /var/www/html/project'
Enter fullscreen mode Exit fullscreen mode

Make sure to replace *your_ip * with your actual server ip address

Then you need to enable pipelines inside your repository. This section is inside repository settings

Image description

Then you need to create a public key and private key (.pem) using puttygen. And insert them there.

Image description

And don't forget to add the server ip to known hosts.

Image description

Now SSH into server again and add these commands,

cd /var/www/html/project/
sudo composer install
sudo chmod -R 775 /var/www/html/project/storage
sudo cp .env.example .env
sudo php artisan key:generate
Enter fullscreen mode Exit fullscreen mode

Now if you do a git commit from your local machine you will see pipelines run successfully.

Image description

Now let's add .sh file or shell script to make this more complete. create .deploy.sh file inside root.

#!/bin/bash

# Change ownership to ubuntu
sudo chown -R ubuntu:ubuntu /var/www/html/project

# Mark the repository as a safe directory
git config --global --add safe.directory /var/www/html/project

# Change to the project directory
cd /var/www/html/project

# Pull the latest changes
sudo git pull origin main

# Install dependencies without dev dependencies and without interaction
sudo composer install --no-interaction

# Change ownership back to www-data for the web server
sudo chown -R www-data:www-data /var/www/html/project
Enter fullscreen mode Exit fullscreen mode

And call him here in pipelines file

Image description

COMMAND: 'cd /var/www/html/project && ./deploy.sh'

And run this in cli in server.

sudo chmod +x /var/www/html/project/deploy.sh
Enter fullscreen mode Exit fullscreen mode

Top comments (0)