Install Nginx 1.0.15 + PHP 5.3.12 + MySQL 5.1.62 on CentOS 6.2

Since I’ve chose to run a low-memory VPS for my blog, I decide to install the ‘lnmp’ manually. Here I will show how to do it (install Nginx 1.0.15 + PHP 5.3.12 + MySQL 5.1.62 + eAccelerator 0.9.6.1).

Prepare

1. Of course a pc with Internet connection, virtual machine is the best option if this is the first time.

2. Flush ‘iptables’ if necessary, and shut down SELINUX

3. Download directory is /usr/local/src

Installation

First, download all source code

1
2
3
4
5
6
7
8
9
10
cd /usr/local/src
wget http://nginx.org/download/nginx-1.0.15.tar.gz
wget http://au.php.net/get/php-5.3.12.tar.gz/from/us.php.net/mirror
wget http://mysql.mirror.kangaroot.net/Downloads/MySQL-5.1/mysql-5.1.62.tar.gz
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
wget http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0
wget http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0
wget http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0
wget http://downloads.sourceforge.net/project/pcre/pcre/8.30/pcre-8.30.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fpcre%2Ffiles%2Fpcre%2F8.30%2F&ts=1335184494&use_mirror=aarnet
wget http://downloads.sourceforge.net/project/eaccelerator/eaccelerator/eAccelerator%200.9.6.1/eaccelerator-0.9.6.1.tar.bz2?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Feaccelerator%2Ffiles%2Feaccelerator%2FeAccelerator%25200.9.6.1%2F&ts=1335276559&use_mirror=waix

Then install some libaries, they should be libiconv,libmcrypt,mhash, mcrypt and pcre

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local
make
make install
cd ../
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../
tar zxvf pcre-8.30.tar.gz
cd pcre-8.30
./configure
make && make install
cd ../

Install MySQL

First, add group in OS for mysql application

1
2
groupadd mysql
useradd -s /sbin/nologin -M -g mysql mysql

Next step is unzip, compile and install. Notice that the LNMP would be intall in /usr/local/webserver

1
2
./configure --prefix=/usr/local/webserver/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg
make && make install

Then the following script is used to change the directory permissions, make mysql auto start when boot, and for easy management, service command will be use to manipulate mysql

1
2
3
4
5
6
7
8
9
10
11
12
chmod +w /usr/local/webserver/mysql
chown -R mysql:mysql /usr/local/webserver/mysql
/usr/local/webserver/mysql/bin/mysql_install_db --basedir=/usr/local/webserver/mysql --user=mysql
cp -f /usr/local/webserver/mysql/share/mysql/my-medium.cnf /etc/my.cnf
sed -i 's/skip-locking/skip-external-locking/g' /etc/my.cnf
sed -i 's:#innodb:innodb:g' /etc/my.cnf
cp /usr/local/webserver/mysql/share/mysql/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
ln -s /usr/local/webserver/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/webserver/mysql/insclude/mysql /usr/include/mysql
chkconfig mysqld on

Restart the machine, mysql should be run properly. Follow by setting the root password

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/usr/local/webserver/mysql/bin/mysqladmin -u root password ‘root'
cat > /tmp/mysql_sec_script<<EOF
use mysql;
delete from user where not (user='root') ;
delete from user where user='root' and password='';
drop database test;
DROP USER ''@'%';
flush privileges;
EOF
/usr/local/webserver/mysql/bin/mysql -u root –p'root' -h localhost < /tmp/mysql_sec_script
rm -f /tmp/mysql_sec_script
/etc/init.d/mysqld restart
/etc/init.d/mysqld stop

Install PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cd /usr/local/src
tar zvxf php-5.3.12.tar.gz
cd php-5.3.12
mkdir -p /usr/local/webserver/php
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production /usr/local/webserver/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #fpm is embeded in php 5.3 and above
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on #auto start
cp /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf

Modify php-fpm.conf, change the user and group to following:

1
2
user = www
group = www

Then install eAccelerator

1
2
3
4
5
6
7
8
cd ../
tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1
/usr/local/webserver/php/bin/phpize #mark down the output
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
cd ../

Modify php.ini (located in /usr/local/webserver/php/etc/php.ini), change extension_dir = “./” to extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/"

