Laradock php-fpm apache2 file_get_contents connection refused - php

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?

Related

Apache Server development environment

I´m trying to explain he problem I have.
I usually use XAMPP or MAMP for this but I want to use the Apache Server that comes with OS X High Sierra by default installed.
So, OS X has apache server and php installed for default and they work great without a problem, and the localhost works fine with many other projects I have like angular and php.
My issue is this...
I tried to install a dev environment on my computer for php-laravel projects so after I installed laravel with homebrew and do some changes in the files:
/private/etc/apache2/extra/httpd-vhosts.conf
/private/etc/apache2/httpd.conf
/etc/hosts
httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "~/Sites"
#VirtualDocumentRoot "~/Sites"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "~/Sites/cms/public"
ServerName cms.test
ServerAlias *.test
</VirtualHosts>
httpd.conf
ServerRoot "/usr"
<IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
Listen 8080
</IfDefine>
<IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
Listen 80
</IfDefine>
User myusername
ServerName localhost:80
DocumentRoot "/Users/myusername/Sites"
<Directory "/Users/myusername/Sites">
Options All
MultiviewsMatch Any
AllowOverride All
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 cms.test
After this I restarted the server and when I tried to go to:
localhost
I just wrote on chrome browser localhost
If I tried localhost/info.php
localhost/info.php
As you can see inside the Sites folder there are an info.php file
folder Sites is where ServerDirectory is and as you can see there are an info.php file
when I tried localhost/cms/public/index.php
the result is the same
I execute $ whereis httpd and the result is /usr/sbin/httpd
then I executed $ /usr/sbin/httpd -V and the result is
Server version: Apache/2.4.28 (Unix)
Server built: Oct 9 2017 19:54:20
Server's Module Magic Number: 20120211:68
Server loaded: APR 1.5.2, APR-UTIL 1.5.4
Compiled using: APR 1.5.2, APR-UTIL 1.5.4
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_FLOCK_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr"
-D SUEXEC_BIN="/usr/bin/suexec"
-D DEFAULT_PIDLOG="/private/var/run/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="/private/etc/apache2/mime.types"
-D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"
I also executed the command apachectl -S
and the result is
VirtualHost configuration:
ServerRoot: "/usr/local/opt/httpd"
Main DocumentRoot: "/usr/local/var/www"
Main ErrorLog: "/usr/local/var/log/httpd/error_log"
Mutex rewrite-map: using_defaults
Mutex default: dir="/usr/local/var/run/httpd/" mechanism=default
Mutex mpm-accept: using_defaults
PidFile: "/usr/local/var/run/httpd/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="_www" id=70
Group: name="_www" id=70
I can see two different configurations and I do not know what I can do to fix this.
So that is why I am asking for help, I really don´t know what else I can do to fix this.
1) It looks like you have apache installed in multiple locations. Check to make sure. Do:
find /usr -name apachectl
If you get more than one result, then it is likely you have installed one version from Homebrew (which will install apachectl in /usr/local/bin, and set the PidFile directive for it's configuration file to /usr/local/var/run/httpd/httpd.pid) and, have another version that comes with Sierra, which will install in /usr/sbin, your system binary folder (and in my experience with this problem, set the PidFile to run in /etc/var/run/).
"Whereis" does not return the apachectl which has precedence (ie, which one you are calling when you execute the command). A better command is:
which apachectl
Which will reveal which apachectl you are currently using when you type in apachectl.
If it turns out you have multiple installations, you will need to set precedence for the version of apachectl you want in your ~/.bash_profile.
The reason why you are seeing two different configurations is because you are checking one's configuration with /usr/sbin/httpd - the apache that comes native with Sierra, and checking the other's configuration files with apachectl -S, which is likely calling the httpd installed by Homebrew.
2) Preventing access to a directory can be a problem that results from not having the correct permissions on the files themselves. Often you will need to
chown -R user:group dir_name
the the directory, where user and group are what the user is listed as in your configuration file. On my machine, this is _www for both in the configuration file.

Xubuntu Linux - Why doesn't my localhost direct me to my virtual host site?

