Generate NGINX virtual hosts script

I created simple script for creating NGINX virtual hosts so that you don’t have to do it manualy for every new website. Script was created for Linux – CentOS 7 – operating system but it should work on other distributions too. It is written in bash. You will also need wget and tar installed for script to work – wordpress install option.

What it does is pretty straightforward. On input side it will ask you for domain name, SSL option and WordPress installation. You can choose between http and https virtual host definition. By default it will create document root for your domain and NGINX configuration file for that domain. If you choose option for WordPress installation, then it will also download latest wordpress version and unpack files to your newly created document root. You’ll still need to create database manually and finish WordPress installation. This script is suitable for basic NGINX website configurations

Just download script here and template files. Put script createsite to your /usr/sbin/ directory and make it executable. Of corse you can change virtual host templates according to your needs too.

  • Copy script to your /usr/sbin directory and make it executable:
[root@vincentvega sbin]# chmod +x createsite 
  • Open script with text editor and setup settings under »global settings« according to your needs.
  • Now you can create new virtual host by running command createsite
[root@vincentvega ~]# createsite

Enter domain name [without www]: test.org
Install latest WordPress? [y/n] y
Do you want SSL support? [y/n]: y

---------------------------------------------------------------------
  Your virtual host for test.org has been created!
---------------------------------------------------------------------

Domain name: test.org
Document root: /var/www/sites/test.org/public_html
Document root owner: nginx:nginx

Latest WordPress installed: yes
Please finish your wordpress setup by visiting your created website.

SSL enabled: yes
SSL certificate directory: /etc/nginx/ssl/test.org

Please load your certificate and key in directory listed above!

NGINX is already restarted!

---------------------------------------------------------------------
  • It will generate NGINX configuration, create all necessary directories and download latest WordPress if you selected that option:
server {
        listen       443;
        server_name  test.org www.test.org;

        ssl on;
        ssl_certificate /etc/nginx/ssl/test.org/fullchain.pem;
        ssl_certificate_key /etc/nginx/ssl/test.org/privkey.pem;

        error_log  /var/log/nginx/error.log;

        root   /var/www/sites/test.org/public_html;
        index  index.php index.html;
		
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi_params;
            fastcgi_param  SERVER_ROOT /var/www/sites/test.org/public_html;
	    fastcgi_param  DOCUMENT_ROOT /var/www/sites/test.org/public_html;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
	}
}

1 Comment

Got Something To Say:

Your email address will not be published. Required fields are marked *

*

I accept the Privacy Policy

This site uses Akismet to reduce spam. Learn how your comment data is processed.

hi, its not working properly,

for me i have used, domain, and ssl and ignored wordpress.

but its did not create any config file under /etc/nginx/sites-available or sites-enabled.
only /etc/www/ website directory is ther.

© 2024 geegkytuts.net
Hosted by SIEL


About author