Sunday, April 21, 2013

How do I install the latest versions of Apache, MySQL, and PHP on a Debian based Linux machine?

What up! So you want to know how to install the latest versions of Apache, MySQL, and PHP on a Debian based Linux machine? Sweet. Me too.

If you need help getting a Debian based Linux machine online, check out a post I did the other day titled: How do I set up a Next Generation Rackspace Cloud Server running Debian 7 (Wheezy)?

Okay, slap yourself in the face real quick. It's time to get started!

SSH into your machine.

Then walk through the following post: How do I install mysql-client, mysql-server, and libmysqlclient-dev on a Next Generation Rackspace Cloud Server running Debian 7 (Wheezy)? Note that you can skip the libmysqlclient-dev piece.

Great. You're back. Okay, now run the following command:
sudo aptitude install apache2
You'll be prompted to "ok" the amount of space that will be used after unpacking the archives. Enter "yes" to proceed.

Apache's default document root is /var/www on Debian, and the configuration file is /etc/apache2/apache2.conf. Additional configurations are stored in subdirectories of the /etc/apache2 directory such as /etc/apache2/mods-enabled (for Apache modules), /etc/apache2/sites-enabled (for virtual hosts), and /etc/apache2/conf.d.

If you enter your server's IP address into your browser, you'll see that we're on fire!
Okay, now on to installing php:
sudo aptitude install php5
PHP 5.4 will be installed. If you're setting up a Debian 6.0 (Squeeze) box and require php 5.4 (like I used to) then splash some hot coffee in your face because we've got an extra couple of steps to perform!

Run the following command:
sudo nano /etc/apt/sources.list
Add the following lines to the end of the file:
# http://www.dotdeb.org/instructions/ (but only for php54)
deb http://packages.dotdeb.org squeeze-php54 all
deb-src http://packages.dotdeb.org squeeze-php54 all
Now save the file. Next, run the following two commands:
wget http://www.dotdeb.org/dotdeb.gpg
cat dotdeb.gpg | sudo apt-key add -
After doing that, we can now update our list of available packages via the following command:
sudo aptitude update
Ok, now we can install php 5.4 via the following command:
sudo aptitude install php5
Boom goes the dynamite! We can verify our install by running the following command:
php -v
Which will output:
PHP 5.4.14-1~dotdeb.1 (cli) (built: Apr 21 2013 05:21:34)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
Okay, sweet! Now that we have php installed we can put a dot php file in apache's default document root (/var/www), restart apache, and then test our new setup. To begin, run the following command:
sudo nano /var/www/info.php
Then enter:
<?php
phpinfo();
Save the file. Now restart apache (since we haven't done so yet since installing php) via the following command:
sudo /etc/init.d/apache2 restart
Now access your new info.php page. You'll see the following:
You'll see we now have php working via the "Apache 2.0 Handler" (see "Server API" line). If you do a search for "mysql" you'll see that "mysql" is nowhere to be found! We'll need to fix that (as well as install other useful php modules). To see your options, run the following command:
aptitude search php5
I recommend installing php5-mysql, php5-curl, php5-gd, php-pear, php5-imagick, php5-imap, php5-mcrypt, php5-memcache, php5-sqlite, php5-tidy, php5-xmlrpc, and php5-xsl via the following command:
sudo aptitude install php5-mysql php5-curl php5-gd php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-sqlite php5-tidy php5-xmlrpc php5-xsl
Okay sweet. Now we're talking! Revisit your info.php page (aptitude will have already restarted apache for you), do a search for "mysql" and then revel in your success! You are on fire!
Okay, now if you're anything like me you'd like a web interface to your remote database. PHPMyAdmin has you covered... but only if we install it! Get started with the following command:
sudo aptitude install phpmyadmin
As usual you'll be prompted about incoming file size. Enter "yes" when prompted. Next, you'll be presented with the following package configuration screen:

Select "apache2". You'll then be prompted with a second package configuration screen:

Select "No". And boom! You've got PHPMyAdmin installed! Not! There's actually one more step. To see for yourself visit your /phpmyadmin page and observe the 404!
To fix this. Run the following command:
sudo nano /etc/apache2/conf.d/phpmyadmin.conf
Then add the following line to the file:
Include /etc/phpmyadmin/apache.conf
Save the file. Then, restart apache via the following command:
sudo /etc/init.d/apache2 restart
Now revisit your /phpmyadmin page and observe your awesomeness!
Now that we've got our system set up properly we can remove our info.php file via the following command:
sudo rm /var/www/info.php
Now das wassup! Congrats, you're done! You successfully installed the latest versions of Apache, MySQL, and PHP.

If you're interested in getting your Apache hosting environment set up you should check out a post I wrote the other day titled: How do I set up Apache virtual hosts on a Debian based Linux machine and configure to support HTTPS?

No comments:

Post a Comment

About Me

My photo
I code. I figured I should start a blog that keeps track of the many questions and answers that are asked and answered along the way. The name of my blog is "One Q, One A". The name describes the format. When searching for an answer to a problem, I typically have to visit more than one site to get enough information to solve the issue at hand. I always end up on stackoverflow.com, quora.com, random blogs, etc before the answer is obtained. In my blog, each post will consist of one question and one answer. All the noise encountered along the way will be omitted.