This tutorial shows how you can install an Apache2 webserver on a Fedora 17 server with PHP5 (through PHP-FPM) and MySQL support. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites.
I do not issue any guarantee that this will work for you!
1 Preliminary Note
In this tutorial I use the hostname server1.example.com with the IP address 192.168.0.100. These settings might differ for you, so you have to replace them where appropriate.
2 Enabling Additional Repositories
We need to install mod_fastcgi later on which is available in the RPMforge repositories. RPMforge can be enabled as follows:
rpm –import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt
rpm -ivh rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
3 Installing MySQL 5
To install MySQL, we do this:
yum install mysql mysql-server
Then we create the system startup links for MySQL (so that MySQL starts automatically whenever the system boots) and start the MySQL server:
systemctl enable mysqld.service
systemctl start mysqld.service
Set passwords for the MySQL root account:
[root@server1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we’ll need the current
password for the root user. If you’ve just installed MySQL, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] <– ENTER
New password: <– yourrootsqlpassword
Re-enter new password: <– yourrootsqlpassword
Password updated successfully!
Reloading privilege tables..
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
Remove anonymous users? [Y/n] <– ENTER
Normally, root should only be allowed to connect from ‘localhost’. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <– ENTER
By default, MySQL comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] <– ENTER
– Dropping test database…
– Removing privileges on test database…
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] <– ENTER
All done! If you’ve completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
4 Installing Apache2
Apache2 is available as a Fedora package, therefore we can install it like this:
yum install httpd
Now configure your system to start Apache at boot time…
systemctl enable httpd.service
… and start Apache:
systemctl start httpd.service
Now direct your browser to http://192.168.0.100, and you should see the Apache2 placeholder page:
Apache’s default document root is /var/www/html on Fedora, and the configuration file is /etc/httpd/conf/httpd.conf. Additional configurations are stored in the /etc/httpd/conf.d/ directory.
5 Installing PHP5
We can make PHP5 work in Apache2 through PHP-FPM and Apache’s mod_fastcgi module which we install as follows:
yum install mod_fastcgi php-fpm
Then open /etc/php.ini:
In order to avoid errors like
[08-Aug-2011 18:07:08] PHP Warning: phpinfo(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Europe/Berlin’ for ‘CEST/2.0/DST’ instead in /usr/share/nginx/html/info.php on line 2
… in /var/log/php-fpm/www-error.log when you call a PHP script in your browser, you should set date.timezone in /etc/php.ini:
[...] [Date] ; Defines the default timezone used by the date functions ; http://php.net/date.timezone date.timezone = "Europe/Berlin" [...]
You can find out the correct timezone for your system by running:
[root@server1 ~]# cat /etc/sysconfig/clock
Next create the system startup links for php-fpm and start it:
systemctl enable php-fpm.service
systemctl start php-fpm.service
PHP-FPM is a daemon process (with the init script /etc/init.d/php-fpm) that runs a FastCGI server on port 9000.
Next restart Apache:
systemctl restart httpd.service
6 Configuring Apache
To make Apache work with PHP-FPM, we need the following configuration:
<IfModule mod_fastcgi.c> DirectoryIndex index.html index.shtml index.cgi index.php AddHandler php5-fcgi .php Action php5-fcgi /php5-fcgi Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization </IfModule>
(To learn more about the FastCgiExternalServer directive, take a look at http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiExternalServer.)
You can put it in the global Apache configuration (so it’s enabled for all vhosts), for example in /etc/httpd/conf.d/fastcgi.conf, or you can place it in each vhost that should use PHP-FPM. I want to use PHP-FPM with all vhosts so I open /etc/httpd/conf.d/fastcgi.conf…
… and put the following section at the end:
[...] <IfModule mod_fastcgi.c> DirectoryIndex index.html index.shtml index.cgi index.php AddHandler php5-fcgi .php Action php5-fcgi /php5-fcgi Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization </IfModule>
The /usr/lib/cgi-bin/ directory must exist, so we create it as follows:
If mod_php is installed and enabled, we need to disable it. Open /etc/httpd/conf.d/php.conf…
… and comment out the AddHandler and AddType lines:
# # PHP is an HTML-embedded scripting language which attempts to make it # easy for developers to write dynamically generated webpages. # <IfModule prefork.c> LoadModule php5_module modules/libphp5.so </IfModule> <IfModule !prefork.c> LoadModule php5_module modules/libphp5-zts.so </IfModule> # # Cause the PHP interpreter to handle files with a .php extension. # #AddHandler php5-script .php #AddType text/html .php # # Add index.php to the list of files that will be served as directory # indexes. # DirectoryIndex index.php # # Uncomment the following line to allow PHP to pretty-print .phps # files as PHP source code: # #AddType application/x-httpd-php-source .phps
Restart Apache afterwards:
systemctl restart httpd.service
Now create the following PHP file in the document root /var/www/html of the default Apache vhost:
<?php phpinfo(); ?>
Now we call that file in a browser (e.g. http://192.168.0.100/info.php):
As you see, PHP5 is working, and it’s working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don’t have MySQL support in PHP5 yet.
7 Getting MySQL Support In PHP5
To get MySQL support in PHP, we can install the php-mysql package. It’s a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:
yum search php
Pick the ones you need and install them like this:
yum install php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy
APC is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It’s similar to other PHP opcode cachers, such as eAccelerator and Xcache. It is strongly recommended to have one of these installed to speed up your PHP page.
APC can be installed as follows:
yum install php-pecl-apc
Now reload PHP-FPM:
systemctl reload php-fpm.service
Now reload http://192.168.0.100/info.php in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:
phpMyAdmin is a web interface through which you can manage your MySQL databases. It’s a good idea to install it:
yum install phpmyadmin
Now we configure phpMyAdmin. We change the Apache configuration so that phpMyAdmin allows connections not just from localhost (by commenting out the <Directory “/usr/share/phpmyadmin”> stanza):
# # Web application to manage MySQL # #<Directory "/usr/share/phpmyadmin"> # Order Deny,Allow # Deny from all # Allow from 127.0.0.1 #</Directory> Alias /phpmyadmin /usr/share/phpmyadmin Alias /phpMyAdmin /usr/share/phpmyadmin Alias /mysqladmin /usr/share/phpmyadmin
Next we change the authentication in phpMyAdmin from cookie to http:
[...] /* Authentication type */ $cfg['Servers'][$i]['auth_type'] = 'http'; [...]
systemctl restart httpd.service
Afterwards, you can access phpMyAdmin under http://192.168.0.100/phpmyadmin/:
- Apache: http://httpd.apache.org/
- Apache Module mod_fastcgi: http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html
- PHP: http://www.php.net/
- PHP-FPM: http://php-fpm.org/
- MySQL: http://www.mysql.com/
- Fedora: http://fedoraproject.org/
- phpMyAdmin: http://www.phpmyadmin.net/