Installing Nginx on Ubuntu Server Without 403 Forbidden Nginx Error
Introduction:
Unlocking the Power And Guide to Setting Up Nginx Load Balancing
A popular choice for hosting the largest and busiest websites online. Prominent for its lightweight nature and efficacy as a reverse proxy, Nginx is a go-to solution for handling heavy traffic loads.
This comprehensive guide walks you through configuring Nginx, setting it up on your local machine, handling potential issues like "403 Forbidden," and even delving into advanced topics like load balancing.
Prerequisites:
Before diving in, ensure you have sudo privileges on your server.
Step 1: Conection
1: Connect your server - - if you don't know ssh linux ubuntu server click here
ssh user@ip
Example:
ssh [email protected]
2. Update Local Packages:
sudo apt-get update
3: How to Install.
sudo apt install nginx -y
After this, apt will install it with other required dependencies on your local server.
Step 2 – Firewall Configuration
Before proceeding, configure your firewall to allow access.
You just type the following command to see this list of user firewalls.
ufw app list
You will see this output.
Available applications:
OpenSSH Ngin* Full Nginx HTTP Nginx HTTPS
This list shows three Nginx profiles available.
Full: Profile allow port 80 and port 443
HTTP: Profile allows only port 80 for normal, unencrypted web traffic
HTTPS: Profile allows only port 443 for TLS/SSL encrypted web traffic.
Now It's required to enable these firewall configurations. We haven't installed nginx SSL yet so you only need to allow traffic on port 80 for HTTP connection.
You can allow firewall rules by following these commands.
sudo ufw allow 'Nginx HTTP'
Please verify your changes:
sudo ufw status
You will receive the list of allowed output.
Step 3 – Checking your Web Server
Next in the installation process. We need to check and make sure the service is running perfectly or not.
sudo service nginx status
Output:
nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/ng!nx.service; enabled; vendor preset: enabled) Active: active (running) since Sun 202*-04-07 06:10:52 UCT; 1s ago Docs: man:ng!nx(8) Process: 9109 ExecStartPre=/usr/sbin/ng!nx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 9110 ExecStart=/usr/sbin/ngin* -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 9111 (ngin*) Tasks: 5 (limit: 9267) Memory: 5.8M CPU: 70ms 06:10:52 ops systemd[1]: Starting A high performancereverse proxy... 07ops systemd[1]: Started A high performance reverse proxy.
2: Update local packages first.
It shows whether the service is running perfectly or not. Now you can see the running status and now we can go further on the next step.
Now you need to execute the following command to see your public IP address.
curl -4 icanhazip.com
Open your browser paste this IP into it and press enter. If you also see this image.
Congratulations you successfully installed the world's best web server.
Step 4 – Managing the Nginx Process
Your web server is up & running.
How to manage.
You can stop anytime by using this command:
sudo service nginx stop
Starting command:
sudo service nginx start
Restart command:
sudo service nginx restart
If you just make some configuration, you can just reload Nginx without affecting current connections.
Reload web server, to follow this command:
sudo service nginx reload
Disable services, to follow this command:
sudo service nginx disable
Enable services, to follow this command:
sudo service nginx enable
Server Blocks
Step 5 – How to deploy multiple websites on a single server.
Introduction of Nginx Web Server Blocks:
Server blocks, also known as virtual hosts. It allows you to deploy multiple web apps on a single node/server. Each server block defines a separate configuration for a specific domain or subdomain.
Prerequisites
Before setting up Nginx Web server blocks, ensure that:
- Your domain names are correctly configured to point to your server's IP address.
Note:
- if you don't know how to do DNS settings click here.
Creating Configuration Files
1. Go to your Nginx Configuration Directory:
cd /etc/nginx/sites-available/
2. Create Configuration Files:
Please create separate configuration files for each server block corresponding to your domain names.
nano you-domain-name.com >>> for main domain
For subdomain:
nano you-domain-name.com >>> for main sub-domain
Configuring Server Blocks
3. Define Server Block Configuration.
You can copy this configuration file and make some changes like domain name and project root directory path.
server {
listen 80;
server_name Domian.com;
#please replace your project directory path here
root /var/www/html/test/build;
index index.php index.html index.htm;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
# if you index file extension is html then keep otherwise please change this
index index.html;
charset utf-8;
location / {
try_files $uri $uri/ /index.html?$query_string; #please change here also
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
We need to link this file from sites-available to sites-enabled directory.
ln -s /etc/nginx/sites-available/you-domain-name.com /etc/nginx/sites-enabled/
For Subdomain
ln -s /etc/nginx/sites-available/you-sub-domain-name.com /etc/nginx/sites-enabled/
Testing and Reloading Nginx
5. Test Configuration:
Test the Nginx configuration for syntax errors:
nginx -t
6. Restart Nginx.
Once the configuration test passes, reload Nginx to apply the changes:
You can see nginx logs in
cd /var/log/nginx/error.log cd /var/log/nginx/access.log
How to restart
sudo service nginx restart
0 Comments