Can I serve one django app from 2 different apache vhosts? - django

I've got a django app that's currently available at dev.mydomain.com, and I'm about to move it to clientsdomain.com. I'm on Ubuntu so I'll run a2dissite dev.mydomain.com and then a2ensite clientsdomain.com.
My vhost files are identical except for the server name -
<VirtualHost 8.8.8.4>
ServerName dev.mydomain.com
#....
</virtualHost>
and
<VirtualHost 8.8.8.4>
ServerName clientdomain.com
#....
</virtualHost>
(obviously that's not my ip address)
I just want to know if I actually have to take down the dev vhost before I run my app from the live vhost. Can I run them both together? Are there any risks to having them up at the same time (if that's even possible).

Each Django application will serve requests coming in to its corresponding VirtualHost. So no, in theory they can run in parallel.
However, you don't get into much detail about your setup. For example, are they backed by the same database? In this case, you do realize the problem, right?

Related

django apps on nginx running separately from apache

I am installing production server for my django apps and I can not make it work. My configuration files can be found here.
Basically I have apache2 installed and running on port 80 for my php applications. I want to run my django apps on nginx with uwsgi, apart from apache2. So I am running nginx on port 8000.
When I open http://IP:8000/ I can see my site properly.
1. But how do I set it up with domain name?
I've set A tag in dns to IP. Now it hits apache2 "it works" page because it hits port 80 on default? So I need to proxy pass all requests to my domain.com? Something like this?
/etc/apache2/sites-enabled/domain.com:
<VirtualHost *:80>
ServerName domain.com
ProxyPreserveHost On
ProxyPass / http://IP:8000
</VirtualHost>
It does not work, so how do I pass all domain requests from apache to nginx?
2. How do I add another domain name? (new app)
Do I just create new socket file for new app, keep it on port 8000 and nginx will decide depending on domain name what conf file to use?
I have not found any similar tutorials, nginx usually handles static files and sends requests to apache2. However I want it other way.
Thanks for your answer. To make it work I had to set apache proxy like this:
<VirtualHost *:80>
ServerName www.domain.com
ProxyPreserveHost On
ProxyPass /static http://XX.XX.XX.XX:8000/static
ProxyPassReverse /static http://XX.XX.XX.XX:8000/static
ProxyPass / http://XX.XX.XX.XX:8000
ProxyPassReverse / http://XX.XX.XX.XX:8000
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.(?!\.css|js|gif|png|jpg|ico))*$
RewriteRule /(.*) http://XX.XX.XX.XX:8000/$1 [P,L]
</VirtualHost>
and enable proxy_http:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo service apache2 restart
1. How do you set it up with domain name?
In the server block of your nginx conf file set the server_name:
server {
listen 8000;
server_name www.my-django-domain-one.foobar;
#rest of your config regarding forwarding to django...
}
Your site will be available at http://www.my-django-domain-one.foobar:8000.
2. How do you add another domain name? (new app)
Nginx will not decide anything based on the conf filename. Create a new conf file or use the existing one (only matters in the sense of how you want to organize your configs)
server {
listen 8000;
server_name www.my-django-domain-two.foobar;
#rest of your config regarding forwarding to django...
}
However, I recommend an approach involving only one web server. Opinions on which to use vary of course but both of them can do what you want to achieve on their own. You add unnecessary complexity to your setup (e.g. two servers to keep patched) and -depending on your traffic- it may even have a significant impact on your performance.
Check out this tutorial to see how you could make your php apps work with nginx and php-fpm.

Route a specific path to a specific EC2 instance using Route 53

I'm not sure if this is even possible, but if so I'm looking for the best way to do it.
Say I want to host my blog for example.com on it's own EC2 instance, and I want the path to my blog to be example.com/blog
Is it possible to route all requests to example.com/blog/* to one instance, and all other requests to that domain elsewhere?
My web server is Apache.
Thanks!
You can now do this with Application Load Balancer and path-based routing: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/tutorial-application-load-balancer-cli.html#path-based-routing-aws-cli
Certainly it's possible, but not with DNS nor with an ELB. The most common solution to this is to use a web server that issues a 301 or 302 redirect.
In your case, example.com would point to whatever the main site is. The web server (nginx or Apache httpd, perhaps) hosting example.com would have a redirect for example.com/blog/* that is found at another destination.
Here's an SO post on using Nginx for a redirect and for using Apache for a redirect.
Yes, but you would have to proxy your requests through an instance handling example.com. How you configure this depends on your web server.
Some examples on how to configure this:
nginx: http://wiki.nginx.org/HttpProxyModule
Apache: http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
Since you are using Apache2 Server, so you can achieve this very easily by creating a Virtual Host.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/vchost1.com.conf
Create Virtual Configuration using the above command, it copies the complete code of content from the default file provided by the apache2 Server.
sudo nano /etc/apache2/sites-available/vchost1.com.conf
start configuring host & domain according to your requirements
<VirtualHost *:80>
ServerAdmin admin#domain.com
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
here also, you can multiple Virtual Host configurations in a single file, start configuring and enjoy hacking.
you can also multiple web applications in a single instance by using the same method and theis reference.
https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-18-04-quickstart

multiple domains, multiple django projects, one server

Hi I'm new to apache and mod_wsgi and am trying to figure out how to configure both so that i can have www.example1.com use djangoproject1 and www.example2.com use djangoproject2.
I followed this tutorial to hook up example1.com to djangoproject1 and it works beautifully, but the tutorial doesn't exactly give the most detailed explanations for what is going on and why i need to do certain things.
what i have so far:
1) a dns zone for example2.com pointing to the server ip
2) installed python environment and django and started a new django project for djangoproject2 per the instuctions on the tutorial
I'm pretty sure i'm going to have to create a new wsgi config file and add a site configuration in /etc/apache2/sites-available/ called example2.com and then enable it, but i'm not sure about what else i'd need to do.
You can have 2 virtual hosts in your apache configuration.
The first virtual host on (default) - port 80
second one on port 81
In the virtual host, you can specify it this way:
NameVirtualHost *:80
<VirtualHost *.80>
DocumentRoot django_project_1_path
.... other config
WSGIScriptAlias / path_to_wsgi_config_1
</VirtualHost>
NameVirtualHost *:81
<VirtualHost *.81>
DocumentRoot django_project_2_path
.... other config
WSGIScriptAlias / path_to_wsgi_config_2
</VirtualHost>

django + wsgi + multiple sites

I have a django-app using sites framework and need to deploy in a single apache virtual host. I was using mod-python with PythonInterpreter and it was working fine. But mod-python is deprecated and I want to migrate to wsgi. But I can figure out how to configure this scenario using wsgi. Can anyone help me? I guess just using WSGIScriptAlias for each site is not working because it is running on same python interpreter.
Can anybody help me?
The default for WSGIApplicationGroup is %{RESOURCE} which expands to include the value of ServerName and the application mount point. Thus each distinct virtual host would be in separate sub interpreter by default.
This means that if you have multiple VirtualHost definitions with different ServerName setting then they would be distinct.
In general though, it would be better to create a distinct daemon process group for each site and delegate each to a different set of processes. This is done using WSGIDaemonProcess and WSGIProcessGroup directives.
When each site is in a separate daemon process group, then often better to set WSGIApplicationGroup to %{GLOBAL} so using main interpreter in process as that avoids some issues with third party C extension modules for Python which aren't written so as to work in sub interpreters.

How to configure Apache httpd to set multi-site for multi-user?

I have a system CentOS with Django and MySQL.
/home/user1/
-/vhost/*.conf
-/webapps/
abc.com
xyz.com
/home/user2/
-/vhost/*.conf
-/webapps/
123.com
789.com
Thanks for help!
UPDATE 1:
In httpd.conf:
User user1
Group apache
And httpd cannot access /home/user2/webapps.
You'll want to read up on Apache Vhost Configuration.
Tell us what you tried, what is working, what is not working, or what you don't understand.
Most common configuration
If your system has 1 IP address for all sites, you're going to want something like this:
#This is your 'default' host,
#people accessing the site via IP-address will see this site.
<VirtualHost *:80>
ServerName abc.com
ServerAlias xyz.com
DocumentRoot /home/user1/webapps/
</VirtualHost>
#This is the 123 and 789 vhost
<VirtualHost *:80>
ServerName 123.com
ServerAlias 789.com
DocumentRoot /home/user2/webapps/
</VirtualHost>
You can add all directives as explained by the <VirtualHost> Directive
EDIT: Updated question
There is a module called
perchild for apache that allows
to run different threads under
different users, configurable by
vhost. The module is not considered
functional, and not under current
development. Unless you know what
you're doing or are willing to take
the risk, you probably shouldn't use
it.
Another solution might be mpm-itk,
which appears to be more reliable.
Read this serverfault question
for more information, or visit their
homepage.
If you don't want to use either of both modules, you could always create a new user and group, run both vhosts under that user, and make both homedirs readable to the user by setting their group to the new group.