Install Apache2 Web Server in Termux

August 15, 2023 12 min read Sandeep Tech Team

Apache2 in Termux Demo

Watch this demonstration of Apache2 web server running in Termux. The video shows the complete process from installation to configuration and testing. This visual guide will help you understand how to transform your Android device into a web server.

Apache2 is one of the most popular web server software in the world, and now you can run it directly on your Android device using Termux. This comprehensive guide will walk you through the entire process of installing, configuring, and using Apache2 in Termux for web development and hosting purposes.

40%
Web Market Share
No Root
Required
Free
Open Source
Full
Featured

What is Apache2 Web Server?

Apache HTTP Server, commonly known as Apache2, is a free and open-source cross-platform web server software. It is developed and maintained by the Apache Software Foundation. Apache2 is the most widely used web server software, serving a large percentage of all websites on the internet. It's known for its stability, security, and flexibility.

Educational Purpose

Running Apache2 in Termux is perfect for learning web development, testing websites locally, and understanding server administration. Always ensure you have proper authorization before hosting any public-facing services.

Why Use Apache2 in Termux?

Running Apache2 in Termux offers several advantages:

  • Portability - Carry a complete web server in your pocket
  • No Root Required - Works on non-rooted Android devices
  • Learning Environment - Perfect for web development practice
  • Testing Platform - Test websites and web applications locally
  • Cost-Effective - Free alternative to paid hosting services
  • Emergency Server - Have a backup web server available anywhere

Prerequisites

Before installing Apache2 in Termux, make sure you have the following:

  • An Android device (Android 5.0 or later)
  • Termux app installed from F-Droid (not Google Play Store)
  • Stable internet connection for downloading packages
  • At least 500MB of free storage space

Important Note

Always install Termux from F-Droid, not from the Google Play Store. The Play Store version is outdated and may not work properly with the latest packages.

Installation Guide

Follow these steps to install Apache2 in Termux:

# Update and upgrade Termux packages apt update && apt upgrade -y

Install the required dependencies:

# Install required packages apt install wget curl git tar openssl -y

Now, install Apache2:

# Install Apache2 apt install apache2 -y

Verify the installation:

# Check Apache2 version apachectl -v

Basic Configuration

After installation, you need to configure Apache2 for Termux:

# Create a backup of the original configuration file cp $PREFIX/etc/apache2/httpd.conf $PREFIX/etc/apache2/httpd.conf.bak

Edit the configuration file:

# Edit the configuration file nano $PREFIX/etc/apache2/httpd.conf

Make the following changes to the configuration file:

  • Find the line Listen 8080 and change it to Listen 8080 (or any other port you prefer)
  • Find the line ServerName www.example.com:8080 and change it to ServerName localhost:8080
  • Find the line DocumentRoot "/data/data/com.termux/files/usr/share/apache2/default-site/htdocs" and make sure it's correct

Save the file and exit the editor (in nano, press Ctrl+X, then Y, then Enter).

Starting the Server

Now that Apache2 is installed and configured, you can start the server:

# Start Apache2 server apachectl start

To check if the server is running:

# Check Apache2 status pgrep httpd

If you see a process ID, Apache2 is running successfully.

Testing the Server

To test if Apache2 is working correctly:

# Test the server locally curl http://localhost:8080

You should see the HTML content of the default Apache2 page. Alternatively, you can open a web browser on your device and navigate to http://localhost:8080.

To access the server from other devices on the same network:

# Find your device's IP address ip addr

Look for your device's IP address (usually under "wlan0" for WiFi). Then, from another device on the same network, navigate to http://[your-device-ip]:8080.

Network Access

Make sure your device's firewall allows incoming connections on port 8080. If you're using mobile data, you may need to set up port forwarding or use a VPN to access the server from outside your local network.

Serving Custom Content

To serve your own website or web application, you need to place your files in the document root directory:

# Navigate to the document root directory cd $PREFIX/share/apache2/default-site/htdocs

Create a simple HTML file:

# Create a simple HTML file echo '<!DOCTYPE html> <html> <head> <title>My Apache2 Server</title> </head> <body> <h1>Welcome to My Apache2 Server in Termux!</h1> <p>This is a custom HTML page served from my Android device.</p> </body> </html>' > index.html

Restart Apache2 to apply the changes:

# Restart Apache2 apachectl restart

Now, when you access http://localhost:8080, you should see your custom HTML page.

Adding PHP Support

To add PHP support to your Apache2 server, follow these steps:

# Install PHP apt install php php-apache -y

Edit the Apache2 configuration file to enable PHP:

# Edit the configuration file nano $PREFIX/etc/apache2/httpd.conf

Add the following lines to the end of the file:

# Enable PHP LoadModule php_module $PREFIX/lib/libphp.so AddHandler application/x-httpd-php .php

Save the file and restart Apache2:

# Restart Apache2 apachectl restart

Create a test PHP file:

# Create a test PHP file echo '<?php phpinfo(); ?>' > $PREFIX/share/apache2/default-site/htdocs/test.php

Now, navigate to http://localhost:8080/test.php in your browser. You should see the PHP information page, which indicates that PHP is working correctly with Apache2.

Setting Up Virtual Hosts

Virtual hosts allow you to host multiple websites on a single server. Here's how to set up virtual hosts in Apache2:

First, create directories for your virtual hosts:

# Create directories for virtual hosts mkdir -p $PREFIX/share/apache2/site1/htdocs mkdir -p $PREFIX/share/apache2/site2/htdocs

Create index files for each site:

# Create index file for site1 echo '<h1>Welcome to Site 1</h1>' > $PREFIX/share/apache2/site1/htdocs/index.html # Create index file for site2 echo '<h1>Welcome to Site 2</h1>' > $PREFIX/share/apache2/site2/htdocs/index.html

