Here is an interesting issue I ran into today while setting up a new VM. I had copied the Apache conf file and SSL/TLS certificate files onto my new server as root using wget. All of the file's ownerships and permissions looked right and I thought I was ready to go. Then I ran into these two misleading errors:
Could not open configuration file /etc/httpd/conf.d/ssl.conf: Permission denied
and
SSLCertificateFile: file '/etc/pki/tls/certs/mydomain.com.crt' does not exist or is empty
Contrary to what Apache reported, the ssl.conf file did have the correct permissions and the crt file did exist and had contents. So what gives? Our trusty SELinux friend is at it again, it seems. Because I had copied these files in from somewhere else, SELinux was detecting something off. It is an easy fix, though. Just run this command, specifing each of the files above:
restorecon -v /etc/httpd/conf.d/ssl.conf
and
restorecon -Rv /etc/pki/tls/certs/
That -R is for "recursive" and it means I want it to do the whole directory.
After fixing up the SELinux permissions for these files I brought in, everything started up just fine!
Showing posts with label selinux. Show all posts
Showing posts with label selinux. Show all posts
Saturday, June 15, 2013
Friday, August 5, 2011
SE Linux and "Can't connect to MySQL server"
I've been setting up a server for the last few days for production. It is to be highly secure, so it is a real pain. Anyway, I ran into this problem where I could not connect to the remote database. It was driving me nuts. (I'm using CentOS by the way, and I was setting up a web server to connect to a remote MySQL database server.)
First, I checked that I was getting network connectivity by doing
telnet 10.0.0.1 3306
from the command line (where 10.0.0.1 is the IP of the database server). And, I got some gobbledygook that had the words "MySQL" in it, so I know that worked and the network was there.
Then, I checked the database's user list, to make sure connections were allowed from the user at my host. I did this by going into MySQL's CLI and entering
SELECT `Host`,`User` FROM mysql.user WHERE 1;
This printed out a list of all the users so I just made sure it and the host were correct. (There are more MySQL troubleshooting tips here.)
Then it finally hit me. What always causes problems for hours on end? SE Linux!! I know it is good for my server, but it sure does drive me nuts sometimes. If you want your web server to be able to connect to a remote DB, you have to give it rights with SE Linux. Run this at the command line:
setsebool -P httpd_can_network_connect_db on
and for good measure:
setsebool -P httpd_can_network_connect on
Update: HA HA! I've had this problem before and completely forgot about it.
Labels:
database,
mysql,
mysql_connect,
php,
selinux
Tuesday, February 15, 2011
VSFTPD & SELinux
This can be a fun combo to work with. SELinux, or Security Enhanced Linux, is the life of any party. And Google searches about SELinux-related problems makes it pretty evident that very few people have taken the time to understand how this program works. I ran across numerous people simply suggesting that you turn off SELinux if it is getting in the way.
Well, I didn't buy into this wholesale approach to getting things to work. Besides, I want a secure system and if SELinux is going to help me in the long run, I want it enabled.
VSFTPD, or Very Secure FTP Daemon, is a pretty standard FTP server. You can install it on CentOS systems (or RHEL, for that matter) by running "yum install vsftpd" from the command line. Once you get it installed you can make changes to the configuration file at "/etc/vsftpd/vsftpd.conf" using a file editor. Also, to make sure it is always running in the background run "chkconfig vsftpd on" and "service vsftpd restart" from the command line.
Make sure the configuration file is set up properly. Only give FTP access to a limited number of users (never include root), disable anonymous access and make sure users can only access the files they need (I recommend chroot-ing them).
This is only half the battle. Now that VSFTPD is running, how do you allow users to access their home directories? SELinux usually will get in the way of this. Well, there is a SELinux setting for this called "ftp_home_dir" and this will allow users to access their home directory via FTP. To set this, from the command line run
Be sure to also check file permissions and file ownership, if you run into problems. A file must be writable for everyone if they do not specifically own it.
If this fails to grant you FTP access where you need it, or your set-up is slightly different, you can always allow the FTP daemon full access to all files by running
This is granting a bit more power to the FTP daemon than is necessary, but it is much better than just disabling SELinux all-together.
By the way, here is a great intro to CentOS & SELinux.
Well, I didn't buy into this wholesale approach to getting things to work. Besides, I want a secure system and if SELinux is going to help me in the long run, I want it enabled.
VSFTPD, or Very Secure FTP Daemon, is a pretty standard FTP server. You can install it on CentOS systems (or RHEL, for that matter) by running "yum install vsftpd" from the command line. Once you get it installed you can make changes to the configuration file at "/etc/vsftpd/vsftpd.conf" using a file editor. Also, to make sure it is always running in the background run "chkconfig vsftpd on" and "service vsftpd restart" from the command line.
Make sure the configuration file is set up properly. Only give FTP access to a limited number of users (never include root), disable anonymous access and make sure users can only access the files they need (I recommend chroot-ing them).
This is only half the battle. Now that VSFTPD is running, how do you allow users to access their home directories? SELinux usually will get in the way of this. Well, there is a SELinux setting for this called "ftp_home_dir" and this will allow users to access their home directory via FTP. To set this, from the command line run
setsebool -P ftp_home_dir 1
Be sure to also check file permissions and file ownership, if you run into problems. A file must be writable for everyone if they do not specifically own it.
If this fails to grant you FTP access where you need it, or your set-up is slightly different, you can always allow the FTP daemon full access to all files by running
setsebool -P allow_ftpd_full_access 1
This is granting a bit more power to the FTP daemon than is necessary, but it is much better than just disabling SELinux all-together.
By the way, here is a great intro to CentOS & SELinux.
Wednesday, October 20, 2010
PHP to Remote Database Error: "Can't connect to MySQL server on..."
On a new server migration project I am working on I have separate web and database servers. So, I use a private network to connect them. It took me a day to get working, so I am putting up a post to help others in similar situations.
First, just to clarify, I knew it was a web server problem (and not a network or db permissions issue), because I could connect to the MySQL server just fine from the MySQL command line on the web server. But, when I pinged the PHP web page from my browser I'd get this error:
Can't connect to MySQL server on '10.1.1.1' (13)
The problem was SELinux, which was installed and crackin' down on Apache rights.
One simple fix:
setsebool -P httpd_can_network_connect=1
More information can be found in this nice article:
http://www.ehow.com/how_2090983_connect-remote-database-under-selinux.html
SELinux would allow me to connect from the command line just fine. I could even execute the PHP script from the command line and connect fine. But SELinux did not like it when Apache tried to connect to a computer on the network. Hence the policy update, as stated above.
If you have other issues or it appears that your db server is having issues, here are some other MySQL debug tips as well.
First, just to clarify, I knew it was a web server problem (and not a network or db permissions issue), because I could connect to the MySQL server just fine from the MySQL command line on the web server. But, when I pinged the PHP web page from my browser I'd get this error:
Can't connect to MySQL server on '10.1.1.1' (13)
The problem was SELinux, which was installed and crackin' down on Apache rights.
One simple fix:
setsebool -P httpd_can_network_connect=1
More information can be found in this nice article:
http://www.ehow.com/how_2090983_connect-remote-database-under-selinux.html
SELinux would allow me to connect from the command line just fine. I could even execute the PHP script from the command line and connect fine. But SELinux did not like it when Apache tried to connect to a computer on the network. Hence the policy update, as stated above.
If you have other issues or it appears that your db server is having issues, here are some other MySQL debug tips as well.
Labels:
apache,
mysql,
mysql_connect,
php,
selinux
Subscribe to:
Posts (Atom)