OpenBSD Web Server - Setup NginX Port HTTP and HTTPS

Talking about web servers, what comes to mind most of us is Apache. OK, that's right, because Apache is one of the most popular web servers in the world and is widely used by many websites in cyberspace. However, this time I will not discuss the Apache web server, this time I will discuss one of the web servers that is currently on the rise and starting to become popular, namely Nginx.

Nginx is an open source web server like Apache, but is very light and faster in processing. Since its release, Nginx only has a function as HTTP or as web serving only. In contrast to Apache which has complete features with various modules embedded in it, it actually makes it heavier.

Currently, Nginx is popular and is equipped with various advanced features, so that it will make your website look more attractive and of course powerful.

Nginx also has lots of features and can be used as a variety of servers, such as:
  1. Reverse proxy server for HTTP, HTTPS, SMTP, POP3, and IMAP protocols.
  2. Load balancer and HTTP cache.
  3. Frontend proxy for Apache and other web servers, combining the flexibility of Apache with the great static content performance of Nginx.



1. Install Nginx Web Server

This is of course, not the only tutorial that explains how to install Nginx. However, we have made the contents of this article simple to make it easier for readers to understand the contents of the article. To install Nginx on OpenBSD you can do it in two ways, namely pkg_add and ports. So that all libraries can be installed completely, we will use the ports system to install Nginx. Run the command below to start the Nginx installation process.

First we check the version of Nginx available in the OpenBSD repository.

Check Repository Nginx
foo# pkg_info -Q nginx
nginx-1.24.0p0
After knowing the version of Nginx in the OpenBSD repository, we start installing Nginx.

Install Nginx with ports
foo# cd /usr/ports/www/nginx
foo# make build
foo# make install
foo# make clean
Or, if you want to install Nginx with pkg_add, run the command below.

Install Nginx with pkg_add
foo# pkg_add nginx-1.24.0p0
After that, you start and enable Nginx to start at boot time in OpenBSD.

Enbale Nginx
foo# rcctl enable nginx
foo# rcctl restart nginx


2. Setup Nginx Port 80 HTTP

On OpenBSD, the main Nginx configuration files are located in the /etc/nginx directory. In that directory there is a file called nginx.conf. This file is a file that manages all Nginx behavior on the OpenBSD server.

Before continuing this discussion, make sure Nginx is running normally on OpenBSD. To configure Nginx port 80 or http, we will create a virtual host for port 80. To do this, open the nginx.conf file then delete all the contents of the script and replace it with the script below.

/etc/nginx
foo# cd /etc/nginx
foo# nano nginx.conf
user  www;
worker_processes  1;

error_log  logs/error.log;
pid        logs/nginx.pid;
worker_rlimit_nofile 1024;

events {
    worker_connections  800;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    index         index.html index.htm;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;
    keepalive_timeout  65;
    server_tokens off;
    
include /etc/nginx/vhosts.conf;
}
After that, you create the vhosts.conf file.

Create vhosts.conf
foo# touch /etc/nginx/vhosts.conf
In the vhosts.conf file, type the script below to run Nginx port 80.

Create vhosts.conf
foo# nano /etc/nginx/vhosts.conf
server {
        listen       80;
        listen       [::]:80;
        server_name  datainchi.com;
        root         /var/www/htdocs/serverblock;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root  /var/www/htdocs/serverblock;
        }
    }
Create a directory for the virtual host, run the command below to create a directory /var/www/htdocs/serverblock.

Create directory virtual host
foo# mkdir -p /var/www/htdocs/serverblock
Continue by creating the index.html file, and run the command to create ownership and permissions

Create index.html
foo# touch /var/www/htdocs/serverblock/index.html
foo# chown -R www:www /var/www/htdocs/serverblock/
foo# chmod -R 775 /var/www/htdocs/serverblock
In the index.html file, type the script as in the example below.

