How to Setup a LAMP Stack on CentOS

For the purpose of this tutorial, we will use yourdomain.tdl as our domain.  Anytime you see something like {yourdomain.tdl} or anything similar enclosed in curly braces, please replace with the text specific for your setup.

Install Apache Server on CentOS

  1. Install
    yum install httpd
  2. Set to startup Apache on boot
    chkconfig --levels 235 httpd on
  3. Open this configuration file /etc/httpd/conf/httpd.conf, uncomment the following line and save the file (i.e. remove the pound sign “#” at the beginning of the line, if there is one)
    NameVirtualHost *:80
  4. Restart Apache (you will get an error that says something like “NameVirtualHost *:80 has no VirtualHosts” that you can ignore.  We will address this when we set up our virtual host later)
    service httpd restart
  5. Test by going to http://localhost/ in your browser.  If successful, you should get the Apache sample page.
  6. For security, delete this file

Grant Existing User Document Root Permissions

This is optional, but it will setup an existing user with the ability to sftp files to the document root.  The code below does the following…

  1. Adds the user to the apache group
  2. Changes the document root permissions (sets all to a group of “apache”)
  3. Sets the permissions (add “group write” ability on everything
sudo usermod -a -G apache <username>
sudo chgrp -R apache /var/www/html
sudo chmod -R g+w /var/www/html

@see http://www.rackspace.com/knowledge_center/article/how-to-add-linux-user-with-document-root-permissions

Set ServerName

  1. Create a DNS A Record to your server’s ip for your server name (i.e. server.yourdomain.tdl)
  2. Edit /etc/hosts file and add your server name to the end of the first two lines and add it for your servers ip.  It might look something like this if your server name is server.yourdomain.tdl and your ip is 12.345.6.78…
    127.0.0.1   localhost localhost.localdomain server.yourdomain.tdl
    ::1         localhost localhost.localdomain server.yourdomain.tdl
    
    12.345.6.78 server.yourdomain.tdl
    
  3. Edit /etc/httpd/conf/httpd.conf and add the ServerName below the example line that is commented out.  Example…
    #ServerName www.example.com:80
    ServerName server.yourdomain.tdl:80

Create a Virtual Host on CentOS

  1. Create the file /etc/httpd/conf.d/{yourdomain.tdl}.conf and edit the file as below…
    <VirtualHost *:80>
        # General
        ServerAdmin {webmaster_email}
        DocumentRoot /var/www/html/{yourdomain.tdl}/public_html/
        ServerName www.{yourdomain.tdl}
        ServerAlias {yourdomain.tdl}
    
        # Logging
        ErrorLog logs/{yourdomain.tdl}-error.log
        CustomLog logs/{yourdomain.tdl}-access.log common
    </VirtualHost>
    
  2. Restart Apache
    service httpd restart

Point your domain to your server

The specific steps vary depending on what domain registrar you are using, but what you need to do is set up an A Record for you domain and point it to your server’s public IP. You can set up one or more of the following

  • A Record for “@”: for none, as in yourdomain.tdl
  • A Record for “www”: as in www.yourdomain.tdl
  • A Record for “*”: for {anything}.yourdomain.tdl
    You can use this wildcard DNS record without doing the other two previous A Records since it will encompass all of the above plus any other subdomains you decide to create now and in the future.

Below is a sample of what each of these might look like (replace the IP 123.456.7.890 with your public IP).

@ - A Record - 123.456.7.890 ---AND--- www - A Record - 123.456.7.890 ---AND--- * - A Record - 123.456.7.890

This may take a little while for the DNS to propagate. I usually see it come through within about 30 minutes, but it can take about 24-48 hours so be patient. You can check the status using this tool at https://www.whatsmydns.net/ to see if it’s working. It won’t tell you when it’s fully complete, but you can at the very least check to see if your new IP has started showing up.

Setup MySQL on CentOS

  1. Install MySQL
    yum install mysql-server mysql php-mysql
  2. Set MySQL to start on boot
    chkconfig --levels 235 mysqld on
  3. Start MySQL
    service mysqld start
  4. Log into MySQL
    mysql -u root
  5. Set root user password for all domains, drop the “any” users then exit
    SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('{NewPassword}');
    SET PASSWORD FOR 'root'@'centos64' = PASSWORD('{NewPassword}');
    SET PASSWORD FOR 'root'@'localhost' = PASSWORD('{NewPassword}');
    
    DROP USER ''@'localhost';
    DROP USER ''@'centos64';
    
    EXIT;
    

    The users you edit might be slightly different. You can run the following which will produce a list of root users and related hosts (example output below).

    mysql> SELECT Host, User FROM mysql.user;
    +-----------+------+
    | Host      | User |
    +-----------+------+
    | 127.0.0.1 | root |
    | centos64  |      |
    | centos64  | root |
    | localhost |      |
    | localhost | root |
    +-----------+------+
    4 rows in set (0.00 sec)
    
  6. You may also want to refer to this tutorial… Create MySQL Database and Add New User (command line)

If you need to set up remote access to your SQL server, try this tutorial on how to allow remote access to MySQL server.

Setup PHP on CentOS

  1. Install PHP
    yum install php
  2. Create the test file /etc/www/html/www.{yourdomain.tdl}/public_html/phpinfo.php with the following contents
    <?php phpinfo(); ?>
  3. Test your new page by going to http://www.{yourdomain.tdl}/phpinfo.php in your browser. If successful, you should get a long page of all your PHP configurations that are currently set.

Setup phpMyAdmin on CentOS

This step isn’t necessary, but can be helpful if you are in need of a MySQL web interface.

  1. Install phpMyAdmin
    yum install phpmyadmin
  2. Restart Apache
    service httpd restart
  3. Test by going to http://localhost/phpmyadmin on your local machine’s browser. Login with your root credentials and you should be good to go.
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments