简体   繁体   English

从 Django 重新启动 Apache

[英]Restarting Apache from Django

I need my Django app to be able to restart the Apache server running it.我需要我的 Django 应用程序才能重新启动运行它的 Apache 服务器。 I'm using mod_python.我正在使用 mod_python。

Which is the easiest way?哪种方法最简单?

The only way this would be possible is if you gave your Django application root permissions, which is horribly unsafe.唯一可行的方法是,如果您授予 Django 应用程序 root 权限,这是非常不安全的。 But, assuming that your app is running as root, you can run但是,假设您的应用程序以 root 身份运行,您可以运行

import subprocess

subprocess.Popen(['/etc/init.d/apache2', 'restart'])

Or whatever command your distribution has for restarting Apache.或者您的发行版用于重新启动 Apache 的任何命令。 If the Django app is not root, you may be able to run it with sudo using the pexpect python library.如果 Django 应用程序不是 root 用户,您可以使用pexpect python 库通过sudo运行它。

import pexpect

child = pexpect.spawn('sudo /etc/init.d/apache2 restart')
child.expect('[sudo] password for .*:')
child.sendline(password)
child.interact()

Note that this way requires you to give the apache user the ability to run sudo which is also insecure.请注意,这种方式需要您为 apache 用户提供运行sudo的能力,这也是不安全的。 In general, I can't see a secure way to accomplish this.一般来说,我看不到实现此目的的安全方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM