0

I need to uncomment the existing text inside the default file

the file in this site is very similar to my default file

https://salsa.debian.org/nginx-team/nginx/blob/master/debian/conf/sites-available/default

the differance is that because i use https certificate I have in the default file another section of server{} and inside of it i have another occurrence of this next part... so I need to find in the first server{} section and add info.php to the line with the arrow I've created for demonstrating in which line i want to add info.php

server {

    # Add index.php to the list if you are using PHP
    --> index index.html index.htm index.nginx-debian.html;

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
            #include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
            #fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
            #fastcgi_pass 127.0.0.1:9000;
            #include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
            #deny all;
    #}

    # Virtual Host configuration for example.com
    #
    # You can move that to a different file under sites-available/ and symlink that
    # to sites-enabled/ to enable it.
    #
    #server {
    #       listen someport;
    #       listen [::]:someport;
    #
    #       server_name example.com;
    #
    #       root /var/www/example.com;
    #       index index.html;
    #
    #       location / {
    #               try_files $uri $uri/ =404;
    #       }
    #}

}

and then search at the next server{} section and in this part I need to change this:

server {

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name mydomainname.com; # managed by Certbot

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
            #include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
            #fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
            #fastcgi_pass 127.0.0.1:9000;
            #include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
            #deny all;
    #}
    listen [::]:someport ssl ipv6only=on; # managed by Certbot
    listen someport ssl; # managed by Certbot

}

to this:

server {

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name mydomainname.com; # managed by Certbot

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;

    }
    # pass PHP scripts to FastCGI server
    #
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
            fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
            #fastcgi_pass 127.0.0.1:9000;
            include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
            deny all;
    }
    listen [::]:someport ssl ipv6only=on; # managed by Certbot
    listen someport ssl; # managed by Certbot

}

I've already tried sed but i'm not so good in the syntax of it because i get all kind of errors about the special characters in it can some one help me with this one?

4
  • Is it not possible to put both files as you want them on the system and then switch between the files? sed is not good for this kind of changes.
    – Rinzwind
    Jul 8, 2019 at 14:43
  • @Rinzwind thanks for replying, so what do you suggest? I'm open for a different solution... Jul 8, 2019 at 14:45
  • It's the same file but it has 2 server{} sections in it... Jul 8, 2019 at 14:50
  • @Rinzwind maybe you would have an answer to this... let's say i want to change all strings inside this file that have this string: try_files $uri $uri/ =404; and change it to this: try_files $uri $uri/ /index.php?$args; with sed I keep confusing the patterns in unix it's driving me nuts... Jul 11, 2019 at 23:50

1 Answer 1

0

Really, you can't ...

sed can match patterns and edit your data from that, but it really can't see the difference between comment and command lines.

Since it is such a small file, it is much easier to open it in some editor and do the editing manually - use vi, nano, gedit or whatever is your favorite.

3
  • ohh feels bad because I need to make an automated script to edit this file without a human involved in it it's the only option? Jul 8, 2019 at 14:37
  • let's say i want to change all strings inside this file that have this string: try_files $uri $uri/ =404; and change it to this: try_files $uri $uri/ /index.php?$args; I keep confusing the patterns in unix it's driving me nuts... Jul 11, 2019 at 23:46
  • @ShaharAlaluf that's another question.
    – Soren A
    Jul 12, 2019 at 7:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .