10

We can store PHP template files using blade templating engine in Laravel. But, I want to create a config file on a remote server having more than 20-30 lines each.

Till now, I was doing this using Perl. I used to execute Perl file that used to dump contents in one file and I used to pass variables as parameters.

Now, I want to do it without using Perl. I tried looking for a solution but failed. To make it easy to understand, Here is what I am trying to do exactly!

I want to create the following config file on a remote server (Just an example).

Example.conf

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
</VirtualHost>

Here, example.com and www.example.com will vary in every config file.

Now, I want to create this file from my laravel application to the remote server. Is there any way I can store template of this config file and can compile and put file on remote server?

I know how can I put this file on Remote server. I just want to know the best possible way to store template and customize it when needed.

Adarsh Sojitra
  • 2,059
  • 1
  • 27
  • 50
  • 1
    Just curious, do you want to manage configuration and automate deployment? because there are better tools for that, for example: Ansible, Terraform, Chef, Puppet... – Razor Apr 03 '18 at 20:23
  • Your question is not pretty clear, could you maybe add an example or a context to this. From what I understand of your question, @Razor is right, you should use appropriate tools to do this, what you're doing is looking like a bad design and it could lead to a major security issue – MathieuAuclair Apr 08 '18 at 00:01
  • @MauthieuAuclair example is already there. Read the question again please ... – Adarsh Sojitra Apr 08 '18 at 09:31
  • Think carefully about sanitizing/validating input for this feature. If a malicious user manages to gain access to an endpoint that writes the template, they can potentially rewrite the configuration to allow all kinds of nasty things. That said, it's a neat idea. We can also use Laravel's [`sftp` filesystem driver](https://stackoverflow.com/a/46432109/5209322) to automatically upload the output file to a remote server. – Cy Rossignol Apr 08 '18 at 22:38

2 Answers2

10

You can put it into blade template like server-config.blade.php and then when you want to place it on a a server you just call:

\File::put('place-on-the-server.conf', view('server-config')->render());

which will generate content based on the blade template (so you can pass variables to this template).

Filip Koblański
  • 9,718
  • 4
  • 31
  • 36
  • 1
    Is using a template engine really a good idea to use as a way to create server config files? – online Thomas Mar 01 '18 at 08:36
  • 2
    Why not? As you mentioned it's a "template" engine. It's design to create templates - not only frontend templates. – Filip Koblański Mar 01 '18 at 08:55
  • It feels like it's not right, however my "feelings" are not the law (thank god) so that's why I'm asking if it really is a good idea :) – online Thomas Mar 01 '18 at 09:13
  • 2
    What would not be a good idea is to give to `php-fpm` filesystem permissions to write the config file, but you may run it with `php-cli`, as the permission belongs to the user which run it (root for instance). – Gonzalo Apr 07 '18 at 12:49
  • I have found using Laravel's blade engine for generating configurations/files really helpful to manage my files. I decided to wrap the engine as a cli for anyone who finds it useful https://github.com/surgiie/blade-cli – surgiie Aug 19 '22 at 17:10
2

As of my understanding, is that in your senario is that you have a website where people can request websites and you want to auto-generate the virtual-host file inside your application and then transfer this to the remote server?

May the above be the case then you have a lot of options.

1 - The answer of Filip

2 - Console commands

You can generate your file anyway you like. I personally have a structure in my laravel applications where I save blank files (templates). I then copy it over, and replace it with given parameters over console command, Artisan Commands

3 - Server scripts

Another way could be using Laravel Envoy. You will have a script on the server that will generate the file needed, and just call a function in your Envoy file of laravel to have it execute it. The good part about this is that you can make a new user on the server specifically for envoy and only allow it to run specific commands. This way theres very little way of messing up a server.Envoy

killstreet
  • 1,251
  • 2
  • 15
  • 37