Custom HTTPD Configurations - add configuration using bash script

feerdispzoo

Verified User
Joined
Jan 17, 2022
Messages
189
Dashboard / Custom HTTPD Configurations / Customize configuration

I can set proxy pass in ngnix directly in directadmin using custom httpd configurations like this:
1705076212734.png




But still I need do this manuall, I need automate this process, is any way define this via API directadmin or using bash script ?
 
if you want for all domains. just put it into "/usr/local/directadmin/data/templates/custom/cust_nginx.CUSTOM2.pre" and trying "rewrite_confs"
 
I have automated bash script for build nodeapps for end users. First in bash script I use dynamic find available ports start from 3000, this script check available ports, seed in env and build app, finally i need add this available port in proxy pass for specific domain.

And each domain have different ports like 3003,3004,3005 etc. and now im looking for solution, maybe is possible create file directly for each user in custom ngnix with this port. some like: /usr/local/directadmin/data/templates/custom/cust_nginx.CUSTOM2.pre but not from root for all domains, but from user level for specific user.
 
ok, this is just my idea.

#note: $USER , $DOMAIN must define in your bash script.

You need bash script to automate put to
Code:
/usr/local/directadmin/data/users/$USER/domains/$DOMAIN.cust_nginx

It same thing when you customize from web panel.

then rewrite virtualhost file for one user
Code:
/usr/local/directadmin/directadmin taskq --run 'action=rewrite&value=httpd&user=$USER'


this will be a bit too many task. but it could working perfect fine.

###update, I forgot mention hook script.

So anyway, you need to store tmp port to somewhere and use these hook to get the port that you assign to domains.
 
I have set bash script running on hook during create new domain.


Base on your response I write bash script:
Code:
#!/bin/bash


#print HOME DIR
sudo -i -H -u $username bash -c 'echo $HOME'


#  'username'
username=${username}




# Find an available port frontend
find_available_port_frontend() {
    local port=3003  # Starting port number, adjust as needed
    while [[ "$(lsof -i :${port})" ]]; do
        ((port++))
    done
    echo "${port}"
}




# Set the available port and username
available_port_frontend=$(find_available_port_frontend)




# List all domains for the user
domains=$(ls "/home/$username/domains")


# Find the domain containing "api"
api_domain=$(echo "$domains" | grep "checkout")


# Find the domain containing "shop"
shop_domain=$(echo "$domains" | grep "shop")




# Create custom nginx httpd configuration
touch "/usr/local/directadmin/data/users/$username/domains/$api_domain.cust_nginx"




#rewrite virtualhost file for one user
/usr/local/directadmin/directadmin taskq --run 'action=rewrite&value=httpd&user=$username'


I touch this file:
touch "/usr/local/directadmin/data/users/$username/domains/$api_domain.cust_nginx"

But im not sure, should I seed something in this file? I have stored port in variable "available_port_frontend" but in your response I not see anywhere how to specific this port.


But I think i need to seed this custom file in the same way like:
--seed i |?PROXY_IP=xxxxxx|
|?PORT_8080=$available_port_frontend|
|?PORT_8081=$available_port_frontend|


so this should be placed content in the same structure like in directadmin:
Code:
# Create custom file with specified content
custom_file="/usr/local/directadmin/data/users/$username/domains/$api_domain.cust_nginx"
echo "|?PROXY_IP=xxxxx|
|?PORT_8080=$available_port_frontend|
|?PORT_8081=$available_port_frontend|" > "$custom_file"


echo "Custom file created for $api_domain with specified content."


Or I should write here full template structure nginx?
 
Last edited:
just store port into some file, like "/tmp/worker_port_$DOMAIN.tmp" and use hooks to read this files via "cat" command.
Or you can use "curl" to read the port from remote files that you store from outside your server.

then clean up after finish.

Currently no ways to pass variable from outside into the hooks script.


I forgot to mention. If you use "pre" hooks script, you don't need to rewrite virtualhost.
 
@jamgames2 thanks for reply, i can create temp file in example /tmp/worker_port..tmp

But im not sure with this part of code: (can you check my previous update)

I create custom file example:
custom_file="/usr/local/directadmin/data/users/$username/domains/$api_domain.cust_nginx"

But im not sure about which structure content should have this file? The same structure Like I place manual in directadmin custom httpd configurations?


Code:
# Create custom file with specified content
custom_file="/usr/local/directadmin/data/users/$username/domains/$api_domain.cust_nginx"
echo "|?PROXY_IP=xxxxx|
|?PORT_8080=$available_port_frontend|
|?PORT_8081=$available_port_frontend|" > "$custom_file"


echo "Custom file created for $api_domain with specified content."
 
yes, you can place same structure. Because it working in same ways when you customize with DA Panel.
 
Back
Top