Showing posts with label apache. Show all posts
Showing posts with label apache. Show all posts

Wednesday, January 7, 2015

Linux Tutorial: How to Build Apache, PHP5 and MySQL From Source

I’m going to show you how to do it the hard way, building everything from source, the way Linus intended. Let’s get started.

Step 1: Determine if it’s right for you.

This is not the only way to get a LAMP server going and not always the best way. You can get these files from your package manager in your favorite distribution or use something like XAMPP like I mentioned in previous articles. For most developers this a perfectly acceptable way to do it, and likely a better use of your time. Here are the reasons you would want to build from source:
You should only build from source if:
  • You have a good idea of what you’re doing.
  • You have time and patience.
  • You want the latest and greatest stuff and your package manager doesn’t have it yet.
  • You want to do something really custom and need to change crucial options.
  • You want full control over the installation details.
  • You need the maximum performance and stability possible.
  • You want to learn something.
If those don’t apply to you, then head over to this site and get XAMPP, it’s an awesome project.

Step 2: Get Sources.

Here is all the stuff you’re going to need. I put asterisks next to the “maybe” stuff that you might not need depending on how updated your system is.
Apache:
PHP:
MySQL:
Cmake: *
Mcrypt: *
APR:
PCRE:
Additional reading about setting up an Apache server can be found here:
Verify your files!
Make sure and download the .asc file also and do a quick checksum on it. This extra few seconds is well worth the benefit. Here is how you do it with Apache:
You should verify all your files before building, just to make sure you didn’t get a corrupt file.
Notes before you start:
You will see version numbers here, and those numbers are for the latest version of the software available when I wrote this. By the time you read this, they will have changed. If you want you can get these exact version numbers and build them exactly as in the article, but usually the newest version of the software is best.
I use “nice -20” before running make to ensure it gets priority. You don’t have to do this, and usually its not even required as most kernels will prioritize it pretty well.
You do not have to be root to build this stuff, in fact you shouldn’t be. You only need root privileges to install the software but you can build it with a regular user.

Step 3: Start Building

Install cmake:
./configure –prefix=/usr –mandir=/usr/share/man gmake
Install MySql:
groupadd mysql useradd -g mysql mysql
tar zxvf mysql-5.5.24.tar.gz (file name will differ)
cd mysql-5.5.24 ./configure –prefix=/usr/local/mysql make make install cd /usr/local/mysql chown -R mysql . chgrp -R mysql . scripts/mysql_install_db –user=mysql chown -R root . chown -R mysql var cp support-files/my-medium.cnf /etc/my.cnf
bin/mysqld_safe –user=mysql &
This will install your MySQL server. If you have issues getting it working, consult this page and follow those instructions.
Install Mcrypt
bunzip2 libmcrypt-2.5.8.tar.bz2 tar xvf libmcrypt-2.5.8.tar cd libmcrypt-2.5.8 ./configure –prefix=/usr –mandir=/usr/share/man nice -20 make make install
Install APR
tar xvzf apr-1.4.6.tar.gz /usr/src/http-2.4.2/srclib cd apr-1.4.6 ./configure nice -20 make
APR Utils:
tar xvzf apr-util-0.9.19.tar.gz ./buildconf –with-apr=/usr/src/apr-1.4.6/ ./configure
Install PCRE
tar xvzf pcre-8.30.tar.gz ./configure nice -20 make make install
Install Apache
cd httpd-2.4.2 CFLAGS=”-O2” ./buildconf –with-apr-util=/usr/src/httpd-2.4.2/srclib/apr-util-1.4.1 –with-apr=/usr/src/httpd-2.4.2/srclib/apr-1.4.6/ –enable-so –prefix=/usr/local/apache2 CFLAGS=”-O2” ./configure nice -20 make make install
Install PHP
We are going to install PHP 5.4.3 which was the most current at the time of this writing.
tar -xvzf php-5.4.3.tar.gz cd php-5.4.3
Note: The configure paramaters are where you really customize your installation. The following is the one I used for my machine, but your needs may be different. I will show you two of them and if mine doesn’t work, you can use a failsafe one.
My config:
./configure –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql –with-curl=/usr/bin/curl –enable-bcmath –enable-calendar –with-curl=/usr/bin/curl –with-gdbm –enable-exif –enable-ftp –with-gd –with-freetype-dir=/usr –with-jpeg-dir=/usr –with-png-dir=/usr/bin/libpng-config –enable-gd-native-ttf –with-gettext –with-mcrypt=/usr/bin/libmcrypt-config –with-mhash –with-mysql=/usr/local/mysql/ –with-mysqli=/usr/bin/mysql_config –with-openssl –with-pdo-mysql=/usr –enable-soap –enable-wddx –with-xsl –with-zlib –enable-mbstring –enable-zip
Failsafe:
./configure –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql
Make sure and get what you really need, but remember you can always recompile later. Now copy over a PHP.ini
cp php.ini-development /usr/local/apache2/php/php.ini
NOTE: Here’s an error you might run into:
configure: error: Cannot find MySQL header files under /usr. Note that the MySQL client library is not bundled anymore!
If you do install the mysql client libraries:
git clone git://github.com/Sannis/node-mysql-libmysqlclient.git cd node-mysql-libmysqlclient nice -20 make make test make clean sudo make install
After it’s built you’ll need to do a couple things:
vi /usr/local/apache2/conf/httpd.conf
Make sure this is here:
LoadModule php5_module modules/libphp5.so
And add this to the file also:
SetHandler application/x-httpd-php
Then restart Apache:
/usr/local/bin/apache2/apachectl restart
This will give you a nice running system! You will run into problems along the way, nothing that should be a showstopper. Good luck and if you have any questions, feel free to comment!

