Greetings Placed at the request of a friend article.
He unfortunately has no registration at the hub yet, but I have little karma to send an invitation.
If you have the opportunity and liked the article, send him an invite, or you can try to raise my karma so that I could send him an invite.
So the task:
Organize convenient administration of a multi-domain web server.
System model: linux -> nginx -> apache -> php -> mysql.
I used to have separate configs for nginx and apache for each domain,
and to add a new host, you had to add at least 2 config files.
')
The work was organized conveniently. In fact, there were 2 scripts. 1 to add a host to nginx, another for apache. (there is still a script to add a host to bind, but that's another story).
But even this was not convenient in terms of adding deleting hosts ... sometimes I forgot to register nginx, sometimes something was wrong in the Apache ...
It was decided as follows:
It is a rule to introduce the following server structure:
/ home / htdocs / [domain name] / www
And given this fact, universal configs for nginx and apache were developed:
nginxuser www-data;
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
# log options
log_format main '$host: $remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
# nginx options
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65 20;
# fastcgi
#fastcgi_intercept_errors on;
server {
listen 80;
location / {
proxy_pass 127.0.0.1:81/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
# Static files location
location ~* \.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf)$
{
if (!-d /home/htdocs/$host/www ) {
set $cur_host "default";
access_log '/var/log/nginx/default.log' main;
}
if (-d /home/htdocs/$host/www ) {
set $cur_host $host;
access_log '/var/log/nginx/host_access.log' main;
}
root /home/htdocs/$cur_host/www;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
}
Moreover, in the case of host nondetection, the data is taken from / home / htdocs / default / www
Universal VirtualHost for apache:
NameVirtualHost *
<VirtualHost *>
ServerAdmin support@gmail.com
UseCanonicalName Off
VirtualDocumentRoot /home/htdocs/%-2+/www
php_admin_value auto_prepend_file /home/htdocs/fix_doc_root.php
DirectoryIndex index.php index.html index.htm
<Directory />
Options FollowSymLinks
AllowOverride All
<Directory / home / htdocs />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow, deny
allow from all
ScriptAlias / cgi-bin / / usr / lib / cgi-bin /
<Directory "/ usr / lib / cgi-bin">
AllowOverride None
Options ExecCGI -MultiViews + SymLinksIfOwnerMatch
Order allow, deny
Allow from all
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
Alias / doc / "/ usr / share / doc /"
<Directory "/ usr / share / doc /">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny, allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 :: 1/128
Based on a standard virtualhost template.
special attention to the following:
1. it is necessary to connect the vhost_alias module
# a2emod vhost_alias
2. VirtualDocumentRoot / home / htdocs /% - 2 + / www
% -2 + means essentially the last 2 parts of the domain name from the request.
those. if the request goes to sub.domain.ru then DocRoot will be /home/htdocs/domain.ru
if you need the full name of the requested domain, simply replace% -2 + with $ 0
3. Everything would be great, but with this configuration, Apache gives the wrong $ _SERVER ["DOCUMENT_ROOT"] to the scripts !!!
To correct this annoying thing, we use the directive
php_admin_value auto_prepend_file /home/htdocs/fix_doc_root.php
The contents of the script /home/htdocs/fix_doc_root.php:
<?
$l = explode("/",$_SERVER['SCRIPT_FILENAME']);
$DOCUMENT_ROOT = $_SERVER["DOCUMENT_ROOT"] = implode("/",Array( $l[0], $l[1], $l[2], $l[3], $l[4]));
?>
Thank you for attention.
This post is posted at my request. I myself have no registration. If you liked the article - glukas.lss@gmail.com.
Sergey Lyapko