Apache+SSL, PHP, and MySQL
Updated: 07/25/2006
General Information
I'm sure many of you have been wondering how people host secure sites using Secure Sockets Layer (SSL). This guide will show you how to set up a web server with SSL, PHP, and MySQL support.Requirements
Installation
Section A -- Apache+mod_ssl
First thing we need to do is install the Apache web server. Currently there are two main versions available: 1.3.x and 2.x. I will be teaching from the 1.3x branch, but many of the steps are the same for 2.x. I will also make notes for those of you who choose to use the 2.x branch.|
# # |
cd /usr/ports/www/apache13-modssl make install distclean |
|
# # |
echo 'apache_enable="YES"' >> /etc/rc.conf echo 'apache_flags="-DSSL"' >> /etc/rc.conf |
Note: For Apache2 users: You only need to install the apache2 port, but then you have to manually create the directories for the SSL Certificate and Key.
|
# # # # # # # # |
cd /usr/ports/www/apache2 make install distclean echo 'apache2_enable="YES"' >> /etc/rc.conf echo 'apache2_flags="-DSSL"' >> /etc/rc.conf mkdir /usr/local/etc/apache2/ssl.key mkdir /usr/local/etc/apache2/ssl.crt chmod 0700 /usr/local/etc/apache2/ssl.key chmod 0700 /usr/local/etc/apache2/ssl.crt |
Section B -- MySQL
|
# # # |
cd /usr/ports/databases/mysql41-server make install WITH_OPENSSL=yes distclean echo 'mysql_enable="YES"' >> /etc/rc.conf |
Section C -- PHP
|
# # |
cd /usr/ports/lang/php4 make config |
|
# # # |
make install distclean cd /usr/ports/lang/php4-extensions make install distclean |
|
# nano -w /usr/local/etc/apache/httpd.conf AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps |
Configuration
Section A -- Create Certificate
It is now time to create your own certificate using the openssl utility. Now, you need to understand that one server can hold multiple certificates, but only one per listening IP address. So, if your server is listening on one IP address, you can only have one certificate for the server. All of your virtual domains can share the same certificate, but clients will get warning prompts when they connect to a secure site where the certificate does not match the domain name. If your server is listening on multiple IP addresses, your virtual hosts have to be IP-based -- not name-based. This is something to consider when creating your certificate.|
# # |
cd ~ openssl genrsa -des3 -out server.key 1024 |
| # | openssl req -new -key server.key -out server.csr |
| # | openssl x509 -req -days 365 -in /root/server.csr -signkey /root/server.key -out /root/server.crt |
|
# # |
cp ~/server.key /usr/local/etc/apache/ssl.key/ cp ~/server.crt /usr/local/etc/apache/ssl.crt/ |
Note: Apache2 users: The correct permissions must be set.
|
# # |
chmod 0400 /usr/local/etc/apache2/ssl.key/server.key chmod 0400 /usr/local/etc/apache2/ssl.crt/server.crt |
Section B -- Configure VirtualHosts
VirtualHosts are neat because they allow you to host many domains on the same server and the same IP address. For this example, we will make three VirtualHost entries -- one for http and two for https (SSL)./usr/local/etc/apache/httpd.conf so you can pull that up in your favorite editor now. The normal VirtualHosts can be placed at the beginning of the file for easy access and should be set up like this:|
ServerName domain.tld NameVirtualHost 192.168.0.2:80 <VirtualHost 192.168.0.2:80> ServerName domain.tld ServerAlias www.domain.tld ServerAdmin admin@domain.tld DocumentRoot /path/to/website/files </VirtualHost> |
|
NameVirtualHost 192.168.0.2:443 <VirtualHost _default_:443> |
|
<VirtualHost 192.168.0.2:443> ServerName domain.tld ServerAlias www.domain.tld ServerAdmin admin@domain.tld DocumentRoot /path/to/website/files SSLEngine on SSLCertificateFile /usr/local/etc/apache/ssl.crt/server.crt SSLCertificateKeyFile /usr/local/etc/apache/ssl.key/server.key </VirtualHost> |
|
<VirtualHost 192.168.0.3:443> ServerName domain2.tld ServerAlias www.domain2.tld ServerAdmin admin@domain2.tld DocumentRoot /path/to/website/files SSLEngine on SSLCertificateFile /usr/local/etc/apache/ssl.crt/server2.crt SSLCertificateKeyFile /usr/local/etc/apache/ssl.key/server2.key </VirtualHost> |
Note: Apache2 users: All of your SSL configuration is in a separate file at /usr/local/etc/apache2/ssl.conf so edit that for your SSL-aware VirtualHosts.
Section C -- Start Services
Your server is now ready to start MySQL and Apache with SSL.|
# # |
/usr/local/etc/rc.d/mysql-server.sh start /usr/local/sbin/apachectl startssl |
|
# # # |
cd /usr/local/etc/apache/ssl.key cp server.key server.key.orig openssl rsa -in server.key.orig -out server.key |
Author: Jon LaBass
jon at bsdguides dot org