Create dynamic sub-domain as per username in Laravel - laravel

I'm creating web application which needs dynamic domain per user.
Let say I've 5 users in my DB such as.
1) wanda
2) edward
3) rick
4) maurice
5) kristin
Now I've a domain as example.com.
I want to get data of that 5 users as per his domain.
1) wanda.example.com
2) edward.example.com
3) rick.example.com
4) maurice.example.com
5) kristin.example.com
route.php
Route::group(['domain' => '{user}.example.com'], function () {
Route::get('/', function ($user) {
echo $user;
exit;
});
});
I'm using Xampp, Apache server so I configure httpd-vhosts file as
C:\xampp\apache\conf\extra\httpd-vhosts
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/myproject"
ServerName example.com
ServerAlias *.example.com
DirectoryIndex index.php
<directory "C:/xampp/htdocs/myproject">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
C:\Windows\System32\drivers\etc\hosts file
127.0.0.1 example.com
127.0.0.1 *.example.com
When I access user.example.com I'm Getting this error.
I want this type of dynamic domain as works.
http://wanda.thewallchat.com/
http://edward.thewallchat.com/

You cannot write * for all in <VirtualHost *:80>
you need to specify each alias which you want to use in your system.
change
ServerAlias *.example.com
to
ServerAlias example.com wanda.example.com edward.example.com rick.example.com maurice.example.com kristin.example.com
Don't forget to restart apache.
If you want to setup on live server configure wildcard subdomain.
ServerAlias *.example.com

Related

Laravel Framework 5.7.29 blank main page

I was having problems with a blank page in my first Laravel project. I'm currently watching the Laravel Framework course on YouTube. This is Lesson 1 called "How to install the Laravel Framework". I checked it out laravel.com for the requirements section and made sure I have all of them. After that, I installed Composer 2.0.13. Using it, I installed Laravel Framework 5.7.29. I got reports that some packages are deprecated. They were about changing the color on the console.
The next step was to change "/etc/hosts". I added the line 127.0.0.1 poligon.local The next step was to change "/opt/lampp/etc/extra/httpd-vhosts.conf". I added the lines:
<VirtualHost *:80>
ServerAdmin admin#example.com
DocumentRoot "/home/stas/projects/poligon.local/public"
ServerName poligon.local
ServerAlias www.poligon.local
<Directory /home/stas/projects/poligon.local/public>
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
Require all granted
</Directory>
ErrorLog "logs/poligon-error.log"
CustomLog "logs/poligon-access.log" common
</VirtualHost>
Then I wrote $ sudo /opt/lampp/lampp start to start the local XAMPP server. And I have a blank page on the "poligon.local/". When I started php artisan serve, I had access to "poligon.local:8000", and there is the Laravel home page. But the author of the video got access to the "poligon.local" without php artisan serve. What do you think is my mistake?
Update: If I create /public/index.html I will get this html page.
Update2: I was experimenting now with php artisan serve --host 127.0.0.1 --port 80 and found that with the XAMPP server enabled, this socket is already in use. But when the server is turned off, I manage to run artisan command with these connection settings. As I understand it, XAMPP occupies this socket, but for some reason can't handle it on its own.
Update3: I checked it out /public/index.php the whole file and found out that it is being executed. Up to the line $response = $kernel->handle( $request = Illuminate\Http\Request::capture() ); There is no continuation after it.
Define the site you added in hosts file in the Virtual Host route.
Change this
<VirtualHost *:80>
ServerAdmin admin#example.com
DocumentRoot "/home/stas/projects/poligon.local/public"
ServerName poligon.local
ServerAlias www.poligon.local
<Directory /home/stas/projects/poligon.local/public>
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
Require all granted
</Directory>
ErrorLog "logs/poligon-error.log"
CustomLog "logs/poligon-access.log" common
</VirtualHost>
to this
<VirtualHost poligon.local:80>
ServerAdmin admin#example.com
DocumentRoot "/home/stas/projects/poligon.local/public"
ServerName poligon.local
ServerAlias www.poligon.local
<Directory /home/stas/projects/poligon.local/public>
Options Indexes FollowSymLinks
AllowOverride All
Order Allow,Deny
Allow from all
Require all granted
</Directory>
ErrorLog "logs/poligon-error.log"
CustomLog "logs/poligon-access.log" common
</VirtualHost>
Notice that i have defined the host URL you defined in the virtual host route
From <VirtualHost *:80> to the url you defined <VirtualHost poligon.local:80>
What this * does is it gets all the requests and direct it to XAMPP homepage which is default.

