In this article we will see how we can setup local webserver in Termux with https.
So first Go to termux and make sure that your repos are up-to-date
pkg update
Then install apache2 and openssl
pkg install apache2 openssl openssl-tool
Once it is installed, you can start your webserver just by executing
apachectl
You will see some warnings here, don't worry will be fixing it in few mins, but first open your browser and go to localhost:8080.
But now it is using http and we don't want that we need https, so to do that again go to termux and change your directory
cd $PREFIX/etc/apache2/
and now open httpd conf file
nano httpd.conf
Scroll down and just after ServerRoot write
ServerName localhost
Now press Ctrl W key on your keyboard and search for ssl and uncomment that line
Now again search for shmcb and uncomment that line also.
Then search for httpd-ssl and uncomment it.
Then again search for httpd-vhosts and uncomment that also.
And that's it we are done with this file just save it Ctrl X Y and enter.
Next create a directory SSL and give it required permissions.
mkdir -p $PREFIX/etc/apache2/ssl ; chmod 700 $PREFIX/etc/apache2/ssl
Now execute following to create SSL certificate and key and store them in the directory that we just created.
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout $PREFIX/etc/apache2/ssl/cert.key -out $PREFIX/etc/apache2/ssl/cert.crt
Once it is done, then just
ls ssl
and the certificate and key will be stored there.
Now change directory to extra
cd extra
and open httpd-ssl.conf file
nano httpd-ssl.conf
And search for ServerName,
Here you need to change example.com with localhost
Now search for cert, Scroll down and here you need to specify the certificate path that we created,
$PREFIX/etc/apache2/ssl/cert.crt
Then search for key, Scroll down and specify the key path we created earlier
$PREFIX/etc/apache2/ssl/cert.key
And now we are done with this file also, just save it
Next we need to open httpd-vhosts.conf file
nano httpd-vhosts.conf
scroll down and there you need to change document root with following path
$PREFIX/share/apache2/default-site/htdocs
Next change ServerName to local host
Now here at bottom , just before </virtualhost> write the following
Redirect / https://localhost:8443
Now this will redirect all the connections to this url which our the url of our server with https
Same you have to do for next host also,
Then just save the file and restart Apache server
apachectl -k restart
And open your browser and if you reload the sire. it will redirect you to https connection which is using 8443 port, and you will see a warning, because we are using self sign certificate, just ignore it and continue to the site.
Now if you want configure backend like PHP, you can watch this video.
apachectl -k stop