Add a script in the index.html file
foo# nano /var/www/htdocs/serverblock/index.html
<html>
<head>
<title>Nginx port 80</title>
</head>
<body>
<p align="center">Welcome to Nginx Virtual Host port HTTP</p>
</body>
</html>
Jalankan Nginx dengan perintah rcctl.

Run Nginx
foo# rcctl restart nginx
nginx(ok)
nginx(ok)
To check whether the NGinx server is running normally or there are still script errors, run the curl command, as in the example below.

Check Nginx
foo# curl 192.168.5.3:80
<html>
<head>
<title>Nginx port 80</title>
</head>
<body>
<p align="center">Welcome to Nginx Virtual Host port HTTP</p>
</body>
</html>


3. Setup Nginx Port 443 HTTPS

Once you understand, how to run Nginx on port 80 or HTTP. We continue by running Nginx on port 443 or HTTPS. On port 443, NGinx requires SSL certificate encryption. Creation of an SSL certificate, as the first step in starting the Nginx HTTPS configuration.

a. Create certificate SSL

We use OpenSSL to create SSL certificates for Nginx which is free. With OpenSSL you can easily create an SSL certificate. The following is an example of creating an SSL certificate with OpenSSL.

Create directory SSL
foo# cd /etc/nginx
foo# mkdir -p SSL
foo# cd SSL
After you create the SSL directory, continue by running OpenSSL to start creating the SSL certificate.

Create certificate SSL (/etc/nginx/SSL)
foo# openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/nginx/SSL/nginxssl.key -out /etc/nginx/SSL/nginxssl.crt

b. Create server root HTTPS

Once you have an SSL certificate, the next step is to create a virtual host directory for Nginx HTTPS and provide the ownership and permissions commands. Notice the example below.

/var/www/htdocs/nginxssl
foo# mkdir -p /var/www/htdocs/nginxssl
foo# chown -R www:www /var/www/htdocs/nginxssl/
foo# chmod -R 775 /var/www/htdocs/nginxssl/
Create an index.html file that we will use to test Nginx HTTPS. In the index.html file, type the script below.

Create index.html file
foo# touch /var/www/htdocs/nginxssl/index.html
foo# nano /var/www/htdocs/nginxssl/index.html
<html>
<head>
<title>Nginx port 443</title>
</head>
<body>
<p align="center">Welcome to Nginx Virtual Host port 443 HTTPS</p>
</body>
</html>

c. Setup Nginx HTTPS

To run Nginx port 443, you must create a conf file for the HTTPS virtual host. Open the nginx.conf file, and add the script below.

Add the script in the nginx.conf file
foo# nano /etc/nginx/nginx.conf
include /etc/nginx/vhostsSSL.conf;
After that, create a vhostsSSL.conf file, and type the script below in the vhostsSSL.conf file.

Create vhostsSSL.conf file and add script
foo# touch /etc/nginx/vhostsSSL.conf
foo# nano /etc/nginx/vhostsSSL.conf
server {
        listen       443;
        listen       [::]:443;
        server_name  datainchi.com;
        root         /var/www/htdocs/nginxssl;
ssl on;
ssl_certificate /etc/nginx/SSL/nginxssl.crt;
ssl_certificate_key /etc/nginx/SSL/nginxssl.key;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root  /var/www/htdocs/nginxssl;
        }
    }
The next step is to run Nginx so it can run port 443 HTTPS.

Run Nginx
foo# rcctl restart nginx
nginx(ok)
nginx(ok)
The final step, check the Nginx server. In this test we use the curl command. Run the curl command as in the example below.

Test Nginx port 443 HTTPS
foo# curl -k https://192.168.5.3:443
<html>
<head>
<title>Nginx port 443</title>
</head>
<body>
<p align="center">Welcome to Nginx Virtual Host port 443 HTTPS</p>
</body>
</html>
If it looks like the script above, it means that all the configurations you have done are not wrong. Currently you have an Nginx server running on port 80 and port 443. We recommend that for every Nginx server activity, you should use port 443, to protect your important data.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post