Synology

This page contains some lessons learned when configuring my Synology NAS

The Synology name is trademark of Synology, Inc., registered in the Republic of China (Taiwan) and other regions

Handle Url by Script

modified 16-July-2023 18:09:54

Finding the confiugration files for each virtual host (DSM 7.2)

This article is about DSM 7.2 (seriously changed since this version)/p>

To handle a url of the webserver by a PHP dispatcher script you will have to add configurations to the web server. On Synology you are not allows to modify system configuration files, so we have to create a new configuration for each web site.

Identify Guid of each website

First identify the guid of the website(s) you want the script to be active. In a terminal session to the server enter:

grep  "server_name \|include conf.d/.service." /usr/local/etc/nginx/sites-enabled/webservice_portal_*

This will output the configuration file per virtual host

        server_name www.homecraft.nl ;
        include conf.d/.service.0fac9001-d036-4f13-81af-2381e8d30bb3.092e61cf-32f7-4aec-80da-02f88db41379.conf*;
        server_name homecraft.nl ;
        include conf.d/.service.f8ffa3d0-67f6-4dba-85d3-29daafc59efa.092e61cf-32f7-4aec-80da-02f88db41379.conf*;  

The second stage of the configuration shows which extra configuration file each website uses:

grep  "root \+\"\|include /usr/local/etc/nginx/conf.d/.\+user.conf" /usr/local/etc/nginx/conf.d/.service.*.conf

This for instance outputs

                root    "/volume1/web/homecraft.nl";
                include /usr/local/etc/nginx/conf.d/092e61cf-32f7-4aec-80da-02f88db41379/user.conf*;
                root    "/volume1/web/homecraft.nl";
                include /usr/local/etc/nginx/conf.d/092e61cf-32f7-4aec-80da-02f88db41379/user.conf*;
        

Note that www.homecraft.nl and homecraft.nl will be defined separately, but point to the same user configuration.

Configure script

For each website add user configuration by opening the text editor:

sudo mkdir /usr/local/etc/nginx/conf.d/092e61cf-32f7-4aec-80da-02f88db41379
sudo vi /usr/local/etc/nginx/conf.d/092e61cf-32f7-4aec-80da-02f88db41379/user.conf

For instance, we specify a script blog.php that is in the subdirectory blog of website www.homecraft.nl, that is stored at /var/services/web/homecraft.nl

if ($scheme != "https") {
     rewrite ^ https://$host$request_uri permanent;
}
location ~ ^/blog/content/.*$ {
     deny all;
     return 404;
}
location ~ ^/blog/.*$ {
        root /var/services/web/homecraft.nl;
        try_files $uri /blog/blog.php$is_args$args;
}
location ~ ^/$ {
        root /var/services/web/homecraft.nl;
        try_files $uri /blog/blog.php;
}

The condition redirects the non-https site to the https site; next the section with the 'deny all' and status 404 is to disable direct access to the system files in the /blog/content directory that should only be viewed by the dispatcher script.

To reload the configuration restart the webserver:

sudo nginx -t
sudo nginx -s reload

Finding the confiugration files for each virtual host (DSM 6)

This article is about DSM 6.1.6 until 6.2.4

To handle a url of the webserver by a PHP dispatcher script you will have to add configurations to the web server. On Synology you are not allows to modify system configuration files, so we have to create a new configuration for each web site.

Identify Guid of each website

First identify the guid of the website(s) you want the script to be active. In a terminal session to the server enter:

grep  "server_name\|include /usr/local" /etc/nginx/app.d/server.webstation-vhost.conf

This for instance outputs

        server_name www.homecraft.nl;
        include /usr/local/etc/nginx/conf.d/c26ab877-1e0e-4232-94f6-0cf9edd41e6e/user.conf*;        
        server_name homecraft.nl;
        include /usr/local/etc/nginx/conf.d/e8ffa3d0-67b6-4dba-85d3-29daafc59efa/user.conf*;        

Note that www.homecraft.nl and homecraft.nl will be defined as a separate website, with a different guid.

Enable mail from PHP

modified 16-July-2023 18:09:20

To enable mail in PHP there are two alternatives

  • Enable Synology notifications to enable smtp
  • Install Mail Server

Enable synology notifications to enable smtp

Go to Configuration / Notifications and enable email notifications and specify the smtp server etc.

Then you can send emails from PHP:

$msg = "<style>* {font-family: \"Verdana\"; font-size:11px;}</style>".
            "<h1>Test email</h1><p>Test email</p>";
$headers = "From: somebody@xs4all.nl\r\n" .
        "Reply-To: somebody@xs4all.nl\r\n" .
        "MIME-Version: 1.0\r\n".
        "Content-Type: text/html; charset=ISO-8859-1\r\n".
        "X-Mailer: PHP/" . phpversion();
mail("somebody@xs4all.nl", "Test email", $msg, $headers);

Note: The above script will only work for php 7, not php 8.

Install Mail Server

Note: I did not manage to get it to work yet

Install MailServer

Configure SMTP and SMTP relay

Log in the the Synology server with terminal

You can send a test email using:

echo "Subject: sendmail test" | sendmail -v somebody@xs4all.nl

Next create the following files

/etc/php/conf.d/custom.ini
/usr/local/etc/php56/conf.d/custom.ini
/usr/local/etc/php70/conf.d/custom.ini
/etc.defaults/php/conf.d/custom.ini

all with contents:

[mail function]
sendmail_from = fsomebody@xs4all.nl
sendmail_path = /var/packages/MailServer/target/sbin/sendmail -t

Next restart webserver: sudo synoservicecfg --restart nginx

Note: I did not manage to get it to work yet