How do I set up a LAMP server from scratch using CentOS 6.7?

Dislaimer: This procedures are done keeping in mind that the server being set up will NEVER be used in production, just for testing. I am not a system/network administrator, just a novice programmer asked to set up a server so take the procedures with a grain of salt.

L - CentOS 6.7

  1. Download CentOS from here. Choosing to download the CentOS-6.7-i386-bin-DVD1.iso is enough.

  2. Burn the downloaded ISO to a DVD using an ISO burner tool. In our case, we used WinISO. Doing this should be easy but in case you are lost, a tutorial is avaliable.

  3. Insert the DVD to the computer and change the boot options to boot directly from the DVD.

  4. Install CentOS using this guide as basis. Set the IP address and choose Basic Server as the default installation package as you go.

  5. After CentOS installation is complete, we can check the IP address using the following command

    ifconfig
    
    • In case the IP address you set was wrong, you can change it by issuing the following command

      vi /etc/sysconfig/network-scripts/ifcfg-eth0
      
    • Edit the following lines. Use this as a guide.

      IPADDR=your.ip.address
      NETMASK=the.netmask
      GATEWAY=the.default.gateway
      DNS1=the.dns
      
  6. Finally, restart the network

    /etc/rc.d/init.d/network restart
    chkconfig network on
    
  7. We will use this tutorial as the basis in installing the rest of the LAMP stack.

A - Apache 2.2.15

  1. Install Apache by issuing the following command (-y to respond yes to all the questions)

    yum -y install httpd
    
  2. Start Apache by doing the following

    service httpd restart
    
  3. To check if Apache is running correctly, open a browser and enter your IP address. Some issues might be encountered when starting Apache. This provided a solution to one of the issues we encountered. Another thing to do is bring down the firewall so that the server can be accessed by anyone in the same network. To bring down the firewall, issue this command

    service iptables stop
    
    • IMPORTANT: If the previous command resolved the issue, (Apache can be seen in the browser), It is critical to configure the firewall to accept such requests.

M - MySQL Ver 14.14 Distrib 5.1.73

  1. Install MySQL using the following command

    yum -y install mysql-server
    
  2. Start MySQL by issuing the following

    service mysqld start
    
  3. It is important to secure MySQL. Start configuring this by issuing the following command

    /usr/bin/mysql_secure_installation
    
  4. At first, MySQL root password is blank, so just press Enter on the first question.

  5. Next it will ask for you to set a root password so go ahead and set one.

  6. Lastly, there will be a series of questions to secure MySQL, it is recommended to just answer yes to all the questions.

  7. If you noticed, we disallowed the root to be able to login to MySQL remotely. However, if you need to configure MySQL remotely, you will need to create another user. In creating a new user, follow this example. Issue commands like so

    mysql> CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
    mysql> CREATE USER 'admin'@'%' IDENTIFIED BY 'password';
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION;
    
    • To login to MySQL:

      mysql -u root -p
      # or
      mysql -u admin -p
      
    • To exit MySQL:

      mysql> EXIT
      

P - PHP 5.3.3

  1. Install PHP using the following command

    yum -y install php php-mysql
    
  2. The previously linked tutorial also shows other PHP modules that can be installed. Go ahead and install the modules that you need. When we are done with with installing PHP, restart the server by issuing the following command

    service httpd restart
    
  3. To make Apache and MySQL run automatically when the server starts, issue the following commands (PHP starts at the same time as Apache)

    chkconfig httpd on
    chkconfig mysqld on
    
  4. To test if our installation is correct, we can make a simple PHP file that we will put in our webroot. The webroot is commonly at /var/www/html so create an info.php file there

    vi /var/www/html/info.php
    
  5. If the file does not exist, vi will create it for you. Inside the file, enter the following

    <?php 
        phpinfo();
    ?>
    
  6. Open your browser, type your IP address, a slash(/) and info.php like: http://your.ip.address/info.php If you are successful, you would see a webpage telling you the current status of your PHP installation

Congratulations! LAMP is now successfully installed in your server.


Extra F - FTP

  1. If you need to upload many files to your server, it is important to install FTP. We installed an FTP by following this tutorial. First, lets stop the firewalls. Issue the following commands

    service iptables stop
    service ip6tables stop
    chkconfig iptables off
    chkconfig ip6tables off
    
  2. Install the FTP service by doing the following

    yum -y install vsftpd
    
  3. Start the service and run it automatically on server start

    service vsftpd start
    chkconfig vsftpd on
    
  4. Edit the vsftpd.conf file like so

    vi /etc/vsftpd/vsftpd.conf
    
  5. Change the values/uncomment/add the following lines

    anonymous_enable=NO
    
    ascii_upload_enable=YES
    ascii_download_enable=YES
    
    use_localtime=YES
    
  6. Restart the FTP service

    service vsftpd restart
    
  7. The root user is not allwed to connect to the FTP server for security purposes. We need to add a new user that we can use for the FTP service. To add a new user to CentOS, we do it like so

    useradd admin
    passwd admin
    
  8. You will be asked for the password of the user you are creating, set the password as you see fit. You will be warned for bad passwords that you can ignore, just retype the password that you set. Next is to install FTP itself. Do it by issuing the following

    yum -y install ftp
    
  9. Then we can connect ot the FTP server like so

    ftp your.ip.address
    
  10. We will connect using our IP address. You will be asked for the user to use for connecting to the FTP. Use the user and password that we just created. You will probably receive an error while logging in. The tutorial linked previously explains briefly the cause of this error, a quick fix would be

    setenforce 0
    # or as the tutorial suggests
    setsebool -P ftp_home_dir on
    
  11. Now we can connect to the FTP using the command line or a client application. But first, we need to change the ownership and the permissions of our web root. We can do it like so

    chown -R user html
    chmod -R 777 html
    

There, you have finished setting up your web server!