Configure eAccelerator:

1
2
mkdir -p /usr/local/webserver/eaccelerator_cache
vi /usr/local/webserver/php/etc/php.ini

At the end of the file, input the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[eaccelerator]
zend_extension='/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so'
eaccelerator.shm_size='64'
eaccelerator.cache_dir='/usr/local/webserver/eaccelerator_cache'
eaccelerator.enable='1'
eaccelerator.optimizer='1'
eaccelerator.check_mtime='1'
eaccelerator.debug='0'
eaccelerator.filter=''
eaccelerator.shm_max='0'
eaccelerator.shm_ttl='3600'
eaccelerator.shm_prune_period='3600'
eaccelerator.shm_only='0'
eaccelerator.compress='1'
eaccelerator.compress_level='9'

Install Nginx

First of course add appropriate user group

1
2
3
4
5
6
7
groupadd www
useradd -s /sbin/nologin -g www www #this make www user cant login
mkdir -p /home/wwwroot #to store all domain configuration files
chmod +w /home/wwwroot
mkdir -p /home/wwwlogs
chmod 755 /home/wwwlogs
chown -R www:www /home/wwwroot

Install Nginx

1
2
3
4
5
tar zxvf nginx-1.0.15.tar.gz
cd nginx-1.0.15/
./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make && make install
mkdir /usr/local/webserver/nginx/conf/vhost/ #used to config domain

Make necessary modification to nginx configuration file

1
2
rm -f /usr/local/webserver/nginx/conf/nginx.conf
vi /usr/local/webserver/nginx/conf/nginx.conf

input the following

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
user www www;
worker_processes 1;
error_log /home/wwwlogs/nginx_error.log crit;
pid /usr/local/webserver/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#log format
log_format access '$remote_addr - $remote_user [$time_local] '$request' '
'$status $body_bytes_sent '$http_referer' '
''$http_user_agent' $http_x_forwarded_for';
include vhost/*.conf;
}

This only is basic config for nginx, domain configuration for each domain (if you are hosting multi-domain in a server) would be the next:

1
2
cd /usr/local/webserver/nginx/conf/vhost
vi domain.com.conf

input the following. After that php could be supported by nginx. Change domain.com based on you domain name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server
{
listen 80;
server_name www.domain.com domain.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/domain.com/www;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log /home/wwwlogs/domain.com.access.log;
}

Create root directory for the domain:

1
mkdir -p /home/wwwroot/domain.com/www/

After all done, create a manage script for nginx ( run $ vi /etc/init.d/nginx first):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC='nginx daemon'
NAME=nginx
DAEMON=/usr/local/webserver/nginx/sbin/$NAME
CONFIGFILE=/usr/local/webserver/nginx/conf/$NAME.conf
PIDFILE=/usr/local/webserver/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x '$DAEMON' ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n 'nginx already running'
}
do_stop() {
kill -INT `cat $PIDFILE` || echo -n 'nginx not running'
}
do_reload() {
kill -HUP `cat $PIDFILE` || echo -n 'nginx can't reload'
}
case '$1' in
start)
echo -n 'Starting $DESC: $NAME'
do_start
echo '.'
;;
stop)
echo -n 'Stopping $DESC: $NAME'
do_stop
echo '.'
;;
reload|graceful)
echo -n 'Reloading $DESC configuration...'
do_reload
echo '.'
;;
restart)
echo -n 'Restarting $DESC: $NAME'
do_stop
do_start
echo '.'
;;
*)
echo 'Usage: $SCRIPTNAME {start|stop|reload|restart}' >&2
exit 3
;;
esac
exit 0

add execute pemission, and set as auto start:

1
2
chmod +x /etc/init.d/nginx
chkconfig nginx on

Environment Specification

Type the following command, and you will get the corresponding result:

1
2
3
4
5
service nginx restart #restart nginx
service mysqld restart ##restart mysql
service php-fpm restart ##restart php-fpm
service php-fpm stop #stop php-fpm
service php-fpm start #start php-fpm

Directory for website files /home/wwwroot/ Directory for all application /usr/local/webserver/

Locations for each configuration file:

1
2
3
php: /usr/local/webserver/php/etc/
mysql: /etc/
nginx: /usr/local/webserver/nginx/conf/