Apache control in multi site server configuration - php

I have an ubunto vm running a lamp stack, and a for now, one domain name redirecting to the vm's ip address.
Concept wise the main principle is I want users to register an account and get a "website" running over on a subdomain of my own domain. That part is easy with wildcard subdomains in apache vhosts etc and with certbot i managed to automatically have all subdomains protected under ssl.
Now, if a customer wants to buy his/her own domain name, with me or some other registrar they need to point an A record to my ip address and a CNAME from www to the domain name. And in my end I need to add a vhost file under sites-available folder configuring the virtual host file for that new domain name and restarting "gracefully" apache.
Here lies the problem. How can I manipulate apache vhosts files etc so I can accomplish this at the push of a button on my main website? I'm using PHP in my backend and doing it in php (shell exec etc) seems like a security risk..
Im running on GCP so any of GCP's services are available.
Thanks in advance.

If you are worried about a shell script in your server you can run an application using SSH from a different machine; It can be a small instance that an application can be something like a putty which is available only on windows. You should find something relevant for Linux as there are many alternatives available. Use SSH client application and Python language to automate it.
As the manual process involves running the same command multiple times you can create virtual host files and see if files are created in sites-available directory etc.
Once the new users register for your service you need to create virtual host files and run chown commands and see if the files are created in the sites from the directory and restart your apache. You can refer to the steps in Automating virtual host setup on your server.

Answering my own question:
Enabled mod_macros in apache;
sudo a2enmod macro
Added a macro for creating a http :80 virtual host and another for creating a https :443 virtual host with the necessary variables;
<Macro ProtectedVHost $domain>
<VirtualHost *:80>
ServerAdmin admin#email.com
ServerName $domain
ServerAlias www.$domain
DocumentRoot /var/www/customer_sites/$domain/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName $domain
ServerAlias www.$domain
ServerAdmin admin#email.com
DocumentRoot /var/www/customer_sites/$domain/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/$domain/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/$domain/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
</Macro>
Created a conf file out of my public_html folder on my main websites editable in php where i add records using my own macro as the user adds in his domain:
Use {Macro Name} {Domain Name}
Import that file into apache.conf
Finally i created a CRON that starts ever so often and reads that file and compares it to a saved version. If the files has changed, it will gracefully restar apache which will read and re-create the necessary virtual hosts.
Hope this helps anybody looking to do the same thing

Related

Magento Virtual Host

I am trying to setup virtual host for Magento, but its not working, I created magento.localhost.com.conf in the Apache2/sites-avaliable directory, this is my file :
<VirtualHost *:80>
ServerAdmin magento#localhost.com
ServerName magento.localhost.com
DocumentRoot /srv/www/magento_dev/public_html
<Directory /srv/www/magento_dev/public_html/>
Options Indexes FollowSymlinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /srv/www/magento_dev/logs/error.log
LogLevel warn
</VirtualHost>
I am following instructions from book actually ,also changed /etc/hosts file , I added 192.168.36.1 magento.localhost.com. at the end , but when I try to open the site, it says :
This webpage is not available
I enabled apache rewrite, give permission to all this folders ,basically did everything as instructions, but still not working. My os is Ubuntu 14.04, any tips ?
Follow recommendations in comments for a2ensite and for testing with a simple index.php file.
Also make sure to restart Apache.
When that works, then try with the Magento index.php file.
What you do next depends on if this is a brand new copy of Magento or a copy of a previously installed one. If it's new, you should see the setup page. Follow the instructions. If it's a copy, you will need to change the database core_config_data values relating to your website urls.
Edit:
You say
changed /etc/hosts file ,I added 192.168.36.1
On what machine did you change this? Also is your Magento running in a virtual machine on your Windows machine? And the VM machine has that IP?
Edit 2:
Also change your web server name to "Magento.mysite.com" or something like that. localhost has a special meaning! And it doesn't make sense if you are accessing a remote we server!
Check web server ip with ipconfig or ifconfig on the command prompt. Put that ip in the hosts file of the web browser machine.

Redirect Traffic to Different pages bases on the address used

I Have a webserver running on my raspberry pi and I am using it for multiple projects. I can easily enough access the different pages with something of the from ip-addrss\project-name.php. I was looking to eliminate the ip address and found I could set up the domain names on my router. so http:/projector or projector.local redirects to the raspberry pi. The problem is it always goes to the default page. I can setup http:/ProjectA and http:/ProjectB but they both go to index.php. is there a way in php to redirect based on the url used to get there. so index.php would redirect to projectA.php or projectB.php depending on which url was used? I looked through $_SERVER and $_POST but they didn't seem to have the right information. Some research lead me to believe apache could do this but I have experience configuring apache.
You COULD do it in PHP, by checking $_SERVER['HTTP_HOST'], but that value can be manipulated by who is making the request. So I can access http://ProjectA while specifying the headers host: ProjectB or similar, and you will think that it's ProjectB.
In fact, if you look at the HTTP request, HTTP_HOST is the only way one would determine the domain name. So it doesn't matter if you do it in PHP or Apache.
In Apache, you could do it by enabling vhosts mod for apache. If you're running linux, the command line might be something like this a2enmod vhosts_alias. This will allow you to configure different hosts, determined by the host HTTP header, and IP. Each virtual host points to an individual directory. You can have 2 hosts pointing at the same directory, but you'd have to modify the directory properties, something like this:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName ProjectA
ServerAlias www.ProjectA #you can skip this line if it doesn't apply
DocumentRoot /var/www/foo
<Directory "/var/www/foo">
DirectoryIndex ProjectA.php
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
I didn't try to configure the same directory differently for 2 different hosts. My instincts say that it should work, but it may not.
Here's a guide on how to configure virtual hosts on Ubuntu. https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts
I have no idea how different it is on Raspberry Pi. But the apache config files should have exactly the same syntax and rules. Only paths and commands might differ.

