Guide to Setting Up NGINX and Adding a New Domain
NGINX is a powerful, high-performance web server that’s known for its stability, rich feature set, and low resource consumption. In this guide, I’ll walk you through the process of setting up NGINX and adding a new domain to your server.
Step 1: Install NGINX
Start by installing NGINX. The installation process varies depending on your operating system.
For Ubuntu/Debian systems:
sudo apt update
sudo apt install nginx
For CentOS/RHEL systems:
sudo yum install epel-release
sudo yum install nginx
After installation, you can start NGINX with:
sudo systemctl start nginx
Enable NGINX to start on boot with:
sudo systemctl enable nginx
Step 2: Configure NGINX for Your Domain
NGINX configuration files are located in the /etc/nginx
directory. The main configuration file is /etc/nginx/nginx.conf
, but for domain-specific configurations, you'll create a new file in the /etc/nginx/sites-available
directory and then create a symbolic link to it in the /etc/nginx/sites-enabled
directory.
- Create a Configuration File for Your Domain:
Create a new configuration file in /etc/nginx/sites-available
:
sudo nano /etc/nginx/sites-available/yourdomain.com
- Edit the Configuration File:
Paste the following configuration, adjusting server_name
, root
, and other directives as necessary:
nginxCopy code
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
This configuration listens for connections on port 80 for yourdomain.com
and delivers content from /var/www/yourdomain.com/html
.
- Create the Document Root Directory:
sudo mkdir -p /var/www/yourdomain.com/html
- Set Permissions:
Set the correct permissions for the document root directory:
sudo chown -R $USER:$USER /var/www/yourdomain.com/html
sudo chmod -R 755 /var/www
- Create a Sample
index.html
Page (Optional):
echo "Hello from NGINX on yourdomain.com!" | sudo tee /var/www/yourdomain.com/html/index.html
- Enable the Configuration:
Create a symbolic link to your domain’s configuration file in the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
Step 3: Test and Restart NGINX
Test your configuration for syntax errors:
sudo nginx -t
If you see “syntax is okay” and “test is successful”, restart NGINX to apply the changes:
sudo systemctl restart nginx
Step 4: Update DNS Settings
Update your domain’s DNS settings to point to your server’s IP address. This step varies depending on your domain registrar.
- Log in to your domain registrar’s dashboard.
- Navigate to DNS settings for your domain.
- Create or update A records for
yourdomain.com
andwww.yourdomain.com
to point to your server's IP address.
Congratulations! You’ve successfully set up NGINX and added a new domain. Your website should now be accessible via yourdomain.com
. This basic configuration is the starting point for hosting websites and web applications with NGINX. As you grow, you can explore additional NGINX features and modules to further optimize and secure your web server.