08 Apr

How to setup web server with Apache, MySQL and PHP on Linux (CentOS) machine (LAMP environment)

Category: CMS

We have been running websites on Linux (CentOS) servers with Apache, MySQL and PHP (a.k.a. LAMP). We are very impressive with the performance and stability of CentOS. This article is a step-by-step guide on how to set up a system for running multiple websites on a CentOS 6.x machine with 64-bit CPU.

 

First, verify the type and version of Linux your server is running.

# cd /etc

# cat redhat-release

CentOS release 6.3 (Final)

 

To check whether the machine is 64-bit

# uname -a

Linux serverhost1.example.com 2.6.32-xxx.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

 

If you see x86_64, it means your server is running 64-bit OS. The port of el6 indicates that it is CentOS 6.x.

 

As long as your server is running CentOS, setup procedures on other CentOS versions (like CentOS 5.x) should be similar.

 

To change LOCALE

If you want your machine to support non-English languages, set UTF-8 as the locale. To change on CentOS:

# cd /etc/sysconfig

# cp –ip i18n i18n.save

# vi i18n

LANG="en_US.UTF-8"

:wq

 

Reboot machine for the new locale to take effect.

 

To verify locale on CentOS:

# locale

 

 

Setup firewall

There may be software firewall running on the Linux server. So you need to open up some ports for your services (e.g. FTP, Apache) to work. The default firewall on CentOS is iptables.

 

For example, to add a rule to open port 80 for httpd (Apache):

# iptables -A RH-FW-rule-1-INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT

 

Don’t forget to save your changes. Otherwise your changes will be lost after reboot. To save to firewall rules file (/etc/sysconfig/iptables) after changes:

# /sbin/service iptables save

 

To list firewall rules:

# iptables –L -v

 

To check if iptables is running:

# lsmod | grep ip_tables

 

To start|stop iptables:

# cd /etc/init.d; ./iptables {start|stop}

 

To check whether iptables service is enabled:

# chkconfig --list iptables

 

To start iptables automatically on system reboot, you may do this:

# chkconfig --level 2345 iptables on

 

Setup FTP server so you can transfer files from your PC to the Linux server.

To start vsftpd automatically on system reboot:

# chkconfig --level 2345 vsftpd on

 

To check whether vsftpd service is enabled:

# chkconfig --list vsftpd

 

Add a firewall rule for vsftpd:

# iptables -A RH-FW-rule-1-INPUT -m state --state NEW -p tcp --dport 21 -j ACCEPT

 

To start|stop vsftpd manually:

# cd /etc/init.d; ./vsftpd {start|stop}

 

If you want to send emails from web pages (e.g. online enquiry form) by PHP Mail, you need Sendmail or Postfix to run.

By default, sendmail runs on CentOS. If not, check if postfix is running. 

 

To check whether sendmail is running:

# ps –ef | grep sendmail

 

If either of them is running, you are fine. Just make sure server hostname is in hostname.domainname format (i.e. server1.example.com) and cannot be simply in hostname format (i.e. server1). Check in /etc/sysconfig/network file.

 

To install Apache (httpd) 2.2.x

To install httpd 2.2.x on CentOS 6.3 (64-bit; el6), use the webtatic repo (see the MySQL section in this article on how to set up webtatic repo - once only for all packages).

 

To list what httpd packages are already installed and available in a yum repo:

# yum --enablerepo=webtatic list httpd

Or

# yum --enablerepo=webtatic list | grep httpd

 

To install httpd 2.2.x on CentOS 6.3:

# yum install httpd --enablerepo=webtatic

 

To verify after updating:

# rpm –qa | grep http

 

To start|stop httpd:

# cd /etc/init.d; ./httpd {start|stop}

 

 

To install MySQL 5.1.x on CentOS 6.3 (64-bit; el6):

Firstly, set up the repository on CentOS/RHEL 6.3, install the webtatic-release RPM

# rpm -Uvh http://repo.webtatic.com/yum/el6/latest.rpm

 

To list what mysql packages are already installed and available in a yum repo:

# yum --enablerepo=webtatic list mysql

Or

# yum --enablerepo=webtatic list | grep mysql

 

To install MySQL 5.1.x on CentOS 6.3:

# yum install mysql mysql-server --enablerepo=webtatic

 

 

To create MySQL database and user:

To set MySQL initial root password (default password is ‘blank’, so you must change it for security purpose):

# mysqladmin -u root password 'new-password'

 

MySQL command prompt:

[root@webhost1 ~]# mysql -u root -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 778

Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

 

You can create MySQL user in PHPMyAdmin (web interface for MySQL) or mysql command prompt. Using mysql command prompt:

SQL> create user ‘mysqldba’@’localhost’ identified by ‘password’;

SQL> grant all on databasename.* to ‘mysqldba’@’localhost’;

This user ‘mysqldba’ will only have access to the ‘databasename’ database.

 

To revoke all privileges on a database from a user:

SQL> revoke all on databasename.* from ‘mysqldba’@’localhost’;

 

To show MySQL users, execute the following SQL in phpMyAdmin:

select * from mysql.user;

 

Execute the following on production MySQL servers, giving you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers:

# /usr/bin/mysql_secure_installation

 

To start | stop mysqld:

# /etc/init.d/mysqld {start|stop}

 

 

To install PHP 5.3.x

To check PHP packages installed on Linux:

# rpm –qa | grep php

 

To install PHP 5.3.x on CentOS 6.3 (64bit; el6), use the webtatic repo (see MySQL section above on how to set up webtatic repo - once only for all packages).

 

To list what PHP packages are already installed and available in a yum repo:

# yum --enablerepo=webtatic list php

Or

# yum --enablerepo=webtatic list | grep php

 

To install PHP 5.3.x on CentOS 6.3 (64-bit):

# yum install php --enablerepo=webtatic

 

To install PHP extensions on CentOS 6.3 (64-bit), you may enter the extensions in a single command:

#yum install php-gd php-pear php-common php-mbstring php-pdo php-odbc php-mysql php-devel --enablerepo=webtatic

 

To install php-mcrypt 5.3.3 (not included in webtatic) on CentOS 6.3 (64-bit), execute:

# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-7.noarch.rpm

 

To check if php-mcrypt is in the repo:

# yum list |grep mcrypt

 

To install php-mcrypt 5.3.3 on CentOS 6.3 (64-bit):

# yum install php-mcrypt.x86_64

 

You are done! Now you are ready to configure Apache to create new websites. You may do so in Apache by creating new virtual servers in the Apache configuration file.

 

Leave a comment