How to setup a LAMP server Linux, Apache, MySQL, PHP on Centos 6 VPS

VPS is basically a virtual server or “container” on a larger dedicated server. You will have full control/access (reinstall OS, start/stop/reboot, full root access and such) of your VPS just the same as real dedicated server but with much lower cost since you share the real dedicated server with other people. I will show you how to setup a LAMP server Linux, Apache, MySQL, PHP on Centos VPS. Here is the new VPS specs

  • 1GB RAM
  • 1.2GB vSwap
  • 4 vCPU Cores
  • 30GB Disk space
  • 1TB Bandwidth
  • 1 Gbps uplink

First thing first, After you ordered and paid for your VPS, you will get an detailed email from your VPS provider about your VPS public IPv4 and/or IPv6 address, VPS Control Panel (Virtuozzo, Solus Virtual Manager, OnApp, Virtualizor), your root login password and such. You will need a fresh Centos install on your VPS, it’s your choice of 32 bit version if your RAM is less than 4 GB or 64 bit version if you have more than 4 GB of RAM.

Since we will be using Centos (free Red Hat Enterprise Linux version), we will use alot of “yum” command (Yellow dog Update package manager). From your workstation/desktop terminal in linux, or you can use putty in windows to connect to your VPS via SSH (Secure Shell) protocol.

$ ssh -p 22 root@yourvpsip

You will be asked for your VPS/Centos root password (you can get it from the VPS Server Information‏ email, or you can change it from your VPS control panel). After logged in to your VPS, update all the packages of the current Centos setup.

# yum update -y

Now we are ready to setup LAMP server (Linux, Apache, MySQL, PHP or Perl).

Install Apache server on Centos 6

Apache is the most popular HTTP or web server specially designed for Unix/Linux environment, but now you can also install Apache on Windows platform nowadays. Apache supports many web server features (CGI, SSL, Virtual Domain and more)

To install Apache on your Centos 6 VPS
# yum install httpd
To run Apache automatically by default when the system boot/reboot
# chkconfig httpd on

or

# /sbin/chkconfig --levels 235 httpd on
To start Apache on your Centos 6 VPS
# service httpd start
To restart Apache on your Centos 6 VPS
# service httpd restart
To stop Apache on your Centos 6 VPS
# service httpd stop

Your Apache server should be running by now, you can check it with your web browser by going to http://yourvpsip or http://yourdomain

Install MySQL server on Centos 6

MySQL is the world most popular open source database management system, it’s free yet powerful. MySQL is used to store database for many popular web applications such as WordPress, Drupal, PhpBB…

To install MySQL on your Centos 6 VPS
# yum install mysql-server
To run MySQL automatically by default when the system boot/reboot
# chkconfig mysqld on

or

# /sbin/chkconfig --levels 235 mysqld on
To start MySQL on your Centos 6 VPS
# service mysqld start
To restart MySQL on your Centos 6 VPS
# service mysqld restart
To stop MySQL on your Centos 6 VPS
# service mysqld stop

You can set password for root accounts and removes root accounts accessible from outside the localhost to improve MySQL security by running mysql_secure_installation.

# mysql_secure_installation

You will be asked to enter the current MySQL password which you don’t have yet, leave it blank. Then you will be asked to enter the new root password which you are going to set it with the password for root account in MySQL server. Type your MySQL root password twice. The next three questions you would want to answer “Y” for yes.

  • Remove anonymous users? [Y/n] Y
  • Disallow root login remotely? [Y/n] Y
  • Reload privilege tables now? [Y/n] Y
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Install PHP on Centos 6

PHP (PHP Hypertext Preprocessor) is a very widely used open source server side scripting language which also free to download and use. PHP can run on many platforms (Linux, Unix, Mac OS, Windows…) Many web CMS (content management systems) are written in PHP include WordPress, PHPbb, Joomla, Drupal, Moodle, even Facebook and Digg.

To install PHP and necessary PHP Modules on your Centos 6 VPS
# yum install php php-pear php-mysql php-gd php-mbstring

You can install more PhP Modules to your VSP/Dedicated Server to support more libraries and PHP applications.

To see what PHP Modules available for your system
# yum search php-

Here are some PHP Modules that available for my Centos 6 VPS

php-bcmath.i686 : A module for PHP applications for using the bcmath library
php-cli.i686 : Command-line interface for PHP
php-common.i686 : Common files for PHP
php-dba.i686 : A database abstraction layer module for PHP applications
php-devel.i686 : Files needed for building PHP extensions
php-embedded.i686 : PHP library for embedding in applications
php-enchant.i686 : Human Language and Character Encoding Support
php-fpm.i686 : PHP FastCGI Process Manager
php-gd.i686 : A module for PHP applications for using the gd graphics library
php-imap.i686 : A module for PHP applications that use IMAP
php-intl.i686 : Internationalization extension for PHP applications
php-ldap.i686 : A module for PHP applications that use LDAP
php-mbstring.i686 : A module for PHP applications which need multi-byte string handling
php-mysql.i686 : A module for PHP applications that use MySQL databases
php-odbc.i686 : A module for PHP applications that use ODBC databases
php-pdo.i686 : A database access abstraction module for PHP applications
php-pear.noarch : PHP Extension and Application Repository framework
php-pecl-apc.i686 : APC caches and optimizes PHP intermediate code
php-pecl-apc-devel.i686 : APC developer files (header)
php-pecl-memcache.i686 : Extension to work with the Memcached caching daemon
php-pgsql.i686 : A PostgreSQL database module for PHP
php-process.i686 : Modules for PHP script using system process interfaces
php-pspell.i686 : A module for PHP applications for using pspell interfaces
php-recode.i686 : A module for PHP applications for using the recode library
php-snmp.i686 : A module for PHP applications that query SNMP-managed devices
php-soap.i686 : A module for PHP applications that use the SOAP protocol
php-tidy.i686 : Standard PHP module provides tidy library support
php-xml.i686 : A module for PHP applications which use XML
php-xmlrpc.i686 : A module for PHP applications which use the XML-RPC protocol
php-zts.i686 : Thread-safe PHP interpreter for use with the Apache HTTP Server
To learn more about specific PHP Modules packages detail
# yum info module name

For example

# yum info php-xml
To add more PHP Modules to your server
# yum install module name

For example

# yum install php-xml

Remember to restart apache every time after making change to PHP for the changes to take effect on your centos vps.

# service httpd restart

or

# /etc/init.d/httpd restart

Test PHP on your Centos 6 VPS

To make sure PHP works on your VPS, we are going to create a phpinfo file, which also will show you PHP version, server information and a lot more details about PHP on your VPS.

# yum install nano
# nano /var/www/html/info.php

Add this to your info.php file and save it.

<?php
phpinfo();
?>

You should be able to see your info.php file via http://yourip/info.php or http://yourdomain/info.php

By now you should have a working LAMP Linux, Apache, MySQL, PHP on your Centos 6 VPS. All your html or php files should be placed in /var/www/html/

If you are reading and following this article, means either you have a dedicated or VPS server, please follow this article How to install, config and secure openssh server to secure your SSH server.

Recommended For You

About the Author: Linux Tips

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.