How can I access my Laravel app from another PC? - php

I'm trying to access my Laravel app from another PC in the same network using IP address but I can't get it to work by accessing 192.168.1.101:8000 in my browser.
What should I do?

Why don't you use Laravel's artisan for it? Very simple:
sudo php artisan serve --host 192.168.1.101 --port 80
Now from other computers, you can type: http://192.168.1.101
Do not forget to replace the IP with your own local one. That's it.
Note: The sudo is only needed if you wanna serve on port 80.

Access laravel using your IP address
php artisan serve --host 0.0.0.0
Now you can access laravel server by http://laravel-server-ip-address:8000
If you want to change the port as well then
php artisan serve --host 0.0.0.0 --port 8101
Now your laravel server is http://laravel-server-ip-address:8101

Go to httpd.conf in Apache folder and find the following lines:
DocumentRoot "c:/wamp/www"
<Directory "c:/wamp/www">
# ...Stuffs in here
Options Indexes FollowSymLinks
# ...Stuffs in here
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
Then replace the last line within <Directory> tag for:
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Allow from ::1
Allow from localhost
</Directory>
Add a virtual host for your laravel application, go to httpd-vhosts.conf and add the following lines:
<VirtualHost *:80>
DocumentRoot "D:/.../your-laravel-app-path/public"
ServerName yourservername.dev
<Directory "D:/.../your-laravel-app-path/public">
AllowOverride All
Order deny,allow
Allow from all
Require all granted
</Directory>
</VirtualHost>
Restart all your apache services
This should do it, i'm using wamp on Windows and works for me.

Use this command in which ip address is ip-v4. Check ip-v4 from your wifi connection properties.
php artisan serve --host 192.168.10.15 --port 5000

Find your machine local IP:
ifconfig
Go to project folder and type following command:
sudo php artisan serve --host your_ip --port your_port
example:
sudo php artisan serve --host 192.168.1.70 --port 8080
Browse form other computer:
192.168.1.70:8080

You can do it by using laravel's built in command php artisan serve.
Follow the steps:
Go to your project's root directory within command line.
run php artisan serve
If you still can't access port 8000 or it is already in use then run:
php artisan serve --port 8383
And it should listen on new port you provided. Also you can set other options for this command. Look for the help php artisan help serve.

Related

Laravel 9: run project without using php artisan serve command

I want to run my laravel 9 app without 'php artisan serve'
i know how to do it in older version but laravel 9 don't have the server.php file in root directory
so how to do it now
and i don't want to use 'localhost/project/public/' url
I am currently on
laravel: version 9.19
OS: Ubuntu 22.04.1 LTS
php: 8.1.2
Server: Apache/2.4.52 (Ubuntu)
First step is to add your project to list of virtual hosts in XAMPP that is located in \apache\conf\extra > httpd-vhosts.conf
It can be something like:
<VirtualHost *:80>
DocumentRoot "{pathToProject}\public"
ServerName projectname.localhost
<Directory "{pathToProject}">
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Second step is to add host in your hosts file that is located in same disk as your OS then \Windows\System32\drivers\etc > hosts
It should probably be something like
127.0.0.1 projectname.localhost
If you've changed default address settings, you will have to change address in hosts as well.
Restart XAMPP and you're good to go.

How to share a laravel project on the local network

I have a laravel project and its user has a virtual domain how do I publish it in the local network only
This is my configuration in httd.vhosts.conf file
<VirtualHost *:80>
ServerName smarts.local
DocumentRoot "C:/xampp/htdocs/smarts/public"
SetEnv APPLICATION_ENV "development"
<Directory "C:/xampp/htdocs/smarts/public">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
This is in my hosts file
127.0.0.1 smarts.local
Currently it works on my pc only but does not work on other devices in the network.
you can use the artisan command,
first run the cmd on your computer,
then go to the folder of your project like this: cd c:\wamp64\your_project_folder,
then type this code: php artisan serve --host=YOUR IP --port=ONE FREE PORT
you can find your computer port with the running this command in cmd: ipconfig /all
your command should be like this:
php artisan serve --host=192.168.1.106 --port=8080
and the others can run your project from your computer with writing this code in the addressBar of their own browser : 192.168.1.106:8080
If you want others to access smarts.local.
make sure your system ip address is static.
then every one who wants to access smarts.local should add the following to their hosts file
127.0.0.1 smarts.local (Replace 127.0.0.1 with ur public static ip)
or you should use DNS server in your network.
You can also write a startup command which will execute php artisan serve.(But you have to make sure ur public ip is static)
Its so much easy to do.
1.In windows go to cmd and type ipconfig.
2.get you local IPv4 Address (e.g:192.168.1.5).
3.Go to visual studio code and open Terminal
4.Type like this (php artisan serve --host=IPv4 Address)
Hopefully this will help you to share your project inside your same network.

