I. Introduction
Brief overview of Nginx and Docker:
Nginx is a widely used high-performance web server, load balancer, and reverse proxy that has gained immense popularity among web developers and system administrators due to its efficiency and speed. In this blog post, we will guide you through the process of setting up a Nginx project on Ubuntu. We will start by cloning the Nginx-project repo, followed by the installation of NGINX and Docker on Ubuntu. After this, we will walk you through the process of building and running a Docker container and configuring NGINX to work with it. By the end of this blog post, you will have a fully functional notes app running on your Ubuntu system, accessible through a web browser.
Purpose of the blog:
The purpose of this blog is to provide a step-by-step guide for installing NGINX on Ubuntu, cloning a Git repository, installing Docker, building and running a Docker container, and configuring NGINX to proxy pass to the Docker container. The end result will be the successful deployment of a notes application on a web server. This guide is intended for developers and system administrators who want to deploy web applications using NGINX and Docker on Ubuntu.
II. Cloning the Nginx-project Repo
Installing Git on Ubuntu:
Git is a version control system that allows developers to keep track of changes in their code. It is an essential tool for collaboration and managing code repositories. In this section, we will cover how to install Git on Ubuntu.
The installation process is straightforward and can be done using the Ubuntu package manager. The following command will install Git:
sudo apt-get install git
After running this command, the package manager will download and install Git on your system. Once the installation is complete, you can check the version of Git installed by running the following command:
git --version
If Git is installed correctly, you should see the version number printed in the output. With Git installed, you can now start using it to manage your code repositories.
Cloning the Nginx-project repository:
Once Git is installed on your Ubuntu system, you can clone the Nginx-project repository by following these steps:
Open the terminal on your Ubuntu system.
Run the following command to navigate to the directory where you want to clone the repository:
cd /path/to/directory
Replace
/path/to/directory
with the path to the directory where you want to clone the repository.Run the following command to clone the repository:
git clone https://github.com/sanket363/Nginx-project.git
This will download a copy of the Nginx-project repository to your local system.
Once the repository is cloned, you can navigate to the directory using the following command:
cd Nginx-project
You can now continue with the installation of NGINX and Docker and configure NGINX as a reverse proxy for your Docker container as outlined in the rest of this blog post.
III. Installing NGINX on Ubuntu
Prerequisites for installing NGINX:
To install NGINX on Ubuntu, you will need the following prerequisites:
An Ubuntu server: You will need a running Ubuntu server. The latest version of Ubuntu is recommended.
Root privileges: You will need root privileges or sudo access to install packages and make changes to system configurations.
Update Ubuntu packages: Make sure the Ubuntu package repository is up-to-date with the latest packages by running the following command:
sudo apt-get update
- Install a compiler: Install the GCC compiler and related tools to compile and install NGINX from the source code.
sudo apt-get install build-essential
- Install OpenSSL: OpenSSL is a library for secure communication. NGINX requires OpenSSL to be installed.
sudo apt-get install libssl-dev
Configure Firewall: If you have a firewall enabled on your Ubuntu server, make sure to allow HTTP and HTTPS traffic through it.
Decide on a method of installation: You can install NGINX on Ubuntu from the official Ubuntu repositories or from the source code. If you choose to install from the source code, you will need to download it from the NGINX website.
With these prerequisites, you are now ready to install NGINX on your Ubuntu server.
Updating package index:
Updating the package index is the process of downloading the latest information about the available software packages from the configured repositories on your system. This is necessary to ensure that you have access to the latest versions of software packages and their dependencies.
To update the package index on an Ubuntu system, you can use the following command:
sudo apt-get update
This command will refresh the list of available packages from the configured repositories and update the package index. It is recommended to run this command before installing any new software packages or updates on your Ubuntu system to ensure that you have the latest versions available.
Note that updating the package index does not install any new packages or updates, but only updates the local package index. To install new software packages or updates, you will need to use the appropriate command for your package manager, such as apt-get install
or apt-get upgrade
.
Installing NGINX with apt-get:
To install NGINX on Ubuntu using apt-get
, follow these steps:
- Update the package index:
sudo apt-get update
- Install NGINX:
sudo apt-get install nginx
Starting and verifying NGINX:
- Verify that NGINX is running:
sudo systemctl status nginx
This will show the status of the NGINX service. If NGINX is running, you should see the message "active (running)".
- Test the installation:
Open a web browser and enter the IP address of your Ubuntu server in the address bar. You should see the default NGINX welcome page, indicating that NGINX has been successfully installed and is running.
That's it! You have successfully installed NGINX on your Ubuntu system using apt-get
. You can now begin configuring NGINX to serve your web content.
Enabling NGINX to start automatically after reboot:
For enabling the Nginx to start automatically after reboot use below command:
sudo systemctl enable nginx
IV. Docker Installation
Installing Docker with apt-get:
To install Docker on Ubuntu using apt-get
, follow these steps:
- Update the package index:
sudo apt-get update
- Install Docker:
sudo apt-get install docker.io
- Verify that Docker is installed:
sudo docker --version
This will display the version of Docker that is installed on your system.
- Start the Docker service:
sudo systemctl start docker
- Enable Docker to start automatically on boot:
sudo systemctl enable docker
- Verify that Docker is running:
sudo systemctl status docker
This will display the status of the Docker service. If Docker is running, you should see the message "active (running)".
That's it! You have now installed Docker on your Ubuntu system using apt-get
. You can now begin using Docker to run and manage containers on your system.
Adding a user to the Docker group:
By default, the Docker daemon runs with root privileges, which means that any user who has access to the Docker daemon has full root access to the system. To avoid this security risk, it is recommended to add users who need to run Docker commands to the docker
group. Users in the docker
group can run Docker commands without the need for root privileges.
To add a user to the docker
group on Ubuntu, follow these steps:
- Create a
docker
group (if it doesn't already exist):
sudo groupadd docker
- Add the user to the
docker
group:
sudo usermod -aG docker $user
Replace <username>
with the username of the user you want to add to the docker
group.
Rebooting the system to apply changes:
For getting the above changes reflect need to restart the system you can do it by below command
sudo reboot
V. Building and Running Docker Container
Navigating to the directory with the Dockerfile:
To navigate to the directory with the Dockerfile for the Nginx project on Ubuntu, follow these steps:
Open a terminal window on your Ubuntu system.
Change to the root directory by running the following command:
cd /
- Navigate to the
Nginx-project
directory by running the following command:
cd Nginx-project
This assumes that the Nginx-project
directory is located in the root directory. If it is located in a different directory, you will need to navigate to that directory instead.
- Verify that the Dockerfile is located in the
Nginx-project
directory:
ls
This will display a list of the files and directories in the current directory. The Dockerfile should be listed among them.
That's it! You have now navigated to the directory with the Dockerfile for the Nginx project on your Ubuntu system. You can now build and run the Docker container using the Dockerfile.
Building the Docker container:
To build the Docker container for the Nginx project on Ubuntu, follow these steps:
Open a terminal window on your Ubuntu system.
Navigate to the directory with the Dockerfile for the Nginx project.
Run the following command to build the Docker container:
docker build -t <image-name> .
The docker build
command builds a Docker image from a Dockerfile in the current directory and tags it with the specified name (my-nginx-app
in this case). The dot (.
) at the end of the command specifies that the Dockerfile is located in the current directory.
Wait for the Docker container to build. This may take a few minutes, depending on the size and complexity of the Dockerfile.
Verify that the Docker container has been built by running the following command:
docker images
This will display a list of the Docker images that are currently installed on your system. The my-nginx-app
image should be listed among them.
That's it! You have now built the Docker container for the Nginx project on your Ubuntu system. You can now run the container using the docker run
command.
Running the Docker container and mapping port 8000:
To run the Docker container for the Nginx project on Ubuntu and map port 8000 to the container's port 8000, follow these steps:
Open a terminal window on your Ubuntu system.
Run the following command to start the Docker container:
docker run -p 8000:8000 my-nginx-app
The docker run
command starts a Docker container from the specified image (my-nginx-app
in this case) and maps port 8000 on the host system to port 8000 on the container. The -p
option specifies the port mapping.
Wait for the Docker container to start. This may take a few seconds.
Open a web browser and navigate to
http://localhost:8000
. This should display the Nginx welcome page.To stop the Docker container, press
Ctrl+C
in the terminal window.
That's it! You have now run the Docker container for the Nginx project on your Ubuntu system and mapped port 8000 to the container's port 8000. You can now access the Nginx web server in your web browser by navigating to http://localhost:8000
.
VI. Configuring NGINX
Backing up NGINX's configuration files:
To ensure that we have a safety net in case we encounter any issues, we should first back up the configurations of nginx before proceeding. This way, we can easily revert to a previous state without having to reconfigure everything from scratch.
To back up NGINX's configuration files on Ubuntu, follow these steps:
Open a terminal window on your Ubuntu system.
Navigate to the directory where NGINX's configuration files are stored. This is usually
/etc/nginx/
.
cd /etc/nginx/
- Create a backup directory to store the configuration files:
sudo mkdir /etc/nginx/backup
- Copy the configuration files to the backup directory:
sudo cp -R * /etc/nginx/backup/
This will copy all of the configuration files in the current directory to the backup directory. The -R
option tells the cp
command to copy directories recursively.
- Verify that the configuration files have been backed up by checking the contents of the backup directory:
ls /etc/nginx/backup
This will display a list of the files and directories in the backup directory. The configuration files should be listed among them.
That's it! You have now backed up NGINX's configuration files on your Ubuntu system. You can restore these files if necessary by copying them back to the original directory.
Adding a proxy pass configuration for the Docker container:
- To add a proxy pass configuration for your Docker container, navigate to the sites-enabled path on Ubuntu:
cd /etc/nginx/sites-enabled/
- Use the following command to open the default file in Vim:
sudo vim default
- Find the
location /
block and inside the curly braces, add the following line to create a proxy pass to your Docker container:
proxy_pass http://127.0.0.1:8000;
Adding another location block for proxying API requests to the container:
Adding another location
block for proxy the API requests to the container:
location /api {
proxy_pass http://127.0.0.1:8000/api;
}
Copying the static files to the /var/www/html/ directory:
- After saving above changes in default file now navigate to the /var/www/html/ location and do below commands:
mkdir static
- Copying the static folder files to /var/www/html/static/
cp -r /home/<your-user>/Nginx-project/mynotes/build/static/* /var/www/html/static/
- Once done restart the nginx service
systemctl restart nginx
- Now navingate to your browser and go to the url
http://<public-ip>/
Boom ๐ฅ๐ฅ๐ฅ you can see your notes app
VII. Conclusion
Recap of the steps to install and configure NGINX and Docker for hosting a notes app on Ubuntu:
Here's a recap of the steps to install and configure NGINX and Docker for hosting a notes app on Ubuntu:
Install NGINX using
apt-get
.Configure NGINX by editing its configuration files. This includes setting up virtual servers and configuring SSL if needed.
Install Docker using
apt-get
.Create a Docker container for the notes app by writing a Dockerfile and building the container image.
Run the Docker container and map a port on the host system to the container's port.
Add a proxy pass configuration to NGINX to forward incoming requests to the Docker container.
Test the setup by accessing the notes app from a web browser.
Back up NGINX's configuration files in case of future issues.
By following these steps, you can set up a robust and scalable hosting environment for your notes app on Ubuntu.