Get the most out of your Centmin Mod LEMP stack
Become a Member

Nginx How to edit vhost template ?

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by nVidian, Mar 18, 2015.

Tags:
  1. nVidian

    nVidian Member

    97
    8
    8
    Mar 16, 2015
    Ratings:
    +31
    Local Time:
    1:48 AM
    1.7.9
    5.5
    Hi, how & where to edit the default nginx vhost template so I dont have to edit every vhost after create using menu #2 ?

     
  2. Mask

    Mask Active Member

    108
    31
    28
    Nov 10, 2014
    Ratings:
    +37
    Local Time:
    11:48 PM
    Nginx 1.9.1
    MariaDB 10.0.19
    I am afraid you will have to edit the config file for each domain since each domain has it's own config file. You can view them under /usr/local/nginx/conf/conf.d/

    If there is come configuration that you need to add to all domains, add it in a file and then just include the file with one line in each domain config.
     
  3. eva2000

    eva2000 Administrator Staff Member

    53,826
    12,160
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,712
    Local Time:
    4:48 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    actually default vhost template for menu option 2 in centmin.sh is at inc/nginx_addvhost.inc

    For .07 stable it's at lines 47 to 93 of inc/nginx_addvhost.inc

    although include file method might be better depending on what you are trying to do :)
     
  4. neverminder

    neverminder Member

    44
    3
    8
    Nov 23, 2017
    Ratings:
    +5
    Local Time:
    8:48 PM
    1.13.6
    10.0.33
    If i edit the above file it will be overwritten by an nginx update, right? I'm just trying to find a solution to uncomment #include /usr/local/nginx/conf/cloudflare.conf; for every new vhost. Would that be possible?
     
  5. eva2000

    eva2000 Administrator Staff Member

    53,826
    12,160
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,712
    Local Time:
    4:48 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    yes it would get overwitten if you edit default the template itself

    yes very easy if you know some shell scripting to uncomment all vhosts cloudflare.conf include file you just use a bash loop like this

    this first command just does a recursive grep returns which vhost files have the mere mention of the keyword cloudflare.conf include and on which line within each vhost file it is on within vhost directory at /usr/local/nginx/conf/conf.d/. When you have many vhost files in /usr/local/nginx/conf/conf.d/, the output would be more than one vhost file
    Code (Text):
    grep -rn 'cloudflare.conf' /usr/local/nginx/conf/conf.d/
    

    example line 60 and 62 of /usr/local/nginx/conf/conf.d/domain.com.ssl.conf contain keywords for cloudflare.conf - first line is just a comment of instructions and second line is actual include file
    Code (Text):
    grep -rn 'cloudflare.conf' /usr/local/nginx/conf/conf.d/
    /usr/local/nginx/conf/conf.d/domain.com.ssl.conf:60:  # uncomment cloudflare.conf include if using cloudflare for
    /usr/local/nginx/conf/conf.d/domain.com.ssl.conf:62:  #include /usr/local/nginx/conf/cloudflare.conf;
    

    now using a shell for loop you can do a sed replacement replacing
    Code (Text):
    #include /usr/local/nginx/conf/cloudflare.conf;
    

    with
    Code (Text):
    include /usr/local/nginx/conf/cloudflare.conf;
    

    in SSH, run these 3 command for sed replacement - first command backups up vhost directory just in case. Forward slashes get escaped with back slash for sed.
    Code (Text):
    cp -a /usr/local/nginx/conf/conf.d /usr/local/nginx/conf/conf.d-backup-b4
    cd /usr/local/nginx/conf/conf.d/
    for v in $(ls /usr/local/nginx/conf/conf.d/); do sed -i "s|#include \/usr\/local\/nginx\/conf\/cloudflare.conf;|include \/usr\/local\/nginx\/conf\/cloudflare.conf;|g" $v; done
    

    Then use previous grep to check if the include file has been commented out
    Code (Text):
    grep -rn 'cloudflare.conf' /usr/local/nginx/conf/conf.d/
    

    as you can see it has
    Code (Text):
    grep -rn 'cloudflare.conf' /usr/local/nginx/conf/conf.d/
    /usr/local/nginx/conf/conf.d/domain.com.ssl.conf:60:  # uncomment cloudflare.conf include if using cloudflare for
    /usr/local/nginx/conf/conf.d/domain.com.ssl.conf:62:  include /usr/local/nginx/conf/cloudflare.conf;
    

    When you have many vhost files in /usr/local/nginx/conf/conf.d/, the output would be more than one vhost file

    But this only is for after a vhost is created not during creation. I can easily add a persistent /etc/centminmod/custom_config.inc variable to control if vhosts are created with cloudflare.conf include enabled or disabled at vhost creation time. I'll have this added to 123.09beta01 branch within a few minutes/hours if you want to wait :)
     
  6. eva2000

    eva2000 Administrator Staff Member

    53,826
    12,160
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,712
    Local Time:
    4:48 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    @neverminder

    you might want to use this newly added variable VHOSTCTRL_CLOUDFLAREINC to enable csfcf.sh cloudflare.conf include in newly created Nginx vhost config files as outlined at csfcf.sh - automate Cloudflare Nginx & CSF Firewall setups. In persistent config file /etc/centminmod/custom_config.in set
    Code (Text):
    VHOSTCTRL_CLOUDFLAREINC='y'
    

    Once set, just create nginx vhost normally via centmin.sh menu option 2, 22 or nv command.

    You would of needed to update Centmin Mod 123.09beta01 code first via SSH command
    Code (Text):
    cmupdate
    

    or via centmin.sh menu option 23 submenu option 2 then exit centmin.sh

    Full details at Beta Branch - add VHOSTCTRL_CLOUDFLAREINC & VHOSTCTRL_AUTOPROTECTINC variables
     
  7. neverminder

    neverminder Member

    44
    3
    8
    Nov 23, 2017
    Ratings:
    +5
    Local Time:
    8:48 PM
    1.13.6
    10.0.33
    Wow, that was fast! Thank you.
     
  8. BamaStangGuy

    BamaStangGuy Active Member

    668
    192
    43
    May 25, 2014
    Ratings:
    +272
    Local Time:
    12:48 PM
    Is there any chance in the future to be able to have a custom default vhost template that will not be overwritten? For my redirect server it would be nice to have everything auto generated exactly how I need it.
     
  9. eva2000

    eva2000 Administrator Staff Member

    53,826
    12,160
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,712
    Local Time:
    4:48 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    yeah eventual plan is to have such a feature :)