Cakephp 3 and Subdomain - php

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.

Related

Apache2 - Set Document Root based on subdirectory

I have a default Virtual Host. It is configured to watch Document Root inside /var/www/html and works great. But I have a problem. I have multiple sites inside subdirectories and I don't know how to set Document Root for each of them. For example, /var/www/html/test, /var/www/html/test2, etc. When I include file from /var/www/html/test, it searches inside var/www/html. For example, I include file like include_once '/core.php', but I have an error Failed opening required '/var/www/html/core.php', because there is no current file here. Is it possible to set Document Root for each of directories inside main Virtual Host? Thank you!
My main Virtual Host configuration:
<VirtualHost *:80>
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
AccessFileName .htaccess
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Also, you can say, that I need to use relative paths, but it isn't possible in some ways, because, sometimes, I work with ready sites, created by another people.
Is it possible to set Document Root for each of directories inside
main Virtual Host?
Short answer is no, you cannot. 1 Document Root per Virtual Host. In order to understand how the things are working you need to understand what is the directives context. Lets take as an example VirtualHost directive. As we can see from the docs it has only 1 Context which is Server Config. And server config context tells next:
directive may be used in the server
configuration files (e.g., httpd.conf), but not within any
VirtualHost or Directory containers. It is not allowed in
.htaccess files at all.
But DoocumentRoot has two contexts: server config and virtual host. Rest you should figure out for yourself ;)
There are plenty ways how you can achieve desired result
Running several name-based web sites on a single IP address
Running different sites on different ports
Mixed port-based and ip-based virtual hosts
Hope that this answer will help you!

Apache control in multi site server configuration

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

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.

Configuration: DNS or Apache

I have the following problem.
I have two domains: www.domain1.de and www.domain2.de
I also have path is on Jelastic server where to find my PHP page
myphpsite.jelastic.dogado.eu.
Now I wanted to do the following.
1) If I go to www.domain1.de, then should address bar of the Web Browser
www.domain1.de be displayed, but the page is fetched from myphpsite.jelastic.dogado.eu.
2) When I go to www.domain2.de, then should address bar of the Web Browser
www.domain2.de be displayed, but the page is fetched from myphpsite.jelastic.dogado.eu / admin /.
so
1) www.domain1.de -> myphpsite.jelastic.dogado.eu
2) www.domain2.de -> myphpsite.jelastic.dogado.eu / admin /
The first one I can do by CNAM Record
But we can I solve the second problem without frames?
thank you
This is just a matter of configuring Apache VirtualHosts (assuming that you're using Apache), or Nginx Server Blocks (if you're using Nginx). Leo's link can help you with either of them, within a Jelastic context (where to find those config files etc.): http://docs.jelastic.com/multiple-domains-php
Here's a quick example for Apache:
<VirtualHost *:80>
DocumentRoot /var/www/webroot/ROOT
ServerName domain1.de
ServerAlias www.domain1.de
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/webroot/ROOT/admin
ServerName domain2.de
ServerAlias www.domain2.de
</VirtualHost>
You might also wish to define different log files for each domain etc.
There are many possibilities, and since Jelastic gives you direct access to the server config. files you can configure almost anything that you could want here. It's just a matter of reviewing the Apache (or Nginx) docs and trying it out.
Notes:
Beware that the Jelastic default configuration defines a wildcard to catch all requests. You need to place your custom configuration before that (either literally, or via an include), or else overwrite the default VirtualHost configuration block - otherwise your config. will not have any effect.
The above example handles HTTP only. If you need to handle HTTPS, you need to configure this separately. (e.g. :443 instead of :80)
Remember that you need to restart Apache (or Nginx) for config. changes to take effect.
See also:
Apache VirtualHost Documentation
Nginx Server Blocks Documentation

XAMPP localhost returns object not found after installing Laravel

After much faffing about I got laravel to work with XAMPP. However, I can't seem to access directories in the htdocs folder via localhost now. Attempt to access a file returns Object not found! along with The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
The changes I made to get laravel working seems like a blur now. The only thing I remember doing is editing the hosts file to enable the virtual host to work by adding 127.0.0.1 laravel.dev in the hosts file (using a mac btw). I also added a virtual host to the httpd-vhost.conf file.
I did undo the above changes but it didn't make a difference.
Any thoughts on whats gone wrong?
Thanks.
Dispelling Confusion
Just thought I'd clarify what my experience is. Before installing laravel 4 I could access all my projects with localhost/someProjectName but now it fails.
I'm trying to identify what change caused this behaviour. Btw, I have no problems accessing my laravel project (my mapping allows me access to it via laravel.dev)
Look into your /etc/httpd.conf file and see if you have Virtual hosts activated.
If so, check your etc/extra/httpd-vhosts.conf file. You might have set up a VirtualHost already.
EDIT: I have found that once you turn on Virtual Hosts, XAMPP needs a default VirtualHost for your basic htdocs files
Try adding a default VirtualHost (at the bottom of the file) like so:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/Applications/XAMPP/htdocs"
<Directory "/Applications/XAMPP/htdocs">
Options Indexes FollowSymLinks Includes execCGI
AllowOverride All
Order Allow,Deny
Allow From All
</Directory>
</VirtualHost>
It sounds to me like it is a problem with the way your directories are configured. Be sure to create a Virtual Host (if you're using Apache) that points to the public folder within your application directory.
For example, if your Laravel project is under /Applications/XAMPP/htdocs/Laravel then set up a Virtual Host like this:
<VirtualHost *:80>
DocumentRoot /Applications/XAMPP/htdocs/Laravel/public
# Other directives here
</VirtualHost>
Additionaly, if you're working with PHP >= 5.4, SSH into the application folder and run
./artisan serve
(Be sure that your PHP executable is in your PATH variable). Then go localhost:8000 and you should have your application running.
Running this command runs the PHP built-in webserver and saves you the trouble of configuring virtual hosts.
In your Apache's http.conf, find the DocumentRoot line and add the subdirectory /public on the end.
Once that is done and you've restarted Apache, you'll be able to access everything which is contained within your htdocs/public folder (including subdirectories of that folder), along with any routes you've defined in Laravel.
Laravel is designed to be set up this way as to protect the code and files by not having them in the folder which is served out to the web.
I have same issue and found that there are miss configuration in vhost. So that's are correct configuration.
in etc/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "D:/xampp/htdocs/laravel-master/public"
ServerName laravel.local
ErrorLog "logs/laravel-master-error.log"
<Directory D:/xampp/htdocs/laravel-master/public>
AllowOverride All
Order Allow,Deny
Allow From All
</Directory>
</VirtualHost>
In the hosts file
127.0.0.1 laravel.local
I don't know if you're running L4 or L3. However, launch CLI and
$ cd ./path/to/project
Then for L4:
$ php artisan serve
For L3:
$ php -S localhost:8000 -t public
Now you can go to localhost:8000 and see your application.
You're having problem because the object really doesn't exist in your htdocs directory. You don't have to append xampp after localhost or 127.0.0.1 because xampp will treat it as an object or a folder under htdocs.
if you want to access your blog, make sure you have a blog folder under htdocs and put in your URL localhost/blog

Categories