Cakephp 3 and Subdomain

Cakephp 3 and Subdomain
I have an application cakephp 3 inside the root of my server and need to create a subdomain that this also goes in the same root with the cake.
example:
bin
config
src
...
shop (Sub domain)
How have the cake in my root it does not allow access subdomio.
What can I do to get around this problem?
Because the file convention of cakephp is sometimes hard to get the result you want to.
Correct me if I'm wrong. Your situation looks like this?
www.maindomain.com, which run by 'src'.
But you want the sub.maindomain.com use other files outside the cakephp file structure
I think it's too much effort to work outside the file convention. I think this is more '.htaccess' and 'virtual hosts' issue.
I've found a link that maybe can you help you "CakePHP subdomains with htaccess"
UPDATE:
Also keep in mind the folder-permission issue when you try to fix this.
I think the best approach is to have a VirtualHost configured, I am assuming you use Apache, so that the shop folder isn't part of the root of the server. Here you can see how to do it:
Move shop folder to outside Apache root so it's not part of the main website. If it's in /var/www/html/shop move it to /var/www/
Add a VirtualHost to Apache httpd.conf file or add it as shop.conf into /etc/apache2/sites-available/ assuming your server is an Ubuntu 14.04, you must know where to save the file according to your distro.
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
ServerName shop.local
ServerAlias shop.local
ServerAdmin alejandro#ulfix.com
DocumentRoot /var/www/shop/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
Optional: Edit /etc/hosts file so that you can reach the new subdomain if it is in a VM o you can't resolve it from your computer:
192.168.100.14 shop.local # Change IP to your Server's IP
Enable site in Apache (In Ubuntu you'll need to run: sudo a2ensite shop.local) and restart Apache Server.
See CakePHP 2.x virtual host file for Apache2 for reference.

How can you configure Apache to run CakePhP and raw PHP Apps as separate virtual hosts running side-by-side on the same server?

On my CentOS Linux VPS I have full LAMP installed. I have a CakePHP website (and intend to add a few more) as a virtual host. I also have a web based bug tracker called MantisBT that is implemented in PHP. I'd like to set up my Apache configuration file (/etc/httpd/conf/httpd.conf) to index both side-by-side. My websites are not in DNS. I access them using the raw IP number.
Here's my directory structure:
/var/www/
MyCakePhPApp/
html/
mantisbt/
Here's what I have in my httpd.conf file:
# This makes my MantisBT work under http:/<my_ip>/mantisbt
DirectoryIndex index.php index.html
# This makes my CakePhP work under http:/<my_ip>
<VirtualHost *:80>
ServerAdmin webmaster#tecadmin.net
ServerName example.com
DocumentRoot /var/www/MyCakePhPApp
<Directory /var/www/MyCakePhPApp>
Allowoverride All
</Directory>
</VirtualHost>
With this my CakePhP is accessible as "http:/my_ip" but my mantisbt site is not accessible. "http:/my_ip/mantisbt" goes to CakePhP and says that controller is missing. If I comment out the entire VirtualHost, then Mantis is indeed accessible as: "http:/my_ip/mantisbt", but of course then my CakePHP site is not accessible.
Does anyone know how to get both these websites working side-by-side?

How can I have a Django application on my main domain, and a PHP application on a subdomain?

I have a Django application hosted on my main domain (example.com), and I now need to host a PHP application on a subdomain (forum.example.com).
In the directory of the main domain, I have the following .htaccess entries:
SetHandler mod_python
PythonPath "/home/.../apps/example'] + sys.path"
PythonOption mod_python.importer.path "['/home/vlive/python']+ sys.path"
PythonHandler django.core.handlers.modpython
#PythonDebug On
SetEnv DJANGO_SETTINGS_MODULE example.settings
SetEnv PYTHON_EGG_CACHE /tmp/egg-cache
At present, when I load the subdomain (forum.example.com) I still see the main site (example.com).
How can I fix this?
You need to set up your subdomains via the VirtualHost directive and only add the mod_python / mod_wsgi handler in one of the VirtualHosts.
You said you loaded the subdomain and it still shows the main site. Would you mind showing us your Apache's site configuration?
The most common reason for wrong VirtualHost being selected is a missing NameVirtualHost directive that matches host/port qualifier specified in VirtualHost directive.
What NameVirtualHost directive do you have set? What is the argument to the two VirtualHost configurations?
The general layout would be:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.com
...
</VirtualHost>
<VirtualHost *:80>
ServerName forum.example.com
...
</VirtualHost>
These would usually be in the same file for a Linux Apache distribution though.
What have you used?
As Graham noted, most probably you have wrong VirtualHost configuration.
Double check that there is two different VH with right ServerName and that you don't use * (wildcard) in ServerName and ServerAlias that may overlap with subdomain or it goes AFTER subdomain.
Apache only search for first VH that matches, so if you have something like *.example.com above any other subdomain like forum.example.com would not work.
Also mod_wsgi is recommended by django documentation, consider switching to it.

Categories