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.
Before we begin, make sure you have the following prerequisites:
Open the Termux app on your Android device.
Update the package lists by running the command:
pkg upd
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.
Once the installation is complete, start the Apache server with the command:
apachect start
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:
http://localhost:8080
Or copy the command and run in termux.
termux-open http://localhost:8080
You would see the default Apache page if everything is working correctly
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.
Path:
/data/data/com.termux/files/usr/etc/apache2/httpd.conf
We can open httpd.conf file with nano text editor and make changes.
nano $PREFIX/etc/apache2/httpd.conf
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.
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
ServerName localhost
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.
mkdir /sdcard/htdocs
Step B: Change document root in httod.conf and save.
DocumentRoot "/sdcard/htdocs"
<Directory "/sdcard/htdocs">
Step C: Create new webpage index.html file in your new DocumentRoot /sdcard/htdocs and write any message.
Step D: Restart Apache2 Server.
apachectl restart
Step E: Run the command to open URL http://localhost:8080 in web browser.
termux-open http://localhost:8080
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
Listen 8090
Now the Apche2 Server will run on 8090 post. The url will be looks like this:
http://localhost:8090
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:
$PREFIX/var/log/apache2/error_log
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.
LogLevel warn
Possible other values are:
LogLevel | Description |
---|---|
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 |
info | Informational messages |
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.
To apply the configuration changes, restart the Apache server with the command:
apachectl restart
Command | Description |
---|---|
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.
Apache2 HTTP Server Documentation