Jump to content
xisto Community
Sign in to follow this  
iGuest

Gnu/linux Webserver - How To Install An Apache Web Server With Php And Mysql

Recommended Posts

GNU/Linux Webserver - How To Install an Apache Web Server with PHP and MySQL

 

This guide will help you install a web server on Red Hat alike RPM-based GNU/Linux distributions, specifically CentOS 6.3 and Fedora 18 but may work with other GNU/Linux based distributions by changing the package manager used for installing the software and discovering where the configuration files are stored on your distribution. If you are looking for installation on Debian like operating systems, then check out the tutorial that http://forums.xisto.com/user/87487-sils/
'>loramchugh
has written on GNU/Linux Web Server With Apache2, PHP and MariaDB.

 

Securing this server for production use is for another tutorial so that I could separate the installation from the configuration.

 

Note: This tutorial is all done inside terminal with a super user, suitable for those who have done a minimal installation which is the recommended way for creating a dedicated web server.

 

How To Install an Apache Web Server

 

Apache (httpd) is the most popular HTTP web server software in use and one of the guiding reasons for installing this web server over other alternatives such as lighttpd or nginx. It has a lot of information that helps with setting it up. If this is your first time installing a web server then it is highly recommended you use Apache. If you want better performance, the two alternatives I suggested are worth looking at.

 

To begin first we need to install httpd:

 

sudo yum -y httpd

Next we need to enable this server:

 

For Fedora 18

sudo systemctl start httpd.servicesudo systemctl enable httpd.service

For CentOS 6.3

sudo /etc/rc.d/init.d/httpd startsudo chkconfig httpd on

And now to test if it is working:

 

I was searching Fedora 18 and CentOS for a common program they shared amongst themselves so that I could perform the test. I first tried nc (netcat), which was found on Fedora but not CentOS. I tried telnet, it wasn't found on either, I tried wget which I found on Fedora but not CentOS. I then tried curl, and discovered that both CentOS and Fedora both shared this program in common. Usually the anaconda installer for CentOS and Fedora rely on curl for downloading, so this is the program we will use for testing whether our web server is running.

 

curl -IL http://localhost/

If you get output similar to:

 

HTTP/1.1 403 ForbiddenDate: Wed, 27 Feb 2013 17:56:46 GMTServer: Apache/2.2.15 (CentOS)Accept-Ranges: bytesContent-Length: 5039Connection: closeContent-Type: text/html; charset=UTF-8

This shows only the header information of the website, which means that the web server responded to our request with this reply. If you would like to see the HTML of the site you can remove the I from the switch so that you get all data returned.

 

Then you have successfully installed the Apache HTTP Server.

 

If you are not so successful and you get:

 

curl: (7) couldn't connect to host

 

You should restart your service to see if that solves the problem first.

 

For Fedora 18

sudo systemctl restart httpd.service

For CentOS 6.3

sudo /etc/rc.d/init.d/httpd restart

And then try the test again. If you are still not successful, take note of the error and leave a message here and I will provide more help in solving your issues.

 

How To Install PHP

 

PHP is a server side scripting language that can be embedded into HTML that allows a flexible way of producing a dynamic nature to static HTML web pages. PHP code is only executed within the delimiters. It started off as a competitor to ASP (Active Server Pages) but is now more widely accepted. There are other dynamic languages that you could use like PERL, Ruby, Python and many more languages.

 

To install php we do:

 

sudo yum -y install php

We now restart our web server for changes to take place, every alteration involving httpd should require a restart:

 

For Fedora 18

sudo systemctl restart httpd.service

For CentOS 6.3

sudo /etc/rc.d/init.d/httpd restart

Now we need to create a PHP file to test:

 

sudo vi /var/www/html/index.php

Inside the vi editor, lets get into insert mode by press I and typing:

 

<?php print('PHP is installed.'); ?>

Press Escape to get out of insert mode and back into command mode then type :wq to save and quit.

 

Now to test it out:

 

curl -L http://localhost/

If you get the output:

 

PHP is installed

You have PHP enabled on your web server. If you want to remove the file you created you can do:

 

sudo rm /var/www/html/index.php

