How to use Nginx on the Raspberry Pi to build a local site and achieve remote access through intranet penetration

Installing Nginx (pronounced "engine-x") turns your Raspberry Pi into a powerful web server that can be used to host websites or web applications. Compared with other web servers, Nginx has a very low memory footprint and can run on resource-constrained devices such as Raspberry Pi. At the same time, combined with the cpolar intranet penetration tool, remote access without public network IP can be realized.

1. Nginx installation

It is also very simple to install Nginx on the Raspberry Pi. You can install it directly through the apt command, and update the warehouse first.

sudo apt update

install updates

sudo apt upgrade

Install the Nginx package after the update is complete

sudo apt install nginx

Start Nginx

sudo /ect/init.d/nginx start

After starting, we can enter the Raspberry Pi port in the external browser 局域网ip:80to access Nginx

image-20230607131709841

2. Install cpolar

Use the cpolar one-piece installation script:

curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash

Add a service to the system

sudo systemctl enable cpolar

Start the cpolar service

sudo systemctl start cpolar

View service status

sudo systemctl status cpolar

Restart the cpolar command (use when needed)

sudo systemctl restart cpolar

Then use the Raspberry Pi LAN ip+:9200 port in an external browser to display the cpolar management interface

image-20230607133940058

Enter the account registered on the official website to log in to operate the tunnel.

image-20230303184844558

3. Configure domain name to access Nginx

We create a random 80 tunnel in the cpolar management interface:

  • Tunnel name: customizable, be careful not to repeat
  • protocol: http
  • Local address: 80
  • Port type: random domain name
  • Region: China

click create

image-20230607134925374

Then open the online tunnel list and view the public network address for creating the tunnel. There are two access methods by default, one is http and the other is https, copy the http address

image-20230607134956379

Open the browser, use the above http public network address to access, you can see the nginx welcome interface, so that the remote access is set up

image-20230607135233922

4. Fixed domain name access

It should be noted that the above steps use a random temporary address, and the public network address will change randomly within 24 hours. Next, we configure a fixed address for it, which will not change, and there is no need to modify the address every day after setting.

You need to upgrade to the basic package or above to support the configuration of fixed domain names

Log in to the background of the cpolar official website , click on the dashboard on the left 预留, find 保留二级子域名, and reserve a second-level subdomain name for the http tunnel.

  • Region: Select the server region
  • Name: Fill in the second-level subdomain name you want to keep (customizable)
  • Description: Remarks, which can be customized

image-20230607140809246

This example reserves a nginxtestsecond-level subdomain named . After the subdomain name is successfully reserved, we copy the subdomain name, and then we need to bind its configuration to the tunnel.

image-20230607140853198

Log in to the cpolar web ui management interface. 隧道管理Click —— on the left dashboard 隧道列表, find the tunnel that needs to configure the second-level subdomain name, and click on the right编辑

image-20230607140924625

Modify the tunnel information and configure the second-level subdomain name into the tunnel:

  • Domain Type: Select instead二级子域名
  • Sub Domain: Fill in the second-level subdomain name we just reserved (in this example nginxtest)

After the modification is complete, click更新

image-20230607141008024

状态After the tunnel is successfully updated, click —— on the left dashboard 在线隧道列表, and you can see the public network address of the nginx tunnel, which has been updated to a second-level subdomain name, and copy the public network address.

image-20230607141038162

Open the browser, let's test the access to the successfully configured second-level subdomain name, and the nginx welcome interface appears, indicating success and normal access. Now, our only private second-level subdomain name in the entire network has been created.

image-20230607141122878

5. Configure a static site

Enter the nginx resource directory, which is the resource directory that nginx looks for by default

cd /var/www/html

In order to make the content of the site more meaningful, we download and deploy a simple static demo site – meditation and relaxation site.

sudo wget https://www.cpolar.com/static/downloads/meditation-app-master.tar.gz

decompress

sudo tar xzf meditation-app-master.tar.gz

Then open the browser and enter the public network address we configured above plus the resource path: /meditation-app-master, you can see the site we configured, and remote access is also available.

image-20230607151025731

Guess you like

Origin blog.csdn.net/weixin_67596609/article/details/132537932