Technology Sharing

Ubuntu practical sequel: Apache httpd easily builds an efficient proxy server

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Ubuntu practical sequel: Apache httpd easily builds an efficient proxy server

Author: Gao Yuhan
Time: 2024.7.11 21:06
Blog: blog.csdn.net/cg_i
Environment: Ubuntu 22.04.4 LTS, Apache/2.4.52 (Ubuntu)

Machines are more capable than humans, and most people will end up doing nothing.

I. Introduction

This article is ["Apache httpd on Windows 11: Easily build an efficient proxy server"](Apache httpd practice on Windows 11: easily build an efficient proxy server - CSDN blog). In the previous chapter, I detailed how toWindows 11Utilization on the systemApache httpdBuild an efficient and stable proxy server. However, due toWindowsandUbuntuThere are significant differences in the configuration and management of the two operating systems. Many people who are used to usingUbuntuUsers of the system may encounter many challenges. To meet the needs of these users, we have specially launched this practical guide, which aims to provide everyone with a detailed and easy-to-understandUbuntuOn the systemApache httpdTutorial on setting up a proxy server. Whether you are a beginner or an experienced developer, I believe you can find a learning path that suits you and easily master it.UbuntuSkills to build an efficient proxy server.

2. Overview of Apache configuration files in Ubuntu

In Ubuntu, Apache2's configuration system is unique. Unlike some systems that concentrate all settings in a single configuration file, Ubuntu adopts a modular design concept. This design distributes different configuration items in multiple files andapache2.confIn the main configuration fileIncludeDirectives are used to reference these scattered configuration files.

Specifically, the Apache2 configuration files and directory structure in Ubuntu can be organized as follows:

  1. Main configuration file:
  • Location:/etc/apache2/apache2.conf
  • Function: This is the main configuration file of Apache, which is automatically read when the system starts. It contains pointers to other configuration files.IncludeInstructions are used to load important settings such as module configuration, port configuration, virtual host configuration, etc.
  1. Module configuration file:
  • Available module configurations:/etc/apache2/mods-available/Directory, where all available Apache module configuration files are stored (.loadand.confdocument).

  • Enabled module configuration:/etc/apache2/mods-enabled/Directory, storage points tomods-availableSymbolic links to files in the directory determine which modules are actually enabled.

  1. Port Profile:
  • Location:/etc/apache2/ports.conf
  • Function: Contains the port number settings for Apache listening. This file needs to be edited when modifying the listening port.
  1. Virtual host configuration file:
  • Available virtual host configurations:/etc/apache2/sites-available/Directory that holds all available virtual host configuration files.
  • Enabled virtual host configuration:/etc/apache2/sites-enabled/Directory, storage points tosites-availableThe symbolic links to the files in the directory determine which virtual hosts are actually enabled.
  1. Other configuration files:
  • Optional:/etc/apache2/conf-available/and/etc/apache2/conf-enabled/Directory for storing other optional configuration files and their enabling links.
  • Environment variables settings:/etc/apache2/envvarsFile containing environment variables for Apache runtime.
  1. User-defined configuration files (non-default main configuration):
  • Location:/etc/apache2/httpd.conf
  • Note: In some cases, users may add custom configurations in this file. However, in a default installation of Apache2 on Ubuntu, this file may be empty because all major configurations have been scattered into other files.

Precautions

  • In Apache2 on Ubuntu,httpd.confIt is not a primary configuration file and may be empty by default. Therefore, when configuring Apache in Ubuntu, you should focus onapache2.confDocuments andIncludeOther configuration files referenced by directives.

3. Configure and enable Apache proxy service

1. Enable the proxy module

  • Navigate to the module enablement directory:cd /etc/apache2/mods-enabled
  • Create the necessary symbolic links to enable the proxy module:
sudo ln -s ../mods-available/proxy.load  
sudo ln -s ../mods-available/proxy_connect.load  
sudo ln -s ../mods-available/proxy_http.load  
sudo ln -s ../mods-available/proxy.conf
  • 1
  • 2
  • 3
  • 4

After execution, use ls -al Run the command to check the link status. The output should be similar to the following:

gao@NAS:/etc/apache2/mods-enabled$ ls -al pr*  
lrwxrwxrwx 1 root root 28  711 20:44 proxy.conf -> ../mods-available/proxy.conf  
lrwxrwxrwx 1 root root 36  711 20:40 proxy_connect.load -> ../mods-available/proxy_connect.load  
lrwxrwxrwx 1 root root 33  711 20:40 proxy_http.load -> ../mods-available/proxy_http.load  
lrwxrwxrwx 1 root root 28  711 20:40 proxy.load -> ../mods-available/proxy.load
  • 1
  • 2
  • 3
  • 4
  • 5
  • Example: proxy.load file content

    LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
    
    • 1

2. Modification proxy.conf Configuration Files

Use a text editor such as vi)edit proxy.conf document:

gao@NAS:/etc/apache2/mods-enabled$ sudo vi proxy.conf
  • 1

Modify the following, making sure to uncomment and configure the proxy settings appropriately:

<IfModule mod_proxy.c>  
    # 如果想将 Apache 用作正向代理。  
    # 注意:请务必在 <Proxy *> 块中限制访问。  
    # 开放代理服务器对您的网络和整个互联网都是危险的。  
    #  
    # 如果只想将 Apache 用作某些 Web 应用程序服务器前的反向代理/网关,  
    # 则不需要 'ProxyRequests On'。  
    ProxyRequests On  
    <Proxy *>  
      Require all granted  
    </Proxy>  
</IfModule>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3. Configure the listening port

  • Navigate to the Apache configuration file directory:cd /etc/apache2
  • edit ports.conf File to add or modify the listening port:
gao@NAS:/etc/apache2$ sudo vi ports.conf
  • 1

Modify the content as follows, add a new listening port (for example 8756):

# 如果只是更改端口或在此处添加更多端口,则可能还需要更改  
# /etc/apache2/sites-enabled/000-default.conf 中的 VirtualHost 语句。  
  
Listen 80  
Listen 8756  
  
<IfModule ssl_module>  
    Listen 443  
</IfModule>  
  
<IfModule mod_gnutls.c>  
    Listen 443  
</IfModule>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

4. Start Apache service

Start the Apache service using the following command:

sudo systemctl start apache2
  • 1

Make sure the Apache service has been started successfully and the proxy service is listening on the configured port.

gao@NAS:~$ netstat -an|grep '8756'
tcp6       0      0 :::8756                 :::*                    LISTEN     
  • 1
  • 2

At this point, the Apache httpd proxy server has been successfully configured and started on the Ubuntu system.