0

I am receiving an error "Connection error: no such file or directory" when trying to load PHP files that have a connection to my database. My PHP files that do not have a connection to the database load without a problem. I have Apache running

I found some articles online to that suggest I may need to edit my php.ini file and also tried to switch 'localhost' in my db_Connect file to my public IP address - no luck so far however.

Here is my db connection:

<?php 

    // connect to the database
    $conn = mysqli_connect('localhost', 'username', 'password', 'database');

    // check connection
    if(!$conn){
        echo 'Connection error: '. mysqli_connect_error();
    }

?>

Here are some logs from today:

[Mon May 25 12:55:06.855800 2020] [php7:warn] [pid 12713] [client 108.24.134.163:59698] PHP Warning:  mysqli_connect(): (HY000/2002): No such file or directory in /home/ubuntu/wescreen/config/db_connect.php on line 4, referer: http://wescreen.tv/index2.php
[Mon May 25 12:55:36.367547 2020] [php7:warn] [pid 12712] [client 108.24.134.163:59712] PHP Warning:  mysqli_connect(): (HY000/2002): No such file or directory in /home/ubuntu/wescreen/config/db_connect.php on line 4, referer: http://wescreen.tv/index2.php

Any help is greatly appreciated!

1 Answers1

0

First of all check your /etc/hosts file first and check if there is a url of your site pointing to your local host. It should be something like this

  127.0.0.1 localhost
  127.0.0.1 test.com

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters``

Then, head to your folder in /etc/apache2/sites-available and inside the file 000-default.conf you will find these lines:

     <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.
#ServerName www.example.com

ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

# 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>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Normally, the DocumentRoot is where your project's directory lies. Try and validate that is true. Check your file ports.conf inside the /etc/apache2/ directory. You will find these lines:

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf

Listen 80

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Check that the ports you are using are successfully running by executing this command:

  netstat -anp | grep apache

Once you have it done, restart your apache service with this command in your terminal:

sudo service apache2 restart

Hopefully, you're good to go.

Oris Sin
  • 1,023
  • 1
  • 13
  • 33
  • Thank you for the response @orissin. I followed all your instructions as specified and did not have luck. The only thing that I noticed different in my host file is that I also have an additional line: `ff02::3 ip6-allhosts` at the bottom of the file – Ethan Messinger May 25 '20 at 14:52
  • Check my editted answer, it might give you a better hint why your localhost is not working. – Oris Sin May 25 '20 at 15:10
  • @orinsin thank you again for your response. My ports.conf file matches what you have above. When I ran the command I see the following: `tcp6 0 0 :::80 :::* LISTEN 14027/apache2 ` – Ethan Messinger May 25 '20 at 15:45