LNMP环境的搭建
一.环境和软件:linux Redhat 6.4 +Nginx-1.0.8 + mysql-5.1.55 + php-5.3.6ZendGuardLoader-php-5.3-linux-glibc23系统分区建议/boot 100M (大约100左右)SWAP 物理内存的2倍(如果你的物理内存大于4G,分配4G即可)/ 分区15~20G/usr/local 20G (用于安装软件)/data 剩余所有空间.
软件源代码包存放位置 /usr/local/src源码包编译安装位置(prefix) /usr/local/software_name脚本以及维护程序存放位置 /usr/local/sbinMySQL 数据库位置 /data/mysql/3306/data(可按情况设置)网站根目录 /data/htdocs(可按情况设置)虚拟主机日志根目录 /data/logs(可按情况设置)Nginx运行账户 nginx:nginx二.编译安装Nginx
[*]1. Nginx的配置需要有pcre,zlib等软件包支持,先安装必备软件包
yum install pcre-devel zlib-devel –y 2.创建Nginx用户
useradd-M -s/sbin/nologin nginx 3.解压Nginx安装包,编译安装(确保开发环境,否则yum –yinstall gcc*)tar zxf nginx-1.0.8.tar.gzcd nginx-1.0.8./configure --prefix=/usr/local/nginx --user=nginx --group=nginx--with-http_stub_status_modulemake && make install 4.配置Nginx
ln –s/usr/local/nginx/sbin/nginx/usr/local/sbin
做Nginx启动脚本,使Nginx 作为系统服务启动
脚本内容:vim/etc/init.d/nginx==================================
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
#chkconfig: - 85 15
#description: this is a script to control nginx
#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/local/nginx/sbin/nginx
pid=/usr/local/nginx/logs/nginx.pid
start() {
if[ -e $pid ]
then
action "Nginxalready running..." /bin/false && exit 1
else
$nginx
[ $? -eq 0 ] &&{
action "Nginxstart isOK..." /bin/true
} || action "Nginxstart is error..." /bin/false
fi
}
stop() {
if [ ! -e $pid ]
then
action "Nginx isnotrunning..." /bin/false&& exit 1
else
kill -s QUIT $(cat$pid)
[ $? -eq 0 ] &&action "Nginx stop isOK..."/bin/true || action "Nginx stop is error..." /bin/false
fi
}
restart() {
$0 stop
$0 start
}
reload() {
if [ ! -e $pid ]
then
action "Nginx isnotrunning..." /bin/false&& exit 1
else
kill -s HUP $(cat $pid)
[ $? -eq 0 ] &&action "Nginx reload isOK..."/bin/true || action "Nginx reload is error..." /bin/false
fi
}
usage() {
echo "Usage: $0 {start|stop|status|restart|reload"
}
status() {
if [ -e $pid ]
then
echo"Nginx is running..."
else
echo "Nginx isstop..."
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
status)
status
;;
*)
usage
;;
esac
===============================================
chmod +x /etc/init.d/nginx
三.编译安装mysql
[*]1.解压包tar zxf mysql-5.1.55.tar.gz
[*]2.若系统是32位的可直接编译安装,若系统是64位的,需要安装一些组件
yum install ncurses-devel
[*]2. 编译安装mysql
cdmysql-5.1.55./configure--prefix=/usr/local/mysql-5.1.56 --with-unix-socket-path=/usr/local/mysql-5.1.56/tmp/mysql.sock--with-charset=utf8 --with-collation=utf8_general_ci--with-extra-charsets=gbk,gb2312 --localstatedir=/usr/local/mysql-5.1.56/data --enable-assembler --with-mysqld-ldflags=-all-static--with-client-ldflags=-all-static--enable-thread-safe-client --with-mysqld-user=mysql--with-big-table --without-debug--with-pthreadmake && make install
[*]3. 配置mysql
cp support-files/my-medium.cnf /etc/my.cnf #创建mysql主配置文件cp support-files/mysql.server /etc/init.d/mysqld #创建mysql启动脚本chmod a+x /etc/init.d/mysqldchkconfig--add mysqldln -s /usr/local/mysql-5.1.56/ /usr/local/mysqlln-s /usr/local/mysql/bin/* /usr/local/binln-s/usr/local/mysql/lib/mysql/*/usr/libln-s/usr/local/mysql/include/mysql/usr/includeuseradd-M-s /sbin/nologin mysqlcd /usr/local/mysql/bin./mysql_install_db--user=mysql#初始化数据库chown -R root:mysql /usr/local/mysqlchown -R mysql /usr/local/mysql/var四.编译安装php
安装php所需的组件(libiconv,libmcrypt,mhash,mcrypt-install)1) 安装libiconvcd/usr/srcwgethttp://down1.chinaunix.net/distfiles/libiconv-1.14.tar.gztarzxf libiconv-1.14.tar.gzcdlibiconv-1.14./configure--prefix=/usr/local/libiconvmake&& make install2) 安装libmcryptcd/usr/src wgethttp://down1.chinaunix.net/distfiles/libmcrypt-2.5.7.tar.gztarzxf libmcrypt-2.5.7.tar.gzcdlibmcrypt-2.5.7./configure
make
make install2) 安装mhashcd/usr/src wgethttp://down1.chinaunix.net/distfiles/mhash-0.9.3.tar.gztarzxf mhash-0.9.3.tar.gzcdmhash-0.9.3./configuremake
make install4)安装mcryptcd/usr/src wgethttp://down1.chinaunix.net/distfiles/mcrypt-2.6.4.tar.gztarzxf mcrypt-2.6.4.tar.gzcdmcrypt-2.6.4echo"/usr/local/lib" >>/etc/ld.so.conf/sbin/ldconfig./configuremake
make install补充:如果在make过程中报:/usr/local/include/mutils.h:27:26:error: mhash_config.h: No such file or directory请执行下面命令,然后重新make编译find/ -name mhash_config.h -exec cp -f {} /usr/local/include/ \;安装PHP
1)安装所需组件包yuminstall zlib libxml2 libjpeg freetype libpng gd curl zlib-devel libxml2-devellibjpeg-devel freetype-devel libpng-devel gd-devel openssl openssl-develcurl-devel -y2)下载PHP包进行编译安装cd/usr/src wgethttp://down1.chinaunix.net/distfiles/php-5.3.6.tar.bz2 tarjxf php-5.3.6.tar.bz2cdphp-5.3.6./configure--prefix=/usr/local/php --with-mcrypt --with-mysql=/usr/local/mysql --enable-fpm--with-config-file-path=/usr/local/php --enable-mbstring-with-iconv=/usr/local/libiconv --enable-static --with-xmlrpc --with-openssl --with-zlib --with-freetype-dir --with-gd--with-jpeg-dir --enable-short-tags --enable-sockets --enable-zend-multibyte--enable-soap --enable-gd-native-ttf --enable-curl --enable-xsl--with-libxml-dir makemakeinstall
补充:报错:configure: error: libpng.(a|so) not found.原因:在/usr/lib下没有libpng.so这个文件,需要从/usr/lib64/下做软连接解决:ln -s /usr/lib64/libpng.so /usr/lib/libpng.so
配置
cp php.ini-production /usr/local/php/php.ini#建立主配置文件ln -sf /usr/local/php/bin/* /usr/local/binln -sf /usr/local/php/sbin/* /usr/local/sbin五.配置Nginx支持php环境
方法一,充当介质。将访问php页面的请求转交给其他服务器处理方法二。通过本身自带的fpm模块来调用本机的php环境
[*]1. 启用php-fpm进程
cd/usr/local/php5/etccp php-fpm.conf.default php-fpm.confvim php-fpm.conf启用以下参数pid = run/php-fpm.pid 确定pid的位置user = nginx 运行用户group = nginx 运行组pm.start_servers = 20 启动时开启的进程数pm.min_spare_servers = 5 最少空闲进程数pm.max_spare_servers = 35 最多空闲进程数
[*]2. 添加php-fpm启动脚本,使php-fpm开机自动启动
cp /usr/src/php-5.3.6/sapi/fpm/init.d.php-fpm/etc/init.d/php-fpmchmod o+x /etc/init.d/php-fpm service php-fpm startchkconfig --add php-fpmchkconfig php-fpm on
[*]3. 修改Nginx配置文件
实现方法一.在server选项中添加location~\.php$ {proxy_pass http://另一台服务器ip}实现方法二。在server选项中添加location~ \.php${root/var/www/benet; php网页文档根目录fastcgi_pass 127.0.0.1:9000; php-fpm的监听地址fastcgi_indexindex.php ; php首页文件include fastcgi.conf; 包括fastcgi.conf样本配置}4.重启Nginx服务 service nginxrestart
六.测试。
在Nginx网站根目录里编写测试文件index.phpecho"<?php phpinfo(); ?>" >>/usr/local/nginx/html/test.php
页:
[1]