LNMP的制作
一、LNMP的简单介绍
LAMP是Linux + Nginx+ MySQL + PHP的合并之后的简称。
二、安装前的准备工作
需要安装一些开发包组
# yum groupinstall "Development Tools" "Development Libraries"
# yum install gcc openssl-devel pcre-devel zlib-devel
三、开始安装LNMP
(1)先安装Nginx
先添加nginx用户和组
# groupadd -r nginx
# useradd -r -g nginx -s /bin/false -M nginx
从网上下载nginx-1.0.13.tar.gz
然后执行下面的命令
# tar xf nginx-1.0.13.tar.gz
# cd nginx-1.0.13
# ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi
--with-pcre
# make
# make install
编写nginx的脚本执行文件
# vim /etc/rc.d/init.d/nginx 其脚本内容如下
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemon
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
# chmod +x /etc/rc.d/init.d/nginx 给文件执行权限
# chkconfig --add nginx 添加到开机启动列表
# chkconfig nginx on 让其在指定的级别打开
# service nginx start 开启其服务
(2)安装mysql
在我们实际的工作中,由于数据库的文件会不断的变大,所以我们要把mysql建在一个LVM上,所以我们要准备以下工作
在磁盘上新建一个分区,并将类型改为8e
# partprobe /dev/sda 同步一下
# pvcreate /dev/sda5 创建PV
# vgcreate myvg /dev/sda5 创建VG
# lvcreate -L 2G -n mydata -p rw myvg 创建LV
# mke2fs -j /dev/myvg/mydata 对LV进行格式化
# vim /etc/fstab 编写配置文件让其开机自起
/dev/myvg/mydata /mydata ext3 defaults 0 0
# mkdir /mydata 创建目录mydata
# mount -a 将其挂载上
# mount 查看是否挂载上
/dev/mapper/myvg-mydata on /mydata type ext3 (rw)
为mysql添加用户和组
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M mysql
下载mysql的安装文件mysql-5.5.20-linux2.6-i686.tar.gz ,然后执行下面的命令
# tar xf mysql-5.5.20-linux2.6-i686.tar.gz -C /usr/local/
# cd /usr/local/
# ln -sv mysql-5.5.20-linux2.6-i686 mysql 创建链接用着方便
# chown -R mysql:mysql /mydata/data/
# chown -R mysql:mysql mysql/*
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# chown -R root .
为mysql提供配置文件
# cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
# vim /etc/my.cnf
将此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2
另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /mydata/data
为mysql提供服务脚本
# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
# service mysqld start
Starting MySQL [ OK ] 说明启动成功
# ln -sv /usr/local/mysql/include /usr/include/mysql 创建链接使输出mysql的头文件至系统头文件路径/usr/include
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf 给系统库输出mysql的库文件查找路径
# vim /etc/man.config 在其添加如下一行使可以使用man命令查找mysql
MANPATH /usr/local/mysql/man
# vim /etc/profile 修改环境变量,添加如下一行
PATH=$PATH:/usr/local/mysql/bin
这样mysql就创建成功了
(3)安装php
如果想让编译的php支持mcrypt、mcrypt、mhash扩展和libevent,此处还需要下载如下几个rpm包并安装之:
libmcrypt-2.5.8-4.el5.centos.i386.rpm
libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm
mhash-0.9.9-1.el5.centos.i386.rpm
mhash-devel-0.9.9-1.el5.centos.i386.rpm
libevent-2.0.17-2.i386.rpm
libevent-devel-2.0.17-2.i386.rpm
mcrypt-2.6.8-1.el5.i386.rpm
由于rpm包只有这几个,所有可以执行下面的命令进行升级
# rpm -Uvh *.rpm --nodeps
为了支持libiconv,还要下载libiconv-1.13.1.tar.gz,并进行安装
# tar xf libiconv-1.13.1.tar.gz
# cd libiconv-1.13.1
# ./configure
# make
# make instal
下载php-5.3.10.tar.bz2然后进行安装
# tar xf php-5.3.6.tar.bz2
# cd php-5.3.6
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc/php --with-config-file-scan-dir=/etc/php --with-bz2 --with-curl --with-iconv=/usr/local
# make ZEND_EXTRA_LIBS='-liconv'
# make install
# cp php.ini-production /etc/php/php.ini 为php提供配置文件
# cp php-5.3.6/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm 为php-fpm提供Sysv init脚本
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 为php-fpm提供配置文件
# vim /usr/local/php/etc/php-fpm.conf
配置pm.的相关选项为你所需要的值,并启用pid文件(如下最后一行):
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /var/run/php-fpm.pid
# chkconfig --add php-fpm
# chkconfig php-fpm on
# service php-fpm start
# ps aux | grep php-fpm 验证其是否成功
nobody 3170 0.0 0.4 33596 2340 ? S 19:15 0:00 php-fpm: pool www
nobody 3171 0.0 0.4 33596 2336 ? S 19:15 0:00 php-fpm: pool www
nobody 3172 0.0 0.4 33596 2340 ? S 19:15 0:00 php-fpm: pool www
(4)将nginx和php进行整合
# vim /etc/nginx/nginx.conf
server {
listen 80;
server_name www.feng.com;
index index.php index.html;
root /nginx/feng/htdocs;
location ~ \.php$ {
root /nginx/feng/htdocs;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
}
# vim /etc/nginx/fastcgi_params 将其内容更改为如下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# service nginx reload 重新载入nginx的配置文件
# cd nginx/feng/htdocs/
# cp index.html index.php
# vim index.php
<?php
phpinfo();
?>
接着就可以通过浏览器访问此测试页面了
(5)安装phpMyAdmin
在ftp上下载并安装,执行下面的命令
# tar xf phpMyAdmin-3.4.10.1-all-languages.tar
# mkdir nginx/feng/htdocs/pma
# mv phpMyAdmin-3.4.10.1-all-languages nginx/feng/htdocs/pma
然后在浏览器中输入www.feng.com/pma 即可进入数据库了
记得在你自己的的主机的浏览器中,要使检查能够成功,需要在hosts文件中,将你的虚拟机的IP和网址写在里面。