Tutorial: How to Install Apache PHP and MySQL on Ubuntu 12.10 Quantal Quetzal

Due to be released October 18th, Ubuntu 12.10 (”Quantal Quetzal”) looks like it’s going to be a pretty decent operating system. Most of the changes are minor, but in testing it I’ve found it to be very stable. I’ve whipped up some tutorials in preparation of the release for those folks looking to get their machines going.
Most web developers prefer a local webserver when doing development. If you can work this way it’s more convienient especially when going solo. Today’s tutorial will show you how to set up Apache, PHP and MySQL on your Quantal Quetzal machine so you can get rolling.

Install Apache

First you install Apache, and it’s as easy as it ever was:
1
sudo apt-get install apache2
After a quick install it will come up automatically. Load up a browser and point it http://localhost and you should see this:
This is your bare Apache server. Next we’ll add PHP.

Install PHP

You’ll want to install the latest version of PHP 5, to do that type:
1
sudo apt-get install libapache2-mod-php5 php5
This will install PHP5 plus the libraries to interface with Apache.
You’ll want to restart the server
1
sudo etc/init.d/apache2 restart
To test it out, create a file called test.php:
1
sudo nano /var/www/test.php
Let’s use the phpinfo() function so we can check out our installation:
/var/www/test.phpphpInfo() Documentation
1
<?php phpinfo(); ?>
It should look like this:
You can inspect your options and if you want to change anything, your php location is here:
1
/etc/php5/apache2/php.ini

Install MySQL

Next you’ll need to install the MySQL server. This is also super easy:
1
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
MySQL will ask you for a password. You really shouldn’t leave this blank, even if it is just going to be on your local machine. It only takes a second to do and some applications will require a password of some type on the server.
Test your server to make sure it’s working properly.
1
2
3
mysql -u root -p
<Enter Password>
show databases;
It should look something like this:
Now you’re up and running!
You may want something to manage your MySQL data, I realize not everyone is as crazy about the prompt as I am. PHPMyAdmin is great:
1
sudo apt-get install phpmyadmin
or you could download something like MySQL Workbench and work with your database that way.

Optional Publishing Method

Publishing can be kind of a pain because when you install Apache it makes the document root of the webserver /var/www that is owned by root. So this causes a problem. Here is one solution.
1
2
sudo mkdir /srv/www
sudo cp -r /var/www/* /srv/www/
then, open up your config file:
1
sudo nano /etc/apache2/sites-available/default
And change your DocumentRoot. There will be a section that says:
1
2
3
4
5
6
7
8
Change 
DocumentRoot /var/www
to 
DocumentRoot /srv/www
and 
<Directory /var/www>
to
<Directory /srv/www>
then restart your server:
1
sudo etc/init.d/apache2 restart
I created a folder in my home directory called “web” and then created a symbolic link:
1
sudo ln -s /srv/www/ /home/jeremy/web/
so I can work in a folder in my home directory ( I actually do most work in VIM ). Of course there’s a little permissions issue still, so run the following commands, but be sure to subsitute your user name.
1
2
3
sudo usermod -g www-data jeremy
sudo chown -R jeremy:www-data /srv/www
sudo chmod -R 775 /srv/www/
Now I can create files or entire an entire website in my own folder ( /home/jeremy/web ) and view it thru localhost.

Conclusion

So it turns out setting up Apache, PHP and MySQL is no different in Quantal Quetzal than in recent Ubuntu versions. Using apt-get for management takes a lot of hassle out of software management. I hope this helps you in setting up a local webserver, so you can start building some awesome stuff.
I will whip out a few more Quantal Quetzal tutorials in the coming days for those early adopters who like to try out the newest stuff. Questions? Leave me a comment!