Installing Tengine with `headers-more-nginx-module` on an ECS

Introduction

Step-by-Step Guide: Installing Tengine with headers-more-nginx-module on an ECS

Tengine, a high-performance HTTP and reverse proxy server, is a fork of Nginx developed by Alibaba. It includes many advanced features and optimizations. This guide will walk you through installing Tengine from source on an ECS (Elastic Compute Service) instance, along with the headers-more-nginx-module for enhanced HTTP header manipulation. We'll also cover setting it up as a systemd service for automatic startup.

Install Compilation Environment

Before compiling Tengine, we need to ensure that all necessary development tools and libraries are installed. These include gcc-c++, pcre-devel (for regular expression support), openssl-devel (for SSL/TLS support), and zlib-devel (for compression).

Run the following commands one by one. If a package is already installed, yum will inform you. Otherwise, it will install or update it.

sudo yum install gcc-c++ -y
sudo yum install pcre pcre-devel -y
sudo yum install zlib zlib-devel -y
sudo yum install openssl openssl-devel -y

Download and Decompress Software

Next, we'll download the Tengine source code and the headers-more-nginx-module. For this guide, we're using tengine-2.3.3 and headers-more-nginx-module-0.33. We'll place these in the /opt/softwares directory.

First, create the directory if it doesn't exist:

sudo mkdir -p /opt/softwares
cd /opt/softwares

Now, download the archives. You can usually find the latest Tengine release on the Tengine website and modules like headers-more-nginx-module on GitHub (e.g., OpenResty's repository).

Example (replace with actual download URLs if different):

# Example: Download Tengine
sudo wget http://tengine.taobao.org/download/tengine-2.3.3.tar.gz

# Example: Download headers-more-nginx-module
sudo wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags/v0.33.tar.gz -O headers-more-nginx-module-0.33.tar.gz

Once downloaded, decompress them in the current directory (/opt/softwares):

sudo tar -zxvf tengine-2.3.3.tar.gz
sudo tar -zxvf headers-more-nginx-module-0.33.tar.gz

Note: Ensure the uncompressed directory for the module is named headers-more-nginx-module-0.33 or adjust the --add-module path in the next step accordingly. If headers-more-nginx-module-0.33.tar.gz uncompresses to a directory like headers-more-nginx-module-v0.33, you might need to rename it:

# Example if uncompressed directory name differs:
# sudo mv headers-more-nginx-module-v0.33 headers-more-nginx-module-0.33

Configure, Compile, and Install Tengine

Now, navigate to the Tengine source directory and run the configure script. We'll specify the installation prefix and add our desired module.

cd /opt/softwares/tengine-2.3.3
sudo ./configure --prefix=/opt/tengine --add-module=/opt/softwares/headers-more-nginx-module-0.33
  • --prefix=/opt/tengine: This tells the build system to install Tengine into the /opt/tengine directory.
  • --add-module=/opt/softwares/headers-more-nginx-module-0.33: This compiles Tengine with the headers-more-nginx-module.

After configuration, compile and install Tengine:

sudo make && sudo make install

Configure Tengine as a Systemd Service

To manage Tengine easily and ensure it starts automatically on boot, we'll create a systemd service file. Systemd service files are typically placed in /usr/lib/systemd/system/ for packaged software or /etc/systemd/system/ for administrator-added services. Following the original guide's convention, we'll use /usr/lib/systemd/system/.

Create and edit the service file nginx.service (we use nginx.service as Tengine is an Nginx fork and often managed this way):

sudo vim /usr/lib/systemd/system/nginx.service

Paste the following content into the file:

[Unit]
Description=The nginx HTTP and reverse proxy server (Tengine)
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/tengine/logs/nginx.pid
ExecStartPre=/opt/tengine/sbin/nginx -t -c /opt/tengine/conf/nginx.conf
ExecStart=/opt/tengine/sbin/nginx -c /opt/tengine/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Explanation of the service file:

  • Description: A short description of the service.
  • After: Specifies dependencies; Tengine will start after these targets are active.
  • Type=forking: Tengine forks a master process, and the parent exits.
  • PIDFile: Path to the PID file Tengine creates.
  • ExecStartPre: Command to run before starting, here it tests the configuration.
  • ExecStart: The command to start Tengine.
  • ExecReload: Command to gracefully reload the configuration.
  • ExecStop: Command to gracefully stop Tengine.
  • PrivateTmp=true: Uses a private temporary directory for the service.
  • WantedBy=multi-user.target: Enables the service to start at boot for multi-user runlevel.

Modify the file permissions:

sudo chmod 755 /usr/lib/systemd/system/nginx.service

(Note: 644 is also a common permission for service files, but 755 as provided in the original guide allows execution if needed, though systemd itself doesn't require the service file to be executable.)

Reload the systemd daemon to recognize the new service file:

sudo systemctl daemon-reload

Manage the Tengine Service

You can now manage the Tengine service using systemctl commands.

Enable service to start on boot:

sudo systemctl enable nginx.service

Disable service from starting on boot:

sudo systemctl disable nginx.service

Start the Tengine service:

sudo systemctl start nginx.service

Stop the Tengine service:

sudo systemctl stop nginx.service

Restart the Tengine service:

sudo systemctl restart nginx.service

View the current status of the service:

sudo systemctl status nginx.service

View all started services:

systemctl list-units --type=service --state=running

Conclusion

You have successfully installed Tengine with the headers-more-nginx-module from source and configured it as a systemd service on your ECS. You can now proceed to configure Tengine further by editing /opt/tengine/conf/nginx.conf to suit your web serving needs. Remember to test your configuration (sudo /opt/tengine/sbin/nginx -t) and reload Tengine (sudo systemctl reload nginx.service) after making changes.

Community

We're excited to see the community adopt Hyperse-io, raise issues, and provide feedback. Whether it's a feature request, bug report, or a project to showcase, please get involved!