Access forbidden either read protected or not readable

Currently I have multiple laravel project in my htdocs and configured like this
<VirtualHost *:0903>
DocumentRoot "C:/xampp/htdocs/svs_web_application/public"
ServerName localhost
</VirtualHost>
<VirtualHost *:0904>
DocumentRoot "C:/xampp/htdocs/api_tk/public"
ServerName localhost
</VirtualHost>
<VirtualHost *:0905>
DocumentRoot "C:/xampp/htdocs/svs_api/public"
ServerName localhost
</VirtualHost>
listen 0903
listen 0904
listen 0905
I can access these projects and do some stuffs.
Not until when I configured another laravel project with this kind of setup
<VirtualHost *:0902>
DocumentRoot "C:/xampp/htdocs/pinoytravelreseller/public"
ServerName localhost
</VirtualHost>
listen 0902
And I got this kind of error
Access forbidden!
You don't have permission to access the requested object. It is either read-protected or not readable by the server
Tried to include this setup
<VirtualHost *:0902>
DocumentRoot "C:/xampp/htdocs/pinoytravelreseller/public"
ServerName localhost
<Directory "C:/xampp/htdocs/pinoytravelreseller/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from all
Require all granted
</Directory>
</VirtualHost>
listen 0902
But still doesn't work is there any configuration do I need to do?
is there any files on that folder,
can you access by the way like localhost:902/demo.php,
demo.php just a php which say a 'hi',
if it cannot run and say 'hi' well,
I think you need to check is there any htaccess file on the root of project folder

Laravel route getting ruined when trying to reach HTTP request

Currently I have successfully setup my Laravel Passport API
Using Laravel 5.8.
I have this httpd-vhosts.conf config for my api
PORT: 80
<VirtualHost *:80>
ServerName <sub-domain>.<domain>.com
ServerAlias <sub-domain>.<domain>.com
Redirect permanent / https://<sub-domain>.<domain>.com
</VirtualHost>
PORT: 443
<VirtualHost *:443>
DocumentRoot "/opt/lampp/htdocs/api_tk/public"
<Directory "/opt/lampp/htdocs/api_tk/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ServerName <sub-domain>.<domain>.com
ServerAlias <sub-domain>.<domain>.com
ErrorLog "logs/API-error_log"
CustomLog "logs/API-access_log" common
ProxyPreserveHost On
ProxyRequests Off
ProxyPassMatch /fingerprint http://localhost:5000
ProxyPassReverse /fingerprint http://localhost:5000
RequestHeader set X-Forwarded-Proto https
RequestHeader set X-FOrwarded-Port 443
SSLEngine on
SSLCertificateFile "/opt/lampp/htdocs/ssl_key/svs-file.crt"
SSLCertificateKeyFile "/opt/lampp/htdocs/ssl_key/private_new.key"
SSLCACertificateFile "/opt/lampp/htdocs/ssl_key/svs-bundle-file.crt"
</VirtualHost>
443 is working fine I can see my HTTPS SSL Lock sign on my browser
But
When I try to do HTTP request
Laravel API always getting ruined.
I have route like this
https://..com/api/login
and this is working fine
But when I tried to do HTTP request like this
http://<sub-domain>.<domain>.com/api/login
it always end up to
https://<sub-domain>.<domain>.comapi/login
Where the slash is missing. This is because of the redirect permanent on my PORT 80 config.
I have this route for my api. (api.php)
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/timekeeping','Auth\Api\AuthController#timekeeping');
Route::post('/login','Auth\Api\AuthController#login');
Route::middleware('auth:api')->group(function () {
Route::post('/timekeeping_app','Auth\Api\AuthController#timekeeping_app');
Route::post('/logout','Auth\Api\AuthController#logout');
Route::post('/register','Auth\Api\AuthController#register');
Route::post('/show_dtr_list','Auth\Api\AuthController#show_dtr_list');
Route::post('/update','Auth\Api\AuthController#update');
Route::post('/delete','Auth\Api\AuthController#delete');
Route::post('/search_user','Auth\Api\AuthController#search_user');
Route::get('/current_time','Auth\Api\AuthController#current_time');
});
How can I prevent this?
UPDATE
Tried to do edit my vhosts config like this
Redirect permanent "/" "https://<sub-domain>.<domain>.com/"
And
Redirect permanent / https://<sub-domain>.<domain>.com\/
But this gives me an output like this
try do add slash under your apache virtual host settings:
<VirtualHost *:80>
ServerName <sub-domain>.<domain>.com
ServerAlias <sub-domain>.<domain>.com
# fix below
Redirect permanent / https://<sub-domain>.<domain>.com/
</VirtualHost>

