Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5102691
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: 系统运维

2006-04-05 23:18:52

            Apache+SSL, PHP, and MySQL


                    
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

In order for public access to your website, you must have a valid domain name.
A text editor (for this guide we will use Nano)
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.0.x.  I will be teaching from the 1.3x branch, but many of the steps are the same for 2.0.x.  I will also make notes for those of you who choose to use the 2.0.x branch.

#cd /usr/ports/www/apache13-modssl
#make install distclean

Apache now gets started on system boot from rc.conf so let's add the respective entry:

# 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/mysql40-server
#make install WITH_OPENSSL=yes distclean

Take a break while it downloads, compiles, and installs.  It'll take about 45 minutes on a K6-2 350MHz.

Section C -- PHP:

#cd /usr/ports/www/mod_php4
#make install distclean
#cd /usr/ports/lang/php4-extensions
#make install distclean

Now, when you get to the PHP configuration screen, you just need to check the OpenSSL box and leave the rest of the default values alone, unless you plan on installing other applications, such as the IMP Webmail, that require other PHP modules.  Time to take another break.

PHP should be installed by now.  At the end of the installation, you will need to edit Apache's configuration file to add two lines after all the "LoadModule" lines for PHP support.

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

Change to any directory you would like to save your certficate in.  I chose root's home directory.  We will then copy the necessary files to the correct directory later.  This way we have a back up in case something happens.

# cd ~
# openssl genrsa -des3 -out server.key 1024

You will be prompted to enter a password for this key.  Remember it because we will need it later.  Now we need to make a Certificate Signing Request (CSR) from the key we just generated

#  openssl req -new -key server.key -out server.csr

Enter your password you had used as this is where you get to enter all the fun information about the certificate, like your name and Fully Qualified Domain Name (FQDN).  Make sure you enter your FQDN for the "Common Name" portion.  For example, if the certificate is for then your CommonName should be webmail.domain.tld.

Alright, your certificate is ready to be signed.  The following steps are to self-sign the certificate, but you could pay money and have it signed by Verisign or Thawte.

#  openssl x509 -req -days 365 -in /root/server.csr -signkey /root/server.key -out /root/server.crt

Ok, your certificate is signed and valid for 365 days, which you could have changed if you wanted.  We now need to copy the files to the appropriate directory for Apache to use them.

#cp ~/server.key /usr/local/etc/apache/ssl.key/

#cp ~/server.crt /usr/local/etc/apache/ssl.crt/



If you want to read more about SSL Certificates, you can read the FAQs at

** 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).

This section will be modifying /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

;
     ServerName domain.tld
     ServerAlias
     ServerAdmin admin@domain.tld
     DocumentRoot /path/to/website/files
;



Now at the bottom of httpd.conf, you should see a whole bunch of lines relating to SSL.  Insert the following line just before the default VirtualHost for SSL like this:

NameVirtualHost 192.168.0.2:443

;



NameVirtualHost tells Apache that there are several virtual hosts under the same IP.  So, at the bottom of httpd.conf you will want to put your VirtualHosts just before .


;
     ServerName domain.tld
     ServerAlias
     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
;


Now, if you had a server listening on another IP address, you could set up another certificate for that IP address to use.  Then, your second VirtualHost could look like this:

;
     ServerName domain2.tld
     ServerAlias
     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
;

If you notice, SSLCertificateFile and SSLCertificateKeyFile are only paths to the certificate and key.  Just remember that you would have to use IP-based VirtualHosts, like we did, and not name-based.

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

When you start apache with ssl, you will be prompted to enter that password you were supposed to remember.  The reason for entering it everytime apache starts is because the RSA private key is stored in encrypted format.  You can remove the encryption to eliminate the password prompt if you would like, but it's not recommended for security reasons.  If you removed the encryption and somebody was able to control your box, they could take your certificate and impersonate you.  But, if you are annoyed by the password prompt and feel confident that your server is secure, these are the steps to remove the encryption:


#cd /usr/local/etc/apache/ssl.key
# cp server.key server.key.orig
# openssl rsa -in server.key.orig -out server.key

Point your favorite browser to and you should have a 128-bit secure connection.  That's all there is to setting up a standard web server with SSL support.  Happy hosting!









Comments

ManDude on March 05, 2004 at 3:48:19 pm MST
Thanks for the info. I'll give it a go later.

Hovi on July 28, 2004 at 10:27:04 am MST
Hey i dont get the options to install anything else with mod_php4.How can i install mod_php4 with some of the options it had before (that i could have picked but now cant)?
thnx

Jon on July 28, 2004 at 1:50:19 pm MST
As of 07/19/2004, the php4 and php5 port structure has changed.  The lang/php4, lang/php5, www/mod_php4, and www/mod_php5 ports are only the "base" php.  You can install individual php extensions under the names of php4-; or you can install php4-extensions which will give you the familiar look and feel that you are used to.

kingsz1 on December 25, 2004 at 7:46:26 pm MST
I got a error message whene I completed the installation and try to start the mysql:
#mysql
ERROR 2002:Can't connect to local MySQL server through cocket '/tmp/mysql.sock'(2)

How can I solve this problem?


Jon on December 26, 2004 at 7:40:42 pm MST
You do not have mysql running when you tried to access the mysql commandline.  You first need to run

# /usr/local/etc/rc.d/mysql-server.sh start
and then you will be able to connect to mysql.
阅读(1554) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~