The location for storing websites is /var/www/html, however it is owned by root and it not really a suitable location if you want your normal user to be creating pages. In our web server configuration set up, we will look at areas that gives us an easier way to serve our own pages from a location in our home directory, so be on the lookout for that guide.

 

How To Install MySQL

 

MySQL is the most popular open source relational database management system being used. Although it is used mostly for web it can be used for anything that requires storing collections of structured data. It provides multi-user access to allow simultaneous queries and scales well for small to medium sized applications. Another alternative to MySQL is MariaDB that is an enhanced drop in replacement for MySQL created by some of the original authors of MySQL. You could also check out PostgreSQL which claims to be the most advanced open source object relational database.

 

To install mysql:

 

sudo yum -y install mysql-server

Once it is installed we need to start and enable it:

 

For Fedora 18

sudo systemctl start mysqld.servicesudo systemctl enable mysqld.service

For CentOS 6.3

sudo /etc/rc.d/init.d/mysqld startsudo chkconfig mysqld on

Next we need to set up a password for our root user on this database as well as remove anonymous log ins, so lets log into mysql with the root user.

 

mysql -u root

Now we have connected to our mysql database (mysql-server) using the mysql client. Now we need to query all the users for this database:

 

SELECT user, host, password FROM mysql.user;

Also the SQL commands (those in uppercase above) are case insensitive which means you could do it all lowercase, mixed case, however you like. I prefer UPPERCASE as this is how you see it widely used and it helps to see the difference between names and commands, although when typing it in the mysql prompt, it's not really that important, but I just try to get use to doing this as a habit.

 

Your output might look like this:

 

+-------+-----------------------+----------+| user  | host            | password |+-------+-----------------------+----------+| root  | localhost        |       || root  | localhost.localdomain |       || root  | 127.0.0.1        |       || root  | ::1            |       ||    | localhost        |       ||    | localhost.localdomain |       |+-------+-----------------------+----------+6 rows in set (0.00 sec)

Every row that has a user root, we need to either put a password on it or remove it. Every row without a user needs to be removed. As you can see with this table, there is no password set for all users on this database, no user is actually the anonymous user, so it allows anonymous login. Although they will not have the privileges like that of the root user. The host with ::1 is the IPv6 address, I don't use IPv6 in my network, so I remove this.

 

So lets remove the ones we don't need first, your commands may differ slightly compared to your table:

 

DELETE FROM mysql.user WHERE user='root' AND host='::1';DELETE FROM mysql.user WHERE user='';

Now we have cleared the ones we don't need, lets set a password on all our root users, make sure to chage YourPasswordHere to the password you want to use for root on mysql:

 

SET PASSWORD FOR root@localhost=PASSWORD('YourPasswordHere');SET PASSWORD FOR root@'127.0.0.1'=PASSWORD('YourPasswordHere');SET PASSWORD FOR root@'localhost.localdomain'=PASSWORD('YourPasswordHere');

Now run the query to show your database to see if we have done what we asked it to do.

 

SELECT user, host, password FROM mysql.user;

If the table is showing the ones you want to keep and all the rows with user root with a password (which is hashed for security reasons) and no anonymous users all we need to do is now test that we can log in with root with the password. So type exit to leave mysql then do:

 

mysql -u root -p

When asked to enter the password for root, type in the password you set. If you log in we have finished with setting it up.

 

There's an easier way to do the above, which also removes the test database but I wanted to also show you how to execute mysql commands. So now we can run this in terminal:

 

mysql_secure_installation

This will take you through the steps of entering your root password, etc. There's no need to change your root password if you have already set it up above but you'll want to disallow root logins from remote connections, disallow anonymous connections and also remove the test database.

 

In this guide we have sucessfully installed Apache (httpd), PHP and MySQL (mysql-server). This is only just the first step. We have shown that it works but we are not really utilising it at this stage. We still need to configure it so that it will be easier for us to work with which is what my next guide will be on and then another guide to actually putting it to use so we can start hosting our own web server whether for testing, production or development. The goal will be to get it production ready, although if you are thinking of hosting others, this is not the way to go about it and I may or Velma, will talk about Virtual Private Servers, which is how I would set up web hosting from a dedicated server.

 

 

Cheers,

 

MC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.