Laradock php-fpm apache2 file_get_contents connection refused

I've running laradock 7.0.0 with the a multi project structure with the following containers:
docker-compose up -d apache2
That will run php-fpm and apache2
I configured my hosts file to connect to localhost:
/etc/hosts
127.0.0.1 myproject.local
I've create an site config for my apache2:
laradock/apache2/sites/myproject.conf
<VirtualHost *:80>
ServerName myproject.local
DocumentRoot /var/www/myproject/public/
Options Indexes FollowSymLinks
<Directory "/var/www/myproject/public/">
AllowOverride All
<IfVersion < 2.4>
Allow from all
</IfVersion>
<IfVersion >= 2.4>
Require all granted
</IfVersion>
</Directory>
</VirtualHost>
I modify the php.ini settings to allow url include:
laradock/php-fpm/laravel.ini
allow_url_include=On
When going to myproject.local the website is showing.
No problems so far. But when i do a file_get_contents, then i get an connection refused:
$json = file_get_contents('http://myproject.local/test.json');
var_dump($json);
exit;
Response:
Warning: file_get_contents(http://myproject.local/test.json): failed to open stream: Connection refused in /var/www/myproject/public/test.json
It seems that php-fpm can't find apache2. When i get the ip adres of the apache2 container:
docker exec -ti laradock_apache2_1 bash
ifconfig
Results in:
eth0 Link encap:Ethernet HWaddr 02:42:ac:16:00:04
inet addr:172.22.0.4
And i modify the hosts file on my php-fpm
docker exec -ti laradock_php-fpm_1 bash
nano /etc/hosts
Add rule:
172.22.0.4 myproject.local
Than the file_get_contents works. The problem with this fix is that the ip of the apache2 container is not always the same. And i'll have to do this every time for every project.
Is there a way to modify the Dockerfile or docker-compose.yml to link php-fpm to apache2 for every project in once?

How to Auto serving laravel

is there any way to auto serving you laravel project?
i'm using virtual host.
<VirtualHost *:80> #laragon magic!
DocumentRoot "C:/laragon/www/pos/public/"
ServerName apk.dev
ServerAlias *.apk.dev
</VirtualHost>
and to access it locally i open the Cmdr and write
php artisan serve --host 192.168.1.100 --port 80
every single time.
and i access it with other device with 192.168.1.100 it's working fine, i just want to know is there any way that i don't need to repeat it, and just by running the laragon i also can access the apk.dev from other device with my local network?

Laravel php artisan serve to mimic HTTPS

I have been searching around to see if there is a way I can mock SSL for local development using Laravel's artisan to serve HTTPS with no luck.
Is this possible and if so, how?
I understand this is a very general question, but I am not seeing anything on this in searches.
You can use ngrok for that
php artisan serve
cd <path-to-ngrok>
./ngrok http localhost:8000
https://ngrok.com/
Laravel uses the in-built PHP5.4 development server php -S (http://php.net/manual/en/features.commandline.webserver.php) for it's artisan serve command (see Illuminate\Foundation\Console\ServeCommand). This only supports plain HTTP, so no, this isn't possible. Your best bet would be to use a Vagrant box set up to work with SSL/TLS.
If you're using xampp, then you can setup HTTPS locally with xampp (this post is also useful for setting up HTTPS) and then you can:
move your project to htdocs folder and visit it with https://localhost/projectFolder/public/
or just create a special VirtualHost in httpd-vhosts.conf for this project (always point to that public folder, this is from where the project is running) and then visit it with https://localhost/ in this example (you can of course, run it on a subdomain if you want to)
<VirtualHost *:80>
ServerName localhost
DocumentRoot "c:\pathToYourProject\projectFolder\public"
<Directory "c:\pathToYourProject\projectFolder\public">
Options All
AllowOverride All
</Directory>
</VirtualHost>
# this should ensure https (this is mentioned in the stackoverflow post, that I linked as useful
<VirtualHost *:443>
ServerName localhost
DocumentRoot "c:\pathToYourProject\projectFolder\public"
SSLEngine on
SSLCertificateFile "conf\ssl.crt\server.crt"
SSLCertificateKeyFile "conf\ssl.key\server.key"
<Directory "c:\pathToYourProject\projectFolder\public">
Options All
AllowOverride All
</Directory>
</VirtualHost>
Theoretically, when you're using this method, you don't even need php artisan serve (tbh I'm not entirely sure if it has any purpose in this case).

Categories