Skip to content

Setting up the remote server and installing prerequisites

Emmanuel Nwanochie edited this page Jul 20, 2024 · 1 revision

Introduction

To ensure our application runs smoothly, we need to set up a remote server with all the necessary prerequisites. This includes installing Git, Python, pip, and PostgreSQL. This guide walks you through each step, ensuring your server is ready for deployment.

Server Setup

  1. Accessing the Remote Server
    To access the remote server, you will need SSH credentials. Use the following command to connect to your server:
ssh username@your_server_ip

Replace username with your server's username and your_server_ip with the server's IP address.

  1. Updating the System
    Before installing any software, it's good practice to update the system packages to their latest versions:
sudo apt update && sudo apt upgrade -y
  1. Installing Git
    Git is essential for cloning the repository and managing code. Install Git and verify the version installed by using the following commands:
sudo apt install git -y
git --version
  1. Installing Python and pip
    Our application is built using Python, so we need to install Python and pip (Python's package installer):
sudo apt install python3 python3-pip -y

Verify the installation:

python3 --version
pip3 --version

PostgreSQL Setup

  1. Installing PostgreSQL PostgreSQL is our database of choice. Install PostgreSQL using the following commands:

Add the PostgreSQL APT repository:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Import the repository signing key:

wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Update the package lists and install the latest stable version of PostgreSQL:

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

Start and enable the postgresql service:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Check the status:

sudo systemctl status postgresql
  1. Configuring PostgreSQL

Switch to the PostgreSQL user to configure the database:

sudo -i -u postgres 

Setup Databases and Users
Create a new PostgreSQL user and databases for the application:

  • Create a new user:
createuser <user-name> --pwprompt

This will prompt for the password of the new user. Enter a secure password following best practices. Replace in the command with your desired user name.

  • Create a new database:
createdb <new-db>

Create three databases for the dev, staging and prod respectively by replacing with the appropriate names. e.g python_dev_db, python_staging_db, python_prod_db

  • Grant all privileges on the database to the new user:
GRANT ALL PRIVILEGES ON DATABASE <new-db> TO <user-name>;

Assuming username is pythonguru

GRANT ALL PRIVILEGES ON DATABASE python_dev_db TO pythonguru;

Repeat this for staging and prod databases respectively.

Exit the PostgreSQL prompt:

\q

NGINX installation

Nginx is a powerful web server that will be used to serve our application and handle requests. Install Nginx using the following command:

sudo apt install nginx -y

Verify the installation:

sudo apt install nginx -y

Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Create a configuration file in the conf.d directory. Within this directory, the conf.d folder is often used to store additional configuration files. This is especially necessary if the server is shared between multiple teams.

sudo touch /etc/nginx/conf.d/<pythonapp.conf>

Replace <pythonapp.conf> with the name of the configuration file.

RabbitMQ

RabbitMQ is a message broker that Celery uses to handle asynchronous tasks Install RabbitMQ using the following commands:

sudo apt install rabbitmq-server -y

Verify the installation:

sudo systemctl status rabbitmq-server

Start and enable RabbitMQ:

sudo systemctl start rabbitmq-server
sudo systemctl enable rabbitmq-server

Cloning of repo and creating of app directories

  1. Create a directory named python and change into the directory
mkdir python
cd python
  1. The repository should be cloned into the created directory using the following command:
git clone <repository-url>

Replace the with the github repository url of the app.

git clone https://github.com/hngprojects/hng_boilerplate_python_fastapi_web.git
  1. Make copies of the directory renaming them to dev, staging and prod respectively.
cp -r /path/to/your/application/yourrepository/* /path/to/your/application/dev/
cp -r /path/to/your/application/yourrepository/* /path/to/your/application/staging/
cp -r /path/to/your/application/yourrepository/* /path/to/your/application/prod/

You alternatively, within the location of application directory, first, rename the directory hng_boilerplate_python_fastapi_web to a manageable name.

mv hng_boilerplate_python_fastapi_web source_code
cp -r source_code source_dev_code
cp -r source_code source_staging_code
cp -r source_code source_prod_code

These application directories represent the dev, staging and prod environments repectively.

Clone this wiki locally