⬆️ ⬇️

Automate the creation of a virtual host for web development based on Apache + Nginx

How to quickly create a virtual host and folder structure in a single line in the terminal



It just so happens that for web development I use a separate server based on Debian. This is primarily due to the fact that I often work outside the home, and it also allows us to work together on a project with colleagues.

Previously, I had to manually create directories for the site, copy and edit configs. I spent a lot of time on it and sometimes I was wrong, and then frantically I searched where and what I missed.



Not so long ago, I thought about automating the process of creating virtual hosts. Rush a little on the Internet, and at the same time remembering bash, a script was born that saved me from routine and mistakes at the same time.



My configuration is as follows:





Create a file:

cd ~ touch addvhost.sh chmod +x addvhost.sh 


')

and write the following



 #!/bin/sh hostmaster="hostmaster@test.ru" #    www_path="/var/www/hosting/" #       wwwuser="www-data" wwwgroup="www-data" case "$@" in "") echo "   (as root)." ;; *) clear echo "  " mkdir -p $www_path$1/www/ mkdir -p $www_path$1/cgi-bin/ mkdir -p $www_path$1/log/ echo "$www_path$1/www/" echo "$www_path$1/cgi-bin/" echo "$www_path$1/log/" echo "\n  index.html " echo " " > $www_path$1/www/index.html chown -R $wwwuser:$wwwgroup /$www_path$1 chmod -R 0755 /$www_path$1 echo "\n  : /etc/apache2/sites-enabled/$1" exec 3>&1 1>/etc/apache2/sites-enabled/$1 echo "<virtualhost \${HOSTING_HOST}:$2>" echo " ServerName $1" echo " ServerAdmin $hostmaster" echo " " echo " DocumentRoot \${HOSTING_ROOT}/$1/www/" echo " <Directory />" echo " Options Indexes Includes FollowSymLinks MultiViews" echo " Order allow,deny" echo " AllowOverride All" echo " Allow from All" echo " </Directory>" echo " " echo " <Directory \${HOSTING_ROOT}$1/www/>" echo " Options Indexes Includes FollowSymLinks MultiViews" echo " Order allow,deny" echo " AllowOverride All" echo " Allow from All" echo " </Directory>" echo " " echo " ScriptAlias /cgi-bin/ \${HOSTING_ROOT}/$1/cgi-bin/" echo " <Directory \${HOSTING_ROOT}/$1/cgi-bin/>" echo " AllowOverride None" echo " Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch" echo " Order allow,deny" echo " Allow from all" echo " </Directory>" echo " " echo " ErrorLog \${HOSTING_ROOT}/$1/log/error.log" echo " LogLevel warn" echo " CustomLog \${HOSTING_ROOT}/$1/log/access.log combined" echo " ServerSignature On" echo " " echo "</virtualhost>" exec 1>&3 echo "\n  : /etc/nginx/sites-enabled/$1" exec 3>&1 1>/etc/nginx/sites-enabled/$1 echo "server {" echo " listen 80;" echo " server_name $1;" echo " " echo " #charset koi8-r;" echo " " echo " access_log $www_path$1/log/$1-nginx.access.log main;" echo " " echo " location / {" echo " proxy_pass http://127.0.0.1:$2/;" echo " proxy_redirect off;" echo " proxy_set_header Host \$host;" echo " proxy_set_header X-Real-IP \$remote_addr;" echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;" echo " client_max_body_size 40m;" echo " client_body_buffer_size 256k;" echo " " echo " proxy_connect_timeout 120;" echo " proxy_send_timeout 120;" echo " proxy_read_timeout 120;" echo " proxy_buffer_size 64k;" echo " proxy_buffers 4 64k;" echo " proxy_busy_buffers_size 64k;" echo " proxy_temp_file_write_size 64k;" echo " proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;" echo " }" echo " #Static files location" echo " location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|html|flv|mp3)$ " echo " {" echo " root $www_path$1/www/;" echo "}" echo "}" exec 1>&3 sleep 1 echo " " sudo /etc/init.d/apache2 restart sudo /etc/init.d/nginx restart echo " ;)" echo "      http://$1" ;; esac 


We will understand a little with the code:


  1. If you decide to "copy-paste" the script, delete the backslash in the "<\ / Directory>". I added a backslash so that the editor does not eat the string.
  2. I use frontend / backend technology, so every single host sits on a separate port of Apache for proxying it with NGINX.
Ports should be listed in the /etc/apache2/ports.conf config



 Listen 8080 Listen 8081 Listen 8082 Listen 8083 #__ 




$ {HOSTING_HOST} and $ {HOSTING_ROOT} are Apache2 variables. I need them.

Instead, you can use your variables or explicitly specify addresses / paths.

Also note that $ {HOSTING_ROOT} has the same meaning as $ www_path, so you can use it.



Now that we’ve figured it all out, we’ll run


 sudo ./addvhost.sh test.ru 8080 


where “test.ru” is the name of our domain (assigned to $ 1), and “8080” is the port on which we will have this test domain (assigned to $ 2).



What we have in the end : we automatically create directories for virtual hosts. Virtual host configs for Apache and Nginx with the parameters we need are also automatically generated, added up and restarted services. Profit



Ps. Of course, this script is very primitive and does not claim to originality, but it is still capable of making life much easier for a novice developer or administrator.

For local development I use MAMP Pro. All the functionality described above, and even more, is available there through a beautiful and convenient GUI, but not everything works on a Mac and not always, but you have to live somehow.

Pps. Alternatively, you can use configuration file templates instead of echo "every line". But I'm still fine.

There will be time - I will finish it and post it.



UPD - A new version of the script using configs templates is available here.

The echo “towel” is replaced with the usual sed -e. The script has become more flexible and simple.



UPD II - Updated the script and article. The article, taking into account the highlighting of the code, and in the script popavil some bugs.

Source: https://habr.com/ru/post/93695/



All Articles