how to create multiple level subdomains with apache2 and/or PHP - php

I am looking for a way to have multiple-level subdomains on a server running Apache2.2 and PHP5. Ideally the solution will be in Apache and not PHP.
For instance:
www.apps.example.com OR
test.apps.example.com
I've seen this on commercial sites before but have not seen any solutions for how to implement this. I essentially want to allow users to either enter www before any subdomain or just to enter the subdomain without the www's. so both x.example.com and www.x.example.com resolve to the same directory.
I am running CentOS 5.4 & Ubuntu 8.04, PHP 5.2.10 & Apache 2.2
Thanks

You can dynamically add entries into apache config files using a PHP language, But remember you are living on edge of uncertainty
// add this to your httpd.conf
Include extra/httpd-vhosts.conf
// add this to extra/httpd-vhosts.conf
<VirtualHost *:80>
// with www prefix
DocumentRoot /www/example/x
ServerName x.example1.com
ServerAlias www.x.example1.com
</VirtualHost>
<VirtualHost *:80>
// without www prefix
DocumentRoot /www/example/x
ServerName x.example1.com
</VirtualHost>

You can do that by adding apache virtual hosts. A bunch of great examples can be found here: http://httpd.apache.org/docs/2.0/vhosts/examples.html

Related

run laravel project without artisan serve

i have an issue that i want to run my laravel project without cmd command php artisan serve , i want something like usually in php project localhost/projectName , when i do all the steps like changing hosts in System32/driver/etc/hosts
and changing E:\wamp\bin\apache\apache2.4.33\conf\extra\httpd-vhosts.conf with all recommended option i restart wamp but when i put on the url projectname.dev the cercle above browser still Spins and nothing displays in the browser!!!!
someone has any idea
the configurations is
<VirtualHost *:80>
ServerAdmin webmaster#ahssanstore.dev
DocumentRoot "E:\wamp\www\AhssanStore\public"
ServerName ahssanstore.dev
ServerAlias www.ahssanstore.dev
ErrorLog logs/ahssanstore.dev-error.log
CustomLog logs/ahssanstore.dev-access.log common
<Directory "E:\wamp\www\AhssanStore\public">
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
and in C:\Windows\System32\drivers\etc
i add 127.0.0.1 ahssanstore.dev
and i restart wamp and after that i navigate to localhost/ahssanstore.dev
For windows, you can try use Laragon.
For those that have an Ubuntu Dev Server (with the possibility of other projects in your /var/www) I did the following to make it work.
Rename your-app/server.php to your-app/index.php
Copy .htaccess from your-app/public/ to your-app/
PS: If anyone knows that this creates some problems down the line please advice.
Done
Sometimes, servername: 'projectname.dev' doesn't work (I'm used to meet like this). Try changing 'projectname.me' instead 'project.dev'.
For VirtualHost configuration, you only need to set two lines:
<VirtualHost *:80>
DocumentRoot "E:\wamp\www\AhssanStore\public"
ServerName ahssanstore.dev
</VirtualHost>
Just Sharing my knowledge. Hope it also works!

Where can I find vhost configuration for symfony3

