📜 ⬆️ ⬇️

directadmin + nginx and how to make friends with them

As you know, apache is a good thing, but its hardness greatly limits the possibilities.
To solve the problem, a bunch of frontend + backend is used. Apache acts as a backend server, and any other one is fake as a frontend server. In this article we will look at a bundle in which nginx acts as a front end server.
The Internet is full of articles on how to configure nginx, but I have not come across any of how to make nginx work along with the directadmin control panel.


How it was.


The task was, but solutions did not immediately come to mind. A little googling, several solutions appeared:
1) wrap up listening 80 port on some other, and there already listen to nginx `th. I didn’t like this decision because it imposes certain playability.
2) somehow make the Apache listen to another port (not 80), and there everything is already simple.

If, in the usual situation, when we do not have a hosting control panel, everything is simple - go and handle the appha's configs, then with the control panel, it will not get so. Each time after a small change in the settings of the site through the directive, the entire section is copied, the whole section of the Apps folder, related to the changed site.
')

Enlightenment


Having climbed a bit in the directory in which I installed directadmin, and this: / usr / local / directadmin, it turned out that there are templates for which virtual hosts are created. Voila ... Happily there were no limits. It turns out all the easier than it could be!

Implementation


You need to copy the files from the $ DIRECTADMIN_HOME / data / templates / directory related to the virus hosts to the $ DIRECTADMIN_HOME / data / templates / custom directory.
This is done in order to update the directadmin version, the templates you changed were not overwritten by the standard ones.

#cd $ DIRECTADMIN_HOME / data / templates /
#cp virtual_host.conf custom / virtual_host.conf
#cp virtual_host2.conf custom / virtual_host2.conf
#cp virtual_host_sub.conf custom / virtual_host_sub.conf
#cp virtual_host2_sub.conf custom / virtual_host2_sub.conf

Now in the files we copied, we need to fix the port. In each file, the line:
<VirtualHost | IP |: 80>
replaced by:
<VirtualHost | IP |: 8181 >
Now apache will listen to virtual hosts on port 8181. And so that before it was delivered traffic, we will take care. More precisely take care of this nginx.

Nginx installation


This is done elementary.
You can install it from the repositories (but I would not advise doing this):
# apt-get install nginx
Why would not advise installing from repositories? Because the release of new stable versions occurs very often, and it is better to download the new version from the developer's site.
At the time of this writing, the last stable version was 0.6.35, and the latest current version is 0.7.42, and version 0.4.13 is available for comparison in repositories.

So, let's start the installation. I describe the process in brief, or rather even say only in commands, since a more detailed description you can easily find on the following link .

#mkdir / root / nginxsrcs && cd / root / nginxsrcs
#wget http://sysoev.ru/nginx/nginx-0.7.42.tar.gz
#tar zxf nginx-0.7.42.tar.gz
#cd nginx-0.7.42
#. / configure
#make
#make install
#cd / usr / local / nginx / conf
#vim nginx.conf

We need to configure nginx as a caching proxy server.
You ask, why not give statics (pictures, JS files, html) without apache? Everything is simple - nginx does not know how to work with ".htaccess" (to be honest, I am only glad to this). But you can customize as you prefer.

Further config, with a note of key places:

worker_processes 3;
pid logs/nginx.pid;
events {
worker_connections 1024;
}

http {
access_log off;
error_log off;
include mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
server {
access_log off;
error_log off;
listen 1.2.3.4:80; # IP:port
server_name *.*; # - . apache

charset windows-1251;

location /
{
proxy_pass h ttp://1.2.3.4:8181/; # apache.
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_max_body_size 10m;
client_body_buffer_size 8k; #128k

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}


Save the config and try to run nginx.
If you are already running apache, then most likely nginx will not start, since port 80 is occupied by apache.

You need to fix the file /etc/httpd/conf/httpd.conf.
The line “Listen 80” should be replaced with “Listen 8181”.
And "NameVirtualHost 1.2.3.4:80" on "NameVirtualHost 1.2.3.4:8181".
Restart apache. Run nginx.
Voila! Everything is working. We can contact any virt-host.

Real IP


We can notice that the client IP will be 1.2.3.4.
This happens due to the fact that the real IP of the client receives nginx, and when the traffic forwards to apache, then it already comes from our internal IP.
But it's not a problem. For apache, there is a wonderful mod_rpaf module.
It is installed elementary.

#cd / root / nginxsrcs
#wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
#tar xzf mod_rpaf-0.6.tar.gz
#cd mod_rpaf-0.6
#apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

Create a mod_rpaf.conf configuration file:
LoadModule rpaf_module /usr/lib/apache/mod_rpaf-2.0.so
RPAFenable On
RPAFsethostname Off
RPAFproxy_ips 127.0.0.1 1.2.3.4
RPAFheader X-Real-IP


Restart apache.

That's it, now we have a fully working bundle of nginx + apache + directadmin.

Source: https://habr.com/ru/post/59809/


All Articles