这只是一个快速演练,描述了如何设置一个体面的开发环境,允许轻松设置多个站点。它已经假定您已经安装并配置了PHP,MySql和
Apache已经运行的Debian或Ubuntu操作系统。你还需要一个有效的sudo。

虽然其中一些东西是Debian / Ubuntu特定的,但将它应用于任何其他发行版并不困难。

我们需要做的第一件事是创建一个'www'组。该群组中的任何人都可以创建新网站。

sudogroupaddwww

把自己放在这个群体中。

sudogpasswd–ausernamewww&&newgrpwww

现在,我们需要确保我们所有站点的存储位置都由www拥有和写入。

sudochown:www/var/wwwsudochmodg+ws/var/www

chmod的s开关设置粘滞位并确保在/ var / www中创建的所有目录也属于www组。

我们需要确保www可以写入保存vhost配置的目录。

sudochown:www/etc/apache2/sites-*

接下来,我们将创建模板vhost配置。我们将使用它来为我们的每个站点生成配置。

<VirtualHost*:80>ServerName{SITE}.localDocumentRoot/var/www/sites/{SITE}.local/htdocsDirectoryIndexindex.php<Directory/>OptionsFollowSymLinksAllowOverrideAll</Directory><Directory/var/www/sites/{SITE}.local/htdocs>OptionsIndexesFollowSymLinksMultiViewsAllowOverrideAllOrderallow,denyallowfromall</Directory>ErrorLog/var/www/sites/{SITE}.local/logs/error.logLogLevelwarnCustomLog/var/www/sites/{SITE}.local/logs/access.logcombined</VirtualHost>

将其保存为/etc/apache2/sites-available/example.conf

现在,让我们创建一个简单的测试站点。

mkdir-p/var/www/sites/foo.local/{htdocs,logs}echo'<?phpphpinfo();?>'>/var/www/sites/foo.local/htdocs/index.phpcd/etc/apache2/sites-availablesed-e's/{SITE}/foo/g'example.conf>foo.confsudoa2ensitefoo.confsudo/etc/init.d/apache2restartecho'127.0.0.1foo.local'|sudotee-a/etc/hosts

这里有一些命令,但它非常简单:

The first line creates the minimal required directory structure for a new web site.
We then create an index.php file which has a call to phpinfo() in it.
Next we move into the directory where our vhost configs are stored.
We then create a new vhosy config called foo.conf which is a copy of the example.conf from above, we use sed to replace all instances of '{SITE}' with 'foo'
Next, enable this vhost. This adds a symlink within /etc/apache2/sites-enabled
Restart Apache.
Add foo.local to our hosts file. This enables the url http://foo.local to resolve to our local machine and hence, be served by Apache.

这是真的。你可以在这个基本想法上建立相当多的东西。我在多用户系统中实现的一件事是向'www'组的成员提供新命令。这些命令主要是
围绕需要sudo的东西的包装器,但它确实使得从最终用户的角度来看整个过程更加清晰。考虑到机会和兴趣,我可能会在另一个教程中详细介绍相关细节。

有一点需要注意。如果您希望网站中的目录可由Apache写入,则必须使它们属于www-data组,并且该组将需要写入权限。例如;

mkdir-p/var/www/sites/foo.local/uploadssudochown:www-data/var/www/sites/foo.local/uploadssudochmodg+w/var/www/sites/foo.local/uploads