Coding Essentials: Python Installation, Ubuntu Service Setup, and Bitbucket Pipeline Configuration WalkthroughCopy link
Install Python 3.9 on Ubuntu
Update Package List:
Open a terminal and run the following command to ensure that your package list is up to date:
sudo apt update
Install the required dependencies for building Python:
sudo apt install software-properties-common
Add Python Repository:
Ubuntu's default repositories may not have the latest Python 3.9 package. You can add the "deadsnakes" PPA (Personal Package Archive), which provides updated versions of Python:
sudo add-apt-repository ppa:deadsnakes/ppa
Update Package List Again:
After adding the PPA, update your package list again:
sudo apt update
Install Python 3.9:
Now, you can install Python 3.9 with the following command:
sudo apt install python3.9
Verify Python Installation:
You can verify that Python 3.9 has been installed successfully by running:
This command should display the Python 3.9 version.
Install pip (Python Package Manager):
sudo apt install python3.9-distutils wget https://bootstrap.pypa.io/get-pip.py sudo python3.9 get-pip.py
That's it! You should now have Python 3.9 installed on your Ubuntu system. You can use Python 3.9 by running the python3.9 command, and you can install Python packages using pip3.9.
Creating a Linux Service for Python FastAPI
Creating a service in Ubuntu involves setting up a systemd service unit. Systemd is a system and service manager for Linux systems, including Ubuntu. With systemd, you can define and manage services, which are background processes or daemons that run automatically at system startup or on demand. Here's a step-by-step guide on how to create a systemd service in Ubuntu:
Before creating a systemd service unit, you should have a clear understanding of what your service does and what command or script it uses to run. Make sure you know the path to the executable or script file.
Service unit files are typically located in the
/etc/systemd/system/ directory and have a .service file extension. You can create a new service unit file using a text editor like nano or vim with administrative privileges:
hoyo-service with your desired service name and replace
hoyo-backend with your desired project name.
sudo nano /etc/systemd/system/hoyo-service.service
In the service unit file you just created, define the service by providing the necessary configuration. Here's a basic template:
[Unit] Description=Hoyo Backend Service After=network.target [Service] ExecStart=/var/www/html/hoyo-backend/venv/bin/python3.9 -m gunicorn main:core -w 1 --access-logfile /var/log/hoyo-backend/gunicorn-access.log --error-logfile /var/log/hoyo-backend/gunicorn-error.log -k uvicorn.workers.UvicornWorker --workers 1 WorkingDirectory=/var/www/html/hoyo-backend/ User=username (optional) Group=groupname (optional) Restart=always (optional) Type=simple (optional) [Install] WantedBy=multi-user.target
This description provides an overview of how to configure a service in a systemd service unit file. You define your service by specifying its description, dependencies (services that must start before it), the command to start it (ExecStart), the working directory (optional), the user and group under which it should run (optional), restart behavior (optional), process type (optional, usually set to "simple"), and when it should be started (optional). This information is essential for systemd to manage your service effectively.
Save the changes to the service unit file and exit the text editor.
After creating or modifying a service unit file, you should reload systemd to apply the changes:
sudo systemctl daemon-reload
Now, you can start your service and enable it to start automatically at boot:
sudo systemctl start hoyo-service sudo systemctl enable hoyo-service
To check the status of your service:
sudo systemctl status hoyo-service
You've now created a systemd service in Ubuntu, and it will run according to the configuration you specified in the service unit file. You can also use systemctl commands to manage the service, such as stopping, restarting, or disabling it.
Provide Bitbucket Cloud with the SSH key pair
To include an SSH key pair in a Bitbucket Pipeline, follow these steps:
- Go to bitbucket.org, find your repository, and click on "Repository settings."
- In the "Pipelines" section, choose "SSH keys."
- Click on "Generate keys."
- Copy the content of the public key file and paste it into the server's SSH authorization file.
- Additionally, record the host address's fingerprint and add this address to the known hosts list.
Configure project bitbucket-pipelines.yml
To generate a bitbucket-pipelines.yml file in your project, adhere to these steps:
Access Your Bitbucket Repository:
- Log in to your Bitbucket account.
- Navigate to the repository where you intend to set up Bitbucket Pipelines.
Create a New File:
- Inside your repository, click on the "Create" button.
- Choose "File" from the dropdown menu.
Name the File:
- Assign the filename as
.bitbucket-pipelines.yml, ensuring you include the leading dot before "bitbucket-pipelines.yml." This adheres to the hidden file convention.
- Click on the freshly created
bitbucket-pipelines.ymlfile to open it for editing.
Incorporate Your Pipeline Configuration:
- Below is a fundamental example for a Python project:
pipelines: branches: master: - step: name: 'Create zip' image: atlassian/default-image:2 script: - zip -r hoyo-backend.zip * artifacts: - hoyo-backend.zip - step: name: 'SCP AND SSH server setup' image: atlassian/default-image:2 script: - scp -r hoyo-backend.zip email@example.com:/var/www/html/ - ssh firstname.lastname@example.org 'unzip -o /var/www/html/hoyo-backend.zip -d /var/www/html/hoyo-backend' - ssh email@example.com 'rm -rf /var/www/html/hoyo-backend.zip' - ssh firstname.lastname@example.org 'python3.9 -m venv /var/www/html/hoyo-backend/venv' - ssh email@example.com '/var/www/html/hoyo-backend/venv/bin/python3.9 -m pip install -r /var/www/html/hoyo-backend/requirements.txt' - ssh firstname.lastname@example.org 'sudo service hoyo-service restart'
Save the bitbucket-pipelines.yml File:
- After adding your pipeline configuration, click the "Commit" button to save the file.
Trigger the Pipeline:
- Once the bitbucket-pipelines.yml file is committed to your repository, Bitbucket Pipelines will automatically detect it and use it to build and deploy your code whenever you push changes to the repository.
That's it! You've now created a bitbucket-pipelines.yml file in your Bitbucket repository, and Bitbucket Pipelines will use this configuration to automate your build and test processes. Customize the pipeline according to your project's needs, and make sure to replace the Python version, dependency installation, and testing commands with those relevant to your project.
Sep 20, 2023