Dynamic subdomain Laravel

I want to know, how to create dynamic subdomain with laravel 5.6 and wampserver
My code in web route for test
Route::group(array('domain' => '{subdomain}.test.local'), function () {
Route::get('/', function ($subdomain) {
$name = DB::table('users')->where('username', $subdomain)->get();
dd($name);
});
});
I do one parameter on my vhost and httpd-vhosts. After I added alias name. Result here
<VirtualHost *:80>
ServerName test.local
ServerAlias *.test.local
DocumentRoot "c:/wamp64/www/test"
<Directory "c:/wamp64/www/test/">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</Directory>
</VirtualHost>
In my browser I try to use test.test.local for try if subdomain is correct but doesn't work.
Note: I would like to have variable subdomain
Thank you for your answer

Laravel 5.2 site on localhost/Wamp

I am trying to learn Laravel.
I created a project on c:/sites where I develop all my sites. I called it larabasic.dev
I cannot open the site in my browser, it keeps switching to www.larabasic.dev and tells me server not found.
I set up a virtual host like I did to all my other sites that works well.
I tried:
1. Changing server.php to index.php and copied the public/htaccess file to the root folder
2. Going to http://larabasic.dev/public
3. Going to http://larabasic.dev/public/index.php
4. Changing vhosts file to:
<VirtualHost *:80>
ServerName larabasic.dev
ServerAlias larabasic.dev
DocumentRoot "c:/sites/larabasic.dev/public"
<directory "c:/sites/larabasic.dev/public">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</directory>
</VirtualHost>
Instead of:
<VirtualHost *:80>
ServerName larabasic.dev
ServerAlias larabasic.dev
DocumentRoot "c:/sites/larabasic.dev"
<directory "c:/sites/larabasic.dev">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</directory>
</VirtualHost>
My hosts file has these 2 entries:
127.0.0.1 larabasic.dev And
::1 larabasic.dev
Nothing helps. Again, any other sites I have developed with plain php work fine.
What am I missing?
Thank you
I've encountered the same problem and I've fixed this by giving 127.0.0.2 to my laravel application.
First, in the hosts file, you need to redirect larabasic.dev to 127.0.0.2:
127.0.0.2 larabasic.dev
Second, you need to bind the virtual host to that IP:
<VirtualHost 127.0.0.2:80>
DocumentRoot "C:/sites/larabasic.dev/public"
ServerAdmin admin#localhost
ServerName larabasic.dev
ServerAlias www.larabasic.dev
<Directory "C:/sites/larabasic.dev/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
# Only if you want the world to see your site
#Require all granted
</Directory>
</VirtualHost>
This is my working configuration on my local machine.

Resources