Edit the Apache2 configuration file to add virtual hosts:

# Edit the configuration file nano $PREFIX/etc/apache2/httpd.conf

Add the following virtual host configurations at the end of the file:

# Virtual host for site1 <VirtualHost *:8080> ServerName site1.localhost DocumentRoot $PREFIX/share/apache2/site1/htdocs <Directory $PREFIX/share/apache2/site1/htdocs> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> # Virtual host for site2 <VirtualHost *:8080> ServerName site2.localhost DocumentRoot $PREFIX/share/apache2/site2/htdocs <Directory $PREFIX/share/apache2/site2/htdocs> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost>

Save the file and restart Apache2:

# Restart Apache2 apachectl restart

To test the virtual hosts, you need to edit the hosts file on your device:

# Edit the hosts file nano /system/etc/hosts

Add the following lines to the hosts file:

127.0.0.1 site1.localhost 127.0.0.1 site2.localhost

Save the file. Now, you can access your virtual hosts by navigating to http://site1.localhost:8080 and http://site2.localhost:8080 in your browser.

Root Access Required

Editing the hosts file requires root access. If your device is not rooted, you can test the virtual hosts by accessing them directly via IP and port, or by using a proxy or VPN service.

Troubleshooting

Here are some common issues and their solutions:

Port Already in Use

If you get an error that the port is already in use, you can change the port in the Apache2 configuration file:

# Find the process using the port netstat -tulpn | grep :8080 # Kill the process if necessary kill -9 [PID]

Permission Denied

If you get permission errors, make sure the files and directories have the correct permissions:

# Set correct permissions chmod -R 755 $PREFIX/share/apache2/default-site/htdocs

Server Not Starting

If the server doesn't start, check the error log:

# Check the error log tail -f $PREFIX/var/log/apache2/error_log

Configuration Errors

If you have configuration errors, check the syntax of the configuration file:

# Check configuration syntax apachectl configtest

Advanced Features

Apache2 offers many advanced features that you can use in Termux:

SSL/TLS Support

To enable HTTPS support, you need to install OpenSSL and configure SSL:

# Install OpenSSL apt install openssl -y # Generate a self-signed certificate openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout $PREFIX/etc/apache2/server.key -out $PREFIX/etc/apache2/server.crt

Edit the Apache2 configuration file to enable SSL:

# Edit the configuration file nano $PREFIX/etc/apache2/httpd.conf

Add the following lines to enable SSL:

# Enable SSL LoadModule ssl_module $PREFIX/lib/apache2/mod_ssl.so Listen 8443 <VirtualHost *:8443> SSLEngine on SSLCertificateFile $PREFIX/etc/apache2/server.crt SSLCertificateKeyFile $PREFIX/etc/apache2/server.key DocumentRoot $PREFIX/share/apache2/default-site/htdocs </VirtualHost>

Save the file and restart Apache2:

# Restart Apache2 apachectl restart

Now, you can access your server using HTTPS at https://localhost:8443.

.htaccess Files

.htaccess files allow you to make configuration changes on a per-directory basis. To enable .htaccess files, edit the Apache2 configuration file:

# Edit the configuration file nano $PREFIX/etc/apache2/httpd.conf

Find the Directory section for your document root and make sure it includes the following directives:

<Directory "$PREFIX/share/apache2/default-site/htdocs"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>

Save the file and restart Apache2:

# Restart Apache2 apachectl restart

Now, you can create .htaccess files in your directories to override the default configuration.

URL Rewriting

URL rewriting allows you to create clean, user-friendly URLs. To enable URL rewriting, edit the Apache2 configuration file:

# Edit the configuration file nano $PREFIX/etc/apache2/httpd.conf

Uncomment the following line to enable the rewrite module:

LoadModule rewrite_module $PREFIX/lib/apache2/mod_rewrite.so

Save the file and restart Apache2:

# Restart Apache2 apachectl restart

Now, you can create .htaccess files with rewrite rules to customize your URLs.

Interactive Demo

Try Apache2 Commands

Experience the power of Apache2 with our interactive command simulator. Try running some basic commands to see how they work.

Termux
$ Type a command or click a button below

Command Reference

Apache2 Commands

Command Description Example
apachectl start Start the Apache2 server apachectl start
apachectl stop Stop the Apache2 server apachectl stop
apachectl restart Restart the Apache2 server apachectl restart
apachectl status Check the status of the Apache2 server apachectl status
apachectl configtest Test the Apache2 configuration file syntax apachectl configtest
apachectl -v Display the Apache2 version apachectl -v
apachectl -M List loaded modules apachectl -M
apachectl -S Show parsed virtual host settings apachectl -S
tail -f $PREFIX/var/log/apache2/error_log Monitor the Apache2 error log tail -f $PREFIX/var/log/apache2/error_log
tail -f $PREFIX/var/log/apache2/access_log Monitor the Apache2 access log tail -f $PREFIX/var/log/apache2/access_log

Apache2 in Termux brings the power of a full-featured web server to your Android device. Whether you're a web developer, a student learning about web servers, or just someone interested in technology, Apache2 provides a robust platform for hosting websites and web applications directly from your Android device.

Back to Blogs

Leave a Comment

Alex Johnson
August 16, 2023
This guide is amazing! I've been trying to set up a web server on my Android device for weeks. The step-by-step instructions made it so easy. The virtual hosts section was particularly helpful for my projects.
Sarah Williams
August 17, 2023
Thanks for this comprehensive tutorial! I'm a web development student and this has been incredibly useful for practicing on the go. The PHP support section worked perfectly. Now I can test my projects anywhere!