I want to access my site via testing.dev instead of localhost:8000.
How can I achieve this?
<VirtualHost *:8000>
DocumentRoot "C:/Websites/test"
ServerName www.testing.dev
ServerAlias testing.dev
</VirtualHost>
I googled the problem, but they all speak of a httpd-vhost.conf file, but I don't seem to have one in my project.
Where / how can I find it? FYI, I'm using the built in webserver (bin/console server:run)
Tried this already
I found a httpd-vhost.conf in my xampp folder (don't use xampp tho)

Virtual Host on Xampp for Laravel is not working

Hello i am learning Laravel and trying to modifying vhost file of my xampp server for laravel practice project, but its not working.
code of vhost file is
<VirtualHost *:80>
DocumentRoot "D:/xampp/htdocs/laravel5Prc/public"
ServerAdmin admin#localhost
ServerName laravel5.prc
ServerAlias laravel5.prc
<Directory "D:/xampp/htdocs/laravel5Prc/public">
AllowOverride All
Options Indexes FollowSymLinks
Require local
# if you want access from other pc's on your local network
#Require ip 192.168.1.121
# Only if you want the world to see your site
#Require all granted
</Directory>
</VirtualHost>
i have taken this code from this url
How to enable Virtual Host on Xampp for Laravel?
its not working form me, anyone please guide me what am i doing wrong in it ?
I'm strongly recommend you to use Laravel's vagrant box called Homestead instead of xampp, it's really amazing! You can read about it here. It's much more flexible and easier to understand than anything else.
I was using php-7.3.2 and had similar problem. This is not the solution to the problem, but an alternative. I went back to php-7.1.25 and ran again. I'm still going to check out what the problem is with version 7.3 and laravel, but maybe this will help some people who come here with the same problem.
This is what you should do, add or uncomment this below at the top of the xampp vhost file
NameVirtualHost *:80
Change this to you valid assumed domain and add the Directory index
ServerName laravel5.test
ServerAlias laravel5.test
DirectoryIndex index.php
And then go to the this folder C:\Windows\System32\drivers\etc\hosts in windows and add your domain of choice for example see this below
127.0.0.2 laravel5.test
127.0.0.3 anotherdomain.test
**** Please note don't use the localhost or 127.0.0.1 created by default to set yours's
you should create you own e.g. (127.0.0.2, 127.0.0.3, 127.0.0.4) in that order
After then restart you xampp server you should be good to go
**** I notice your Document and Directory has this "D:/xampp/htdocs/laravel5Prc/public"
change the D to C and please I would advise your laravel project should be outside the xampp folder, you can use the Document folder for this.
//Open C:\Windows\System32\drivers\etc\hosts
127.0.0.1 laravel5.prc
//Open xampp/apache/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "C:/xampp/htdocs/laravel5Prc/public"
ServerName laravel5.prc
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/d`enter code here`ummy-host2.example.com-access.log" common
</VirtualHost>
why you don't use
php artisan serve
to run laravel on localhost.
from https://laravel.com/docs/5.4/

What is wrong in the deploy of this Laravel application? Need I an effective domain instead the vhost used on my local environment?

I am pretty new in PHP and moreover in Laravel and I am pretty desperate trying to deploy a Laravel 5.4 application that works fine on my local environment on my Linux remote server.
So I think that it is something related to virtual host configuration or something like this (maybe also something related to the .htaccess file)
In my local environment (I am using XAMPP on Windows) I have setted this virtual host into the C:\xampp\apache\conf\extra\httpd-vhosts.conf file:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/HotelRegistration/public"
ServerName laravel.dev
</VirtualHost>
So opening the laravel.dev URL I obtain the standard Laravel homepage (I have yet not replaced it with a landing page).
Then if I open this URL: http://laravel.dev/registration
I obtain the user registration page developed by me, this because I have this route into my web.php file into my project:
Route::resource('/registration', 'RegistrationController');
Then into my RegistrationController.php there is this method showing the resources/views/registration/index.blade.php view
public function index(){
return view('/registration/index');
}
All works fine.
Now I have uploaded this Laravel website into my remote Linux server, into this folder: /var/www/html/HotelRegistration
But now my problem is that in this remote environment I have not virtual host (correct me if I am doing wrong assertion: from what I have understand the virtual host is used on the local environment to simulate a domain that Laravel need to point to the public folder, is it this reasoning correct?)
Anyway, this is the URL of the public folder of my deployed web site on my remote server:
http://89.36.211.48/HotelRegistration/public/
As you can see opening it the Laravel landing page is correctly shown, the problem is that I can access to the previous registration page, the only way that I have found is to open this URL:
http://89.36.211.48/HotelRegistration/public/index.php/registration
but it is pretty horrible and above all when the registration form is submitted it is generated a POST request toward this URL http://89.36.211.48/registration that end into a 404 Not Found error.
So I think that it depend by the fact that in this remote server I can't use a virtual host that simulate a domain (as I have on my local environment), but I am not sure about it.
What can I do to solve the situation? Do you think that using a effective domain (something like: www.myregistration.com) that points to this directory of my remote server http://89.36.211.48/HotelRegistration/public/ I can solve this problem?
You need to configure your domain in your server and need to reconfigure the apache. I'm considering you are having apache2 server so here you can do:
Step 1 Go to the apache2 folder cd /etc/apache2
Step 2 You can see sites-available folder go inside it cd sites-available
Step 3 Make a new file name it laravel.dev.conf
Step 4 Write down the following sudo nano laravel.dev.conf
Step 5 Write down the following option:
<VirtualHost *:80>
ServerAdmin webmaster#laravel.dev
ServerName laravel.dev
ServerAlias www.laravel.dev
DocumentRoot /var/www/html/laravel.dev/public/
ErrorLog /var/www/html/laravel.dev/logs/error.log
CustomLog /var/www/html/laravel.dev/logs/access.log combined
<Directory /var/www/html/laravel.dev/public/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
Step 6 Now go to this folder/create a new one cd /var/www/html/laravel.dev
Step 7 Copy/Install your laravel application here.
Step 8 Now you can enable your site by typing sudo a2ensite laravel.dev.conf
Step 9 Now restart the apache2 sudo service apache2 restart
Now you can have proper access to your domain. Hope this helps.
Since you are using XAMPP
Add the following into your VirtualHost Directive:
<Directory "LINUX PATH TO /HotelRegistration/public">
AllowOverride All
</Directory>
Your final VirtualHost Directive should look like:
<VirtualHost *:80>
DocumentRoot "LINUX PATH TO /HotelRegistration/public"
ServerName 89.36.211.48
<Directory "LINUX PATH TO /HotelRegistration/public">
AllowOverride All
</Directory>
</VirtualHost>
After the configuration changes, restart Apache then you are good to go.

Hosting multiple local sites with XAMPP

I'm new to using XAMPP so this may be simple to some people.
I have a few php projects that I would like to be able to debug locally and view in the browser (not concurrently, but without having to change config files/copy project folders each time I want to work on a different project).
On IIS, you could set up multiple sites to serve from your machine, and I'm looking for something similar in XAMPP. When using IIS, I added multiple records to the Windows hosts file so I could access the locally hosted sites by typing friendly web-style addresses (like http://myproject1.dev)
Thanks.
Greg, you're almost there--you need (like Moses said) to setup virtual hosts.
So if your Windows hosts file has
127.0.0.1 localhost
127.0.0.1 mysite-dev.com
127.0.0.1 anothersite-dev.com
Your virtual hosts file (httpd-vhosts.conf) might look like:
<VirtualHost *:80>
DocumentRoot C:/xampp/htdocs/
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
ServerName mysite-dev.com
DocumentRoot "C:/sites/mysite-dev"
<Directory "C:/sites/mysite-dev">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName anothersite-dev.com
DocumentRoot "C:/sites/anothersite-dev"
<Directory "C:/sites/anothersite-dev">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Don't forget to restart the web server after you make any changes.
I would like to make an additional in terms of up to date information.
XAMMP uses port 80 by default and we are able to publish 1 website. I also use IIS for .Net projects. In this respect, I set the port to XAMMP except the 80 port. So I avoid a conflict.
When we want to publish more than one website, we should do the following operations to httpd.conf (this is the current name).
1. Setting the ports
Find the #Listen expression in the httpd.conf file.
Change Listen 80 to Listen 8000 (or whatever else you want)
Listen 8000
If you need 3 different websites, type the others, including 1 definition on each line, as follows.
Listen 8001
Listen 8002
Listen 8003
2. Define the file paths of sites accessed through ports
Again, find in the httpd.conf file.
Identify the folders of each website as follows.
As you would see, I've created 3 directories called 8000, 8001, 8002 and 8003 under the htdocs directory within the XAMMP directory.
<VirtualHost *:8000>
 DocumentRoot "C:\XAMPP\htdocs\8000"
 ServerName localhost:8000
<\ VirtualHost>
<VirtualHost *:8001>
 DocumentRoot "C:\XAMPP\htdocs\8001"
 ServerName localhost:8001
<\ VirtualHost>
<VirtualHost *:8002>
 DocumentRoot "C:\XAMPP\htdocs\8002"
 ServerName localhost:8002
<\ VirtualHost>
<VirtualHost *:8003>
 DocumentRoot "C:\XAMPP\htdocs\8003"
 ServerName localhost:8003
<\ VirtualHost>
Restart your Apahche server on XAMMP.
You can now view your 3rd site, such as http://localhost:8003 or http://192.168.1.1:8003/.
Hope to be useful.
This question was asked almost ten years ago, and the answers above are a bit dated. Note that XAMPP has a "How-To" for virtual hosts avilable off the dashboard, when you install it.
From the "Welcome to XAMPP for Windows" page (localhost/dashboard, the default when you first load localhost) click on the "HOW-TO" Guides in the top menu bar. From there, look for the link "Configure Virtual Hosts" which will lead you to the localhost page "http://localhost/dashboard/docs/configure-vhosts.html"
In a nutshell, the process involves editing the "httpd-vhosts.conf" file (typically in C:\XAMPP\apache\conf\extra) and replacing the contents of that file with something like this:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/"
ServerName localhost
</VirtualHost>
# vhosts - note sample entry from XAMPP how-to throws an error, so try this:
<VirtualHost *:80>
DocumentRoot "C:/Users/jdoe/Documents/dev.mysite.com/htdocs"
ServerName mysite.local
<Directory "C:/Users/jdoe/Documents/dev.mysite.com/htdocs">
Require all granted
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Additional vhosts (including SSL hosts) can be had by cloning the entry, and modifying DocumentRoot and ServerName directives and port numbers (e.g. 443 for TLS (SSL)). You can find tutorials on the web for creating and signing your own certificate, if you want to go that route.
The final step is to get your Windows machine to point your browser to the Apache host for your virtual domain (e.g. above, http://mysite.local). Using a text editor (Notebook will do) as administrator append the following entry onto your hosts file, which lives here:
C:\Windows\System32\drivers\etc\hosts
Append this entry to the hosts file:
127.0.0.1 mysite.local
IMPORTANT - you must restart your Windows machine or the new host will not respond. Some documentations will tell you just to restart the browser and Apache server, but I've found that's not sufficient.
IME, the hosts system and Apache directives can be particular, so be patient. You may need to rebuild configs, restart Apache, and restart your machine more than once.

Categories