diff --git a/dev/setup/nginx/setup_nginx.py b/dev/setup/nginx/setup_nginx.py index 1208e677317..17a97db4769 100644 --- a/dev/setup/nginx/setup_nginx.py +++ b/dev/setup/nginx/setup_nginx.py @@ -13,46 +13,39 @@ try: except Exception as e: print(f"Symlink creation message: {e}") +def remove_location_block(content, path): + search_str = f"location {path}" + start_idx = content.find(search_str) + if start_idx == -1: + return content + + brace_start = content.find("{", start_idx) + if brace_start == -1: + return content + + count = 1 + i = brace_start + 1 + while i < len(content) and count > 0: + if content[i] == '{': + count += 1 + elif content[i] == '}': + count -= 1 + i += 1 + + return content[:start_idx] + content[i:] + filepath = '/etc/nginx/sites-available/default' if os.path.exists(filepath): content = open(filepath).read() - # Remove old alias block if it exists - old_alias_block = r''' location ^~ /dolibarrbeta { - alias /home/ubuntu/dolibarrbeta/htdocs; - index index.php; - try_files $uri $uri/ /dolibarrbeta/index.php?$args; - - location ~ \.php$ { - include fastcgi_params; - fastcgi_param SCRIPT_FILENAME $request_filename; - fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; - } - }''' - content = content.replace(old_alias_block, '') + # Strip any existing /dolibarrbeta block + content = remove_location_block(content, '/dolibarrbeta') - # Remove old root-based block without timeout if it exists - old_root_block = r"""location /dolibarrbeta { - root /var/www/html; - index index.php; - try_files $uri $uri/ /dolibarrbeta/index.php?$args; - - location ~ \.php$ { - include fastcgi_params; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; - } - } - - """ - content = content.replace(old_root_block, '') - # Insert the new correct root-based location block with increased timeout - if 'location /dolibarrbeta' not in content: - idx = content.find('location / {') - if idx != -1: - block = r"""location /dolibarrbeta { + idx = content.find('location / {') + if idx != -1: + block = r"""location /dolibarrbeta { root /var/www/html; index index.php; try_files $uri $uri/ /dolibarrbeta/index.php?$args; @@ -66,12 +59,10 @@ if os.path.exists(filepath): } """ - content = content[:idx] + block + content[idx:] - open('/tmp/default', 'w').write(content) - print("Nginx config generated at /tmp/default") - else: - print("Error: Could not find 'location / {' in Nginx config") + content = content[:idx] + block + content[idx:] + open('/tmp/default', 'w').write(content) + print("Nginx config generated at /tmp/default") else: - print("Dolibarrbeta location block already configured in Nginx") + print("Error: Could not find 'location / {' in Nginx config") else: print(f"Error: {filepath} not found")