Setting Up Apache2 in Termux: A Step-by-Step Guide with Configuration SettingsPoated on by
Apache2 Server: Introduction
Apache2 is a powerful and widely used web server that allows you to host websites and serve web content.
In this blog post, we will see how to install and configure Apache2 in Termux. We’ll also cover some essential configuration settings and fix SsrverName warning. This will help you customize and optimize your Apache2 setup.
Apache2 Server On Android: Prerequisites
Before we begin, make sure you have the following prerequisites:
- An Android device with Termux installed.
- A stable internet connection.
Step 1: Installation
Open the Termux app on your Android device.
Update the package lists by running the command:
Install the Apache2 package by running:
pkg i apache2 -y
After runing these commands the latest version of Apache2 Server will be installed in Termux.
Step 2: Starting the Apache Server
Once the installation is complete, start the Apache server with the command:
Here you may see ServerName Warning (How to fix? Continue reading…). Just ignore it and verify if Apache2 Server is started.
To verify if Apache2 Server is running, open a web browser on your device and enter the URL:
Or copy the command and run in termux.
You would see the default Apache page if everything is working correctly
Step 3: Configuring Apache2
The Apache config file contains directives that define how the server operates, including settings for various modules, virtual hosts, security measures, and other server-related configurations.
We can open httpd.conf file with nano text editor and make changes.
If you are new to nano, we already posted a detailed post on nano check it out.
In httpd.conf everything started with # sign is a comment and that configuration will not apply.
Fix ServerName Warning
If you are seeing ServerName Warning, it’s because ServerName is not set or is in comment section. We need to add or uncomment this setting.
Open httpd.conf in nano and find ServerName then uncomment and write following line
Other Configuration Options
Inside the httpd.conf file, you’ll find various configuration options. Here are a few essential settings you can customize:
This specifies the directory where your website’s files are located. By default, it is set to /data/data/com.termux/files/usr/share/apache2/default-site/htdocs. You can change it to the desired directory on your device.
For easy access you can change DocumentRoot within /sdcard so that you can easily access your project files. You can use any other code editor to write code.
Step A: First create project Directory in /sdcard named htdocs.
Step B: Change document root in httod.conf and save.
Step C: Create new webpage index.html file in your new DocumentRoot /sdcard/htdocs and write any message.
Step D: Restart Apache2 Server.
Step E: Run the command to open URL http://localhost:8080 in web browser.
By default, Apache listens on port 8080. If you want to use a different port, you can change it here. For example if you want run Apache on 8090 port change like this
Now the Apche2 Server will run on 8090 post. The url will be looks like this:
These directives specify the log file paths for error messages and access logs, respectively. If you want you can change it.
Default ErrorLog path in Termux is:
The Apache error log has a logging level that filters the messages sent to the log. You can specify the level with the LogLevel setting.
Possible other values are:
|emerg||System is unusable|
|alert||Immediate action is required|
|crit||Critical conditions have occurred|
|error||Error conditions have occurred|
|warn||Warning conditions have occurred|
|notice||Normal but significant conditions|
|debug||Detailed debug information|
After making the desired changes, save the file by pressing CTRL + X, then Y to confirm the changes, and finally Enter to save.
Step 4: Restarting Apache
To apply the configuration changes, restart the Apache server with the command:
Useful apachectl Commands
|apachectl start||Starts the Apache HTTP Server.|
|apachectl stop||Stops the Apache HTTP Server.|
|apachectl restart||Restarts the Apache HTTP Server.|
|apachectl graceful||Gracefully restarts the Apache HTTP Server, allowing active connections to complete before restarting.|
|apachectl configtest||Tests the Apache configuration for syntax errors.|
|apachectl status||Checks the status of the Apache HTTP Server.|
|apachectl version||Displays the Apache HTTP Server version.|
With Apache2’s extensive configuration options, you can further customize and optimize your setup according to your specific requirements. By exploring the httpd.conf file, you can fine-tune various settings, including the document root, server name, and directory options.