I am setting up a virtual host on my linux machine, I followed all the steps and guides, but I do not go to my site for some reason. I have LAMP installed and apache, mysql, php is all working. I have a database setup on phpmyadmin and imported a database, but for some reason I cannot get my IP directed to my site! Its literally the last step I need so I can start working on my project.
127.0.0.1 localhost
127.0.0.1 cbirc.com
<VirtualHost *:80>
ServerAdmin admin#cbirc.com
ServerName cbirc.com
ServerAlias www.cbirc.com
DocumentRoot /var/www/cbirc
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
What am I missing here???
You have to check bunch of stuff:
Is apache running
sudo service apache2 status
Afterwards, test it with
netstat -na | grep 80
ping cbirc.com
Was website enabled by issuing following command?
sudo a2ensite cbirc
file /etc/apache2/sites-enabled/cbirc.conf (or some similar name it that folder)
Does www-data group and user (apache) has read-execute access on web content folder
ln -l /var/www/cbirc
If not, change it like this
sudo chown -R www-data:www-data /var/www/cbirc
sudo chmod -R ug+rx /var/www/cbirc
Before issuing request in browser (to eliminate browser cache or security issues), do it with curl or wget
wget http://cbirc.com/
curl -v http://cbirc.com/
And look for errors in logifile
tail -f /var/log/apache2/error.log
Please refer to my debian cheetsheat for further help
Firstly, run 'dig' or 'host' on cbirc.com
Although it is in your /etc/hosts , resolv.conf may prioritise nameservers above your hosts file.
If it is pointing to your localhost, then check the you have enabled the site - a2ensite [name of vhost conf file without the .conf extention] - check by looking in /etc/apache2/sites-enabled for a symbolic link to the sites-available directory.

How can I access my Laravel app from another PC?

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.

how to create a virtual host in ubuntu 14.04

I'm using ubuntu 14.04 operating system and I want to create a virtual host before starting my php project. If any one can give me the terminal commands I would be grateful.
I have already installed necessary installations and I'm using Symfony php framework.
Open a terminal or a console and write the following commands
Go to your sites-available apache2 folder with:
cd /etc/apache2/sites-available/
Copy your 000-default.conf to another file (with the name of your site or vhost Preferably)
sudo cp 000-default.conf example.conf
Open that new file (example.conf) with your best text editor or use:
sudo nano example.conf
Change the example.conf's content with (Ignore the comments):
<VirtualHost *:80>
# 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.
ServerAdmin webmaster#mail.com
ServerName example.com
ServerAlias www.example.com
# example.com is the folder where you have to put your site's code.
# Obviously, its name can be whatever you want
DocumentRoot /var/www/html/example.com
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
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
</VirtualHost>
Enable the site as follows:
sudo a2ensite example.com
Restart your apache2 web server
sudo /etc/init.d/apache2 restart
Edit your /etc/host file with:
sudo nano /etc/hosts
Add at the end of the file, the domain example.com and www.example.com. It must look like:
127.0.0.1 localhost
...
# you can replace with your IP
127.0.0.1 example.com
127.0.0.1 www.example.com
...
Open your best web browser and write in its address bar: www.example.com or example.com
Create virtual host
sudo nano /etc/hosts
Enter Password=>
create ip and domain Name=>
127.0.0.1 testsite.lk
create project folder=>
Go to that directory comand prompt=>
sudo chmod -R 777 testsite.lk
to asign project to group=>
:~/mayura$
> sudo chown -R www-data:www-data testsite.lk/
give permission=>
:~/mayura$
sudo chmod -R 777 testsite.lk/
give document root file for vertual host
:~/mayura$
cd /etc/apache2/sites-available/
sudo nano testsite.lk.conf
<VirtualHost *:80>
DocumentRoot /home/mayura/project/testsite/web
DirectoryIndex index.php
ServerName testsite
<Directory "/home/mayura/project/testsite/web">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
</VirtualHost>
Create New project on Netbeans in that folder=>
index.php
Enable the site=>
cd /etc/apache2/sites-available
sudo a2ensite testsite.lk.conf
Apache reload=>
sudo /etc/init.d/apache2 reload
Start Browser=>
Run=>
xdg-open http://testsite.lk

