This content originally appeared on DEV Community and was authored by Abdallah Samy
as we know in saas apps you may need to specify subdomain for each client
but as saas apps are big and you will change lots of code.
today I tried to do this without changing code as per business needs.
steps :
- configure DNS provider.
- configre nginx
- add wildcard SSL certificate
- change some code in app router.
- try opening your app using web browser
Details
- firstly lets go to DNS provider and add A record for wildcard subdomains
2.open nano /etc/nginx/sites-available/sitename
and add this details
server {
server_name ~^(?<account>.+)\.abdallhsamy.io\.io$;
index index.html;
root /var/www/$account;
listen 80;
access_log /var/log/nginx/$account-access.log;
error_log /var/log/nginx/$account-error.log;
}
then run this command to check whether everything is ok
sudo nginx -t
output should be
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
if so, then run this command to restart nginx service
sudo systemctl reload nginx
3 . add wildcard ssl certificate using letsecrypt
sudo certbot certonly \
--agree-tos \
--email someone@abdallahsamy.io \
--manual \
--preferred-challenges=dns \
-d *.abdallahsamy.io \
--server https://acme-v02.api.letsencrypt.org/directory
output should lookslike this
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/abdallahsamy.io/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2021-07-18. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
if so go to step 2 and add the certificate to subdomains
now we will remove listen 80
and add
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/abdallhsamy.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/abdallhsamy.io/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
then code shloud be :
server {
server_name ~^(?<account>.+)\.abdallhsamy.io\.io$;
index index.html;
root /var/www/$account;
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/abdallhsamy.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/abdallhsamy.io/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
access_log /var/log/nginx/$account-access.log;
error_log /var/log/nginx/$account-error.log;
}
again we should restart nginx
sudo systemctl reload nginx
- finally you need to change some routes in your web app
any my case I use laravel
in
\App\Providers\RouteServiceProvider
class I added this code inmap
function
Route::middleware('api')->group(base_path('routes/subdomains.php'));
then I created this file to isolate new routes
touch routes/subdomains.php
then I added some redirects to application routes
<?php
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Redirect;
/* @var Router $router */
$router->domain('{account}.abdallahsamy.io')->group(function(Router $router) {
$router->get('/', function($account) {
return Redirect::to("/api/v2/accounts/$account");
});
$router->get('{uri}', function($account, $uri = '') {
return Redirect::to("/api/v2/accounts/$account/$uri");
});
});
now you could open your preferred web browser and browse anything.yourdomain.com
feel free to rewrite my article as I know me is not good at expressing
This content originally appeared on DEV Community and was authored by Abdallah Samy

Abdallah Samy | Sciencx (2021-04-17T14:48:57+00:00) How do I convert my web apps to multi-tenant in less than 1 minute. Retrieved from https://www.scien.cx/2021/04/17/how-do-i-convert-my-web-apps-to-multi-tenant-in-less-than-1-minute/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.