Database

How to SELinux allow Apache or PHP-FPM connect remote MySQL

If you have trouble apache or PHP-FPM can not connect to remote MySQSL server, example error on PDO connection on remote server like below

SQLSTATE[HY000] [2002] Permission denied

First you need to check is SELinux enabled with this command:

[root@serverdiary ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          disabled
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

Or you can see on /etc/selinux/config

SELINUX=enforcing

There are 3 options for SELinux:

  1. enforcing – SELinux security policy is enforced.
  2. permissive – SELinux prints warnings instead of enforcing.
  3. disabled – No SELinux policy is loaded.

You can change to permissive, so SELinux only prints warnings instead of enforcing.

[root@serverdiary ~]# setenforce 0
[root@serverdiary ~]# sestatus
SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          disabled
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      31

Or, if you still want to use option on SElinux config, apply this command as root and restart apache or php-fpm.

setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1

P option means permanent, so after reboot, this option still applied.

View Comments

  • Kesinlikle şifre talep etmiyor, bir uygulamaya giri yapmanızı istemiyoruz. Bu şekilde sizden şifre talep eden yerlerden alışveriş yapmamanzı tavsiye ediyoruz.

Recent Posts

How to fix yum update error thread.error: can’t start new thread

If you found error thread.error: can't start new thread on yum update command on CentOS…

5 months ago

How to securing Cockpit login with Google Two Factor Authenticator 2FA

Cockpit is a web-based graphical interface for servers, intended for everyone, especially those who are:…

8 months ago

How to install Cockpit on CentOS 7 / CentOS 9 Stream and configure Nginx reserve proxy

From cockpit-project.org, Cockpit is a web-based graphical interface for servers, intended for everyone, especially those…

11 months ago

How to install and configure Nginx with HTTP3 on CentOS 9 Stream / RHEL 9

We have been using Nginx with HTTP3 for more than 1 year on our production…

11 months ago

How to sync date time using Crony on CentOS 9 Stream / RHEL 9

On CentOS 7, to sync date time we often use NTPD. But on CentOS 9,…

11 months ago

How to install and enable REMI repository on CentOS 9 Stream

Remi repository is one of third-party repository that have latest update of PHP on Enterprise…

11 months ago