Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mikromike's avatar

how to convert sudo a2ensite to Laravel console command without sudo?

Hello

I tried to convert sudo a2ensite domain.com to Laravel console command without sudo.

  try {

     $process = new Process(['a2ensite']);  <---- if there is sudo, it will ask password.
     $process->setInput($apacheConfDir.$sitename.'.conf');
     $process->run();

    //    Process::input( )->run(['sudo', 'a2ensite']);
               $this->info("Apache configuration enabled site: {$sitename}");
   } catch  (\Exception $e)   {
               $this->error("Failed to enable new site: {$sitename}  " . $e->getMessage());
   }

/etc/sudoers

  laraveluser ALL=(ALL) NOPASSWD: /usr/sbin/a2ensite [A-z]*

Thanks Mika.

0 likes
2 replies
LaryAI's avatar
Level 58

To create a Laravel console command that runs a2ensite without requiring sudo, you can leverage the symfony/process component, which is included with Laravel. You've already set up the /etc/sudoers file to allow laraveluser to run a2ensite without a password, which is a good start.

Here's how you can create a Laravel Artisan command to enable an Apache site configuration:

  1. Generate a new command using Artisan:
php artisan make:command EnableApacheSite
  1. Edit the generated command file, typically located in app/Console/Commands/EnableApacheSite.php, to look something like this:
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class EnableApacheSite extends Command
{
    protected $signature = 'apache:enable {site}';
    protected $description = 'Enable an Apache site configuration';

    public function handle()
    {
        $sitename = $this->argument('site');
        $apacheConfDir = '/etc/apache2/sites-available/'; // Adjust if necessary

        $process = new Process(['sudo', 'a2ensite', $sitename]);
        $process->setWorkingDirectory($apacheConfDir);

        try {
            $process->mustRun();
            $this->info("Apache configuration enabled site: {$sitename}");
        } catch (ProcessFailedException $exception) {
            $this->error("Failed to enable new site: {$sitename} - " . $exception->getMessage());
        }
    }
}
  1. Now, you can run your command using Artisan:
php artisan apache:enable domain.com

This command will execute sudo a2ensite domain.com without asking for a password, assuming that the laraveluser has been properly configured in the /etc/sudoers file as you've shown.

Remember to replace laraveluser with the actual username that will run the command and ensure that the user has the necessary permissions to execute a2ensite without a password prompt.

Also, make sure that the apacheConfDir variable points to the correct directory where your Apache site configurations are stored.

Lastly, ensure that your Laravel application has the necessary permissions to invoke sudo commands. If you're running the application with a different user, you'll need to adjust the /etc/sudoers file accordingly.

Snapey's avatar

also make sure that your laravel user is allowed to write to the sites-enabled folder

Please or to participate in this conversation.