NGINX is a powerful and widely used web server that excels in delivering high-performance web content. In this article, we will guide you through the process of setting up NGINX on an Ubuntu server and activating a domain on the NGINX server. We will cover the installation, configuration, and management of NGINX, along with the necessary steps to enable your domain on the server. Additionally, we will explore the commands required to start, restart, and check the status of NGINX.
Installation of NGINX on Ubuntu
NGINX is a popular web server known for its high performance, scalability, and flexibility. In this guide, we will walk you through the process of installing NGINX on a server using Ubuntu as the operating system. The following steps explain how to install NGINX using commands.
Step 1: Update the System.
Before installing any software, it is essential to update the system to ensure that you have the latest packages and security patches. Open the terminal on your server and run the following command:
sudo apt update && sudo apt upgrade
Step 2: Install NGINX
To install NGINX on Ubuntu, use the apt package manager. Run the following command in the terminal:
sudo apt install nginx
This command will install NGINX and its dependencies. During the installation, you might be prompted to confirm the installation by pressing ‘Y’ and then press Enter.
Step 3: Start NGINX
Once NGINX is installed, it will automatically start running. However, if it is not started for any reason, you can manually start it using the following command:
sudo systemctl start nginx
To check if NGINX is running, you can use the following command:
sudo systemctl status nginx
If NGINX is active and running, you will see an “active (running)” status in the output.
Step 4: Testing NGINX
To verify that NGINX is correctly installed and running, open a web browser and enter your server’s IP address or domain name. You should see the default NGINX welcome page if everything is set up correctly.
Step 5: Adjusting Firewall Settings
By default, NGINX listens on port 80 for HTTP traffic. Ensure that your server’s firewall allows inbound traffic on this port. If you are using UFW, a simple command to allow HTTP traffic is:
sudo ufw allow 'Nginx HTTP'
Step 6: Additional Configuration
NGINX’s main configuration file is at /etc/nginx/nginx.conf. You can modify this file to customize NGINX’s behavior according to your needs. However, be cautious when making changes to avoid misconfigurations that could affect NGINX’s performance or security.
Hosting a Website on NGINX Server
Step 1: Create a Website Directory
First, create a directory where your website files will reside. You can choose any location, but a common directory is /var/www/. Use the following command to create the directory:
sudo mkdir -p /var/www/example.com
Replace “example.com” with your actual domain or website name.
Step 2: Configure NGINX
Next, configure NGINX to host your website. Open the NGINX configuration file using a text editor:
sudo nano /etc/nginx/sites-available/example.com
Replace “example.com” with your domain or website name.
Inside the file, add the following configuration:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
NOTE: If you want to set up react js web app you should configure nginx to serve files from the build directory of react js web app. See the below example.
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/build;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Save the file and exit the text editor.
Step 3: Enable the Website
Create a symbolic link from the configuration file in the sites-available directory to the sites-enabled directory to enable your website. Use the following command:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
Step 4: Test Configuration and Restart NGINX
Before restarting NGINX, it’s essential to test the configuration to ensure there are no syntax errors. Run the following command:
sudo nginx -t
If the test is successful, you should see “syntax is okay” in the output. If there are any errors, review your configuration file for typos or mistakes.
Finally, restart NGINX to apply the changes:
sudo systemctl restart nginx
Step 5: Upload Website Files
Upload your website files to the directory you created earlier (/var/www/example.com). You can use FTP, SCP, or any other file transfer method to copy your website files to the server.
Step 6: Access Your Website
Open a web browser and enter your domain or website name (e.g., http://example.com). You should see your website’s content if everything is set up correctly.
1 thought on “Setting Up NGINX on Ubuntu Server and Activating a Domain on NGINX Server”