Apache Virtual host shows blank page over Internet, works fine locally

Please do not answer until you read the full question :)
In a freshly created DigitalOcean CentOS 6.5 (64 bit) server, I tried creating two Apache virtual hosts - www.example-rose.com and www.example-tulip.com by issuing following commands:
Server Side Setup
# software installation
yum install httpd mod_fcgid php-cli
# added cgi.fix_pathinfo=1 in /etc/php.ini
echo "cgi.fix_pathinfo=1" >> /etc/php.ini
echo "PHP_Fix_Pathinfo_Enable 1" >> /etc/httpd/conf.d/fcgid.conf
# added new users and groups for the two virtual sites
groupadd rose
groupadd tulip
useradd -s /bin/false -d /var/www/rose -m -g rose rose
useradd -s /bin/false -d /var/www/tulip -m -g tulip tulip
chmod 755 /var/www/rose
chmod 755 /var/www/tulip
# create new document root and make them own by respective users
mkdir -p /var/www/rose/web
chown rose:rose /var/www/rose/web
mkdir -p /var/www/tulip/web
chown tulip:tulip /var/www/tulip/web
Now since I want to run PHP through SuEXEC, I have done following additional steps of creating a wrapper script for SuEXEC
mkdir -p /var/www/php-fcgi-scripts/rose
mkdir -p /var/www/php-fcgi-scripts/tulip
# inside the wrapper, I added a file (/var/www/php-fcgi-scripts/rose/php-fcgi-starter)
# with below content:
#!/bin/sh
PHPRC=/etc/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=8
exec /usr/bin/php-cgi
# changed the permission
chmod 755 /var/www/php-fcgi-scripts/rose/php-fcgi-starter
chown -R rose:rose /var/www/php-fcgi-scripts/rose
# above steps are repeated for tulip as well
Then finally I added the following content in my httpd.conf file to enable the virtual hosting
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example-rose.com
ServerAlias example-rose.com
ServerAdmin webmaster#example-rose.com
DocumentRoot /var/www/rose/web/
<IfModule mod_fcgid.c>
SuexecUserGroup rose rose
<Directory /var/www/rose/web/>
Options +ExecCGI
AllowOverride All
AddHandler fcgid-script .php
FCGIWrapper /var/www/php-fcgi-scripts/rose/php-fcgi-starter .php
Order allow,deny
Allow from all
</Directory>
</IfModule>
ErrorLog /var/www/rose/error.log
CustomLog /var/www/rose/access.log combined
ServerSignature Off
</VirtualHost>
<VirtualHost *:80>
... similar lines for the second virtual host (Tulip). Cutting short ...
</VirtualHost>
I restarted the server and put one index.php file in the web directory with following content : <?php phpinfo(); ?>.
Local Machine
In order to test the virtual hosts, I issued following commands from my local machine using curl
$> curl -v -H "www.example-rose.com" 162.*.35.155/index.php
* Trying 162.*.35.155...
* Connected to 162.*.35.155 port 80 (#0)
> GET /index.php HTTP/1.1
> User-Agent: curl/7.30.0
> Host: www.example-rose.com
> Accept: */*
>
* Empty reply from server
* Connection #0 to host 162.*.35.155 left intact
curl: (52) Empty reply from server
If I try accessing through web browser instead of curl (after creating /etc/hosts entry for the domains), I get blank page. No Apache Access log or error log is generated in the server. If I put a static file (instead of php file), I can access the static file without any issue. But .php file does not work.
However
When I try to curl from the DigitalOcean's server itself (or from some other server within DigitalOcean's network), I get to see the expected index.php with phpinfo() results without any issue. But doesn't work when I try from outside DO's network.
I have done all sorts common troubleshooting (installing, re-installing, checking permissions, checking conf files again and again etc.) and I am at my wit's end now. Next what?
Please do not answer until you read the full question :)

Categories