MySQL cannot connect via localhost

On an Apache server, MySQL can’t connect when you use localhost, but it work when you chane to IP address

When you use “localhost”, it use socket for connecting to MySQL server, this is faster than using TCP/IP connection, that is used when you use IP address to connect to MySQL server.

First find out socket path. To do this login to MySQL server, run

mysql socket

See if you can connect using this socket with command

login to mysql using socket

In this cause, login to MySQL using socket worked.

I created a simple test PHP script to verify MySQL connection, it was able to connect to MySQL server using “localhost”.

Same script did not work when i try access it using web server. So the problem is web server user not able to connect to MySQL socket. You need to check permission for socket and parrent folders. In this case problem is fixed by running

You can verify enabling SSH access for web server user, then connect to MySQL using command line or try access socket file as apache user.

MySQL Socket Path in php.ini

When a PHP application use localhost to connect, PHP find location of socket from php.ini, you need to verify this path set in php.ini is same as the socket path used by MySQL server.

If path is differnt, you need to make it same. You can either modify php.ini or MySQL server config file.


Find IP with Most Access from Apache Log

To find IP with most access from Apache or other web server log file, run

If you want to see IP that made most POST request

See Hacked log


Apache Show Real IP Address when using CloudFlare

When using Apache web server behind cloudflare, apache logs show cloudflare IP address instead of real visitor IP address. To show actual visitor IP address, you need to install mod_cloudflare apache module.

Before you can install the module, you need to install following requirments.

On Debian/Ubuntu server,

Now install mod_cloudflare with

Restart apache web server with

Verify mod_cloudflare apache module is loaded with

apache cloudflare module


Redirect site from www to non-www

It is better to make web site available with one URL. Many sites work with both wwww and non-www (naked domain) urls.

Using www or non-www is personal choice. One advantage of using wwww for URL is when you have lot of sub domains. If you use non-www url, cookies set by the domain will be available to sub domains. This will increase bandwidth usage as cookie need to be sent with every request browser make to web server.


If you are using Apache web server, you can redirect wwww to non-www url by adding following code in .htaccess file

Redirect non-www to www


If you use Nginx, it is better create a server entry for www URL, then set a redirect

If you want to use same server entry for www and non-www, add following code to nginx server entry for the web site.

Redirect www domain to non-www

If you use custom ports, use

Redirect Naked Domain to www

Related Posts




Apache Show Real IP Behind Reverse Proxy on CentOS

When Apache web server running behind reverse proxy or load balancer, server log and scripts show IP of reverse proxy server or load balancer as IP of visitor. To fix this, you need to configure revese proxy or load balancer to forward Real IP of visitor on Header X-Forwarded-For, this most load balacner do by default.

Edit Apache configuration file



Doing this will make PHP scripts show real IP of visitor. You need to restart Apache web server before the change take effect. You can verify by creating a PHP script with content

To make Apache show real IP in access log, edit


Replace with

Restart Apache web server

Disable Apache Error log in ISPConfig

ISPConfig is a free hosting control panel. It come with Nginx and Apache web servrs. You can select one during installation.

if you are using Apache web server with ISPConfig and want to disable Apache Error logs, then do the following

Now restart Apache

This is not a permanant solution as ISPCOnfig will rewrite apache configuration when you make changes to web site. I had to do this for a server which have too many sites writing errors to error_log, causing high IO load. Proper solution is to fix errors, until errors can be fixed, this is a quick fix, that will reduce IO usage due to error_log.

Make sure you make a copy of files before you run the sed command that modify all apache config, so in cuase anything happens, you can revert back.

Redirect HTTP to HTTPS when using Reverse Proxy

When you are using Reverse Proxy like Nginx, Haproxy or Amazon ELB in front of web server and web server use HTTP to serve all traffic, you can use normal redirect code based HTTPS variable to do the redirect to HTTPS. You need to use X-Forwarded-Proto to do the redirect.

For Apache, add following code to .htaccess to Apache Virtual Host entry.

For Nginx, add following to server entry for the domain name

For IIS edit web.config, add following to section.