Configuring A Ubuntu Server For A React Application Using Docker and Nginx
Configuring A Ubuntu Server For A React Application Using Docker and Nginx
Configuring a Ubuntu Server for a React Application using Docker and Nginx 1
Configure Nginx
Docker Deployment Method
Direct Deployment Method
Test and Reload Nginx
Verify Deployment
Automate Certificate Renewal
Conclusion
Key Takeaways
Introduction
Deploying a web application requires careful planning and configuration to ensure optimal
performance, security, and scalability. This guide will walk you through the process of setting
up and deploying a robust, secure React application on a DigitalOcean droplet using Nginx,
Docker, and Certbot.
We will leverage an Ubuntu server environment to configure a full-stack deployment,
focusing on two methods:
1. Using Docker to encapsulate the React application, providing a consistent and portable
deployment across different environments.
2. Hosting the React application directly from a built folder using Nginx to serve static
content.
Deploy SSL certificates with Certbot to ensure secure HTTPS connections for your
domain.
Set up Nginx as a reverse proxy to serve your application and distribute content
efficiently with CDN integration.
By the end of this guide, you’ll have a robust deployment strategy for your React application,
ensuring it is secure and scalable, ready to meet varying traffic demands.
Configuring a Ubuntu Server for a React Application using Docker and Nginx 2
💡 Before you begin, make sure you have prepared the following:
After installation, the SSH service should start automatically. You can check the status of the
service with the following command:
Configuring a Ubuntu Server for a React Application using Docker and Nginx 3
sudo systemctl enable ssh
Now you have OpenSSH Server installed and running on your Ubuntu server. You can now
proceed to the recommended security configurations.
Follow the on-screen instructions to name your key and optionally create a passphrase. This
will generate a private key (default ~/.ssh/id_rsa) and a public key (default ~/.ssh/id_rsa.pub).
Copy the public key to the Ubuntu server: You can copy the public key using the ssh-copy-id
tool from your local machine. Be sure to replace user with your actual username on the
Ubuntu server and server_ip with your server's IP address.
ssh-copy-id user@server_ip
If you cannot use ssh-copy-id, you can manually copy the contents of your public key into the
~/.ssh/authorized_keys file on the server.
Change or add the following lines to disable password authentication and enable key-based
authentication:
PasswordAuthentication no
PubkeyAuthentication yes
ChallengeResponseAuthentication no
PermitRootLogin no
Configuring a Ubuntu Server for a React Application using Docker and Nginx 4
Check the SSH server logs (optional).
If you're having trouble connecting, you may want to check the SSH server logs to identify
the cause of the failure. Log in to your server (with another authentication method, if
possible) and run the following command to see the latest messages from the SSH server:
Verify that you can still connect from your local machine using your private key. Once you
are able to log in and confirm this, SSH key-based authentication is correctly set up.
User Configuration
Granting Superuser (root) Privileges
To avoid logging in as root directly, create a new user with sudo privileges.
Log in as root:
ssh root@your_server_ip
adduser "username"
Add the new user to the sudo group to grant administrative rights:
Configuring a Ubuntu Server for a React Application using Docker and Nginx 5
# Switch to the new user to log in
su - "username"
If you encounter any error messages or permission problems, it means that the sudo privileges
have not been correctly configured for this user. In that case, you'll need to revisit the
previous steps to make sure the user has been properly added to the sudo group.
To log in as the new user with ssh, use the following command:
ssh "username"@your_server_ip
To manage web content, you can create a group called webmasters and then allocate the
appropriate permissions to directories that the group should manage.
bash
sudo groupadd webmasters
Add your new user to the webmasters group so they can manage web server files:
bash
sudo usermod -aG webmasters "username"
Adjust the directory privileges to allow the webmasters group to read, write, and execute files
as necessary. This is typically done using chown and chmod commands on the web server
document root (e.g., /var/www/html ):
bash
# Change the ownership of the web directory
sudo chown -R "username":webmasters /var/www/html
Configuring a Ubuntu Server for a React Application using Docker and Nginx 6
# Set the correct permissions for the group
sudo chmod -R 775 /var/www/html
This will set the owner of the files to your new user, and the group ownership to the newly
created webmasters group, ensuring that both the user and other members of the group can
modify the web server's files as needed.
Security is crucial, especially when managing a web server. The 775 permission set literally
translates to full read, write, and execute permissions for the user and group, and read and
execute permissions for others. It's often suitable for a web directory. However, depending on
your specific security requirements, you might need to make the permissions more restrictive.
For files that should only be readable and writable by the user and the group, use:
bash
sudo chmod -R 664 /var/www/html
For directories that should only be accessible (read and executed) by the user and the group,
use:
bash
sudo find /var/www/html -type d -exec chmod 775 {} \;
After this, the web server directories will be owned by the "username" with group
"webmasters", and they will have appropriate permissions set for web content management.
Conclusion:
You have now successfully created a new user with sudo privileges, set up
the webmasters group, assigned the appropriate group to your user, and set the necessary file
permissions for managing web content. This basic organizational structure enhances the
Configuring a Ubuntu Server for a React Application using Docker and Nginx 7
security and manageability of your server environment, laying a solid foundation for
deploying applications, like a React app, on your DigitalOcean droplet.
Htop is an interactive process viewer for Unix systems, offering a more convenient interface
for monitoring system resources:
Install additional network tools to support legacy commands and network troubleshooting.
Net-tools provides essential networking utilities, including ifconfig . Install it if commands
like ifconfig are not found by default:
To install Git and verify its successful installation, follow these steps:
This command will display the installed version of Git, confirming that the program is
correctly set up on your system.
Configuring a Ubuntu Server for a React Application using Docker and Nginx 8
In this section, we'll install Node Version Manager (NVM), which allows you to manage
multiple versions of Node.js on a single machine. This is particularly useful for ensuring that
development environments remain consistent with production environments.
To install NVM, retrieve the installation script from the official NVM repository. Replace the
version in the URL with the latest available version, which you can check in the official
repository: NVM GitHub.
Once the script has been executed, close and reopen your terminal to ensure that the changes
take effect and NVM is available in your terminal session. This step ensures that the new
paths and environment variables added by NVM are recognized by your shell configuration
files.
Verify that NVM is installed correctly by running:
nvm --version
With NVM installed, you can now install the specific version of Node.js required by your
React project. If you need Node.js version 14.18.3:
After the installation, you can verify that the correct version of Node.js is installed with the
command:
You now have a configured Node.js environment ready for your full-stack project on your
Ubuntu server.
Installing Nginx
Nginx is a high-performance web server that can also be used as a reverse proxy, load
balancer, and HTTP cache. Installing and configuring Nginx is an important step for serving
Configuring a Ubuntu Server for a React Application using Docker and Nginx 9
the React application and FastAPI service. To install Nginx on Ubuntu, follow these steps:
Install Nginx using the APT package manager:
To ensure Nginx starts automatically at server boot, enable it with the following command:
To determine your server's public IP address and verify that Nginx is correctly serving the
default welcome page, proceed as follows:
Use an external service like icanhazip.com to find your server’s public IP address:
curl -4 icanhazip.com
The command above will display your server's public IP address. Open a web browser and
enter your server's public IP address. You should see the default Nginx welcome page,
confirming that Nginx is installed and serving content correctly.
Alternatively, if your DNS is properly configured to resolve your domain to your server's IP
address, you can also navigate to http://your_server_ip from any web browser. This should
display Nginx's default landing page. Replace "your_server_ip" with your server's actual IP
address.
Firewall Configuration
Use UFW (Uncomplicated Firewall) to establish a basic firewall configuration that allows only
essential ports (SSH, HTTP, HTTPS).
# Install UFW
sudo apt install ufw
Configuring a Ubuntu Server for a React Application using Docker and Nginx 10
ufw app list
# Enable UFW, which will start enforcing the rules you have set
sudo ufw enable
# Check the current status of UFW in detail, including default policies and rules for each port
sudo ufw status verbose
This command removes all Docker versions that might be installed from Ubuntu's
standard package repositories or installed via manual methods.
Make sure to have ca-certificates installed and then configure the Docker repository:
Configuring a Ubuntu Server for a React Application using Docker and Nginx 11
sudo curl -fsSL <https://download.docker.com/linux/ubuntu/gpg> -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \\
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] <https://downlo
ad.docker.com/linux/ubuntu> \\
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \\
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
After running these commands, Docker should be installed, the Docker daemon should
start, and the process should be configured to start at system launch. Verify Docker is
running with:
docker --version
Configuring a Ubuntu Server for a React Application using Docker and Nginx 12
Using Docker
With Docker operational, you can begin using it to manage containers, images, and volumes.
A few basic Docker commands include:
docker images -a
docker ps -a
List Volumes:
docker volume ls
System Update and Firewall Configuration: System packages have been updated, and
the firewall has been configured to permit only essential ports.
User Configuration: A user with sudo privileges has been created for secure server
management.
Essential Utilities: Tools like curl , htop , and potentially net-tools have been
installed to facilitate routine operations and network diagnostics.
Git: The distributed version control system has been set up to manage your code
repositories.
Node.js Environment with NVM: Node Version Manager (NVM) has been installed for
managing different Node.js versions, with the specific version required for the React
project.
Configuring a Ubuntu Server for a React Application using Docker and Nginx 13
Nginx: The web server and reverse proxy have been installed, configured to start on
system boot, and tested to confirm it is serving the default welcome page.
Docker: The containerization platform has been installed and configured to run
applications in isolated containers.
With these components in place, you have a solid foundation for deploying your full-stack
application. The next steps include deploying your PostgreSQL database, configuring Nginx
to serve the React application and proxy the FastAPI backend, and containerizing the
application using Docker for reliable and manageable deployment.
Follow the prompts to complete the SSL certificate setup, which will automatically modify
your Nginx configuration to support HTTPS.
Configure Nginx
Configuring a Ubuntu Server for a React Application using Docker and Nginx 14
Create or modify the Nginx configuration file for getechoxen.com . You already have a
configuration, so ensure it aligns with your application setup.
server {
server_name getechoxen.com www.getechoxen.com;
location / {
try_files $uri $uri/ /index.html;
}
# Video CDN
location /cdn/videos/ {
alias /var/www/videos/;
autoindex on;
}
server {
if ($host = www.getechoxen.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = getechoxen.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name getechoxen.com www.getechoxen.com;
return 404; # managed by Certbot
}
cd /path/to/docker/project
Configuring a Ubuntu Server for a React Application using Docker and Nginx 15
Build and run the production container:
When deploying the React app using Docker, you'll want to configure Nginx as a reverse
proxy to your Docker container instead of directly serving files from the filesystem. Below is
the updated Nginx configuration to achieve that:
server {
server_name getechoxen.com www.getechoxen.com;
# Video CDN
location /cdn/videos/ {
alias /var/www/videos/;
autoindex on;
}
server {
if ($host = www.getechoxen.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = getechoxen.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name getechoxen.com www.getechoxen.com;
return 404; # managed by Certbot
}
In this configuration:
The proxy_pass directive forwards any requests to http://localhost:8081 , which is the port
your Docker container listens on. Ensure your docker-compose.yml correctly maps the
container's port 80 to the host's port 8081 .
Configuring a Ubuntu Server for a React Application using Docker and Nginx 16
This configuration keeps the existing setup for your video CDN and SSL handling managed
by Certbot.
sudo nginx -t
Verify Deployment
Visit https://getechoxen.com/ in a web browser to ensure the site is served over HTTPS.
Check links to ensure the CDN is functioning by navigating to
https://getechoxen.com/cdn/videos/ .
Configuring a Ubuntu Server for a React Application using Docker and Nginx 17
Certbot's installation typically includes a cron job or systemd timer for automatic renewal.
Verify or test automatic renewal with:
This completes the setup for deploying your React app to getechoxen.com with options for
Docker-based or file-based deployment using Nginx and Certbot. Make sure to adapt file
paths and Docker configurations to your specific project structure.
Conclusion
In this guide, we've detailed how to deploy your website, getechoxen.com , using Nginx,
Certbot for SSL/TLS, and Docker to manage your React application. By setting up a flexible
deployment strategy, you can serve your application either through a Docker container or
directly from the file system, thus allowing for optimal adaptability depending on your
development and production needs.
Key Takeaways
Security and Scalability: Implementing HTTPS using Certbot ensures that your website
is secure and trustworthy for users. This step is crucial in protecting user data and
enhancing SEO.
Robust Server Management: Utilizing tools like Nginx for reverse proxying ensures
your server can handle multiple tasks, like directing traffic to various services and
serving static files, all while maintaining efficiency and performance.
With these configurations and tools in place, getechoxen.com is well-prepared to handle web
traffic efficiently and securely. By following this setup, you can ensure smooth site
deployments and efficient server operations, making future expansions or adjustments
straightforward.
Configuring a Ubuntu Server for a React Application using Docker and Nginx 18
Configuring a Ubuntu Server for a React Application using Docker and Nginx 19