LEMP服务器搭建
一.环境和软件:
linux Redhat 6.4 +Nginx-1.0.8 + mysql-5.1.55 + php-5.3.6
ZendGuardLoader-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/sbin
MySQL 数据库位置 /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.gz
cd nginx-1.0.8
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx--with-http_stub_status_module
make && make install
4.配置Nginx
ln –s /usr/local/nginx/sbin/nginx/usr/local/sbin
做Nginx启动脚本,使Nginx 作为系统服务启动
脚本内容:vim/etc/init.d/nginx
==================================
#!/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 is OK..." /bin/true } || action "Nginxstart is error..." /bin/false fi } stop() { if [ ! -e $pid ] then action "Nginx isnot running..." /bin/false&& exit 1 else kill -s QUIT $(cat$pid) [ $? -eq 0 ] &&action "Nginx stop is OK..."/bin/true || action "Nginx stop is error..." /bin/false fi } restart() { $0 stop $0 start } reload() { if [ ! -e $pid ] then action "Nginx isnot running..." /bin/false&& exit 1 else kill -s HUP $(cat $pid) [ $? -eq 0 ] &&action "Nginx reload is OK..."/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-pthread
make && 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/mysqld
chkconfig --add mysqld
ln -s /usr/local/mysql-5.1.56/ /usr/local/mysql
ln -s /usr/local/mysql/bin/* /usr/local/bin
ln -s/usr/local/mysql/lib/mysql/* /usr/lib
ln -s/usr/local/mysql/include/mysql /usr/include
useradd -M -s /sbin/nologin mysql
cd /usr/local/mysql/bin
./mysql_install_db --user=mysql #初始化数据库
chown -R root:mysql /usr/local/mysql
chown -R mysql /usr/local/mysql/var
四.编译安装php
1) 安装libiconv
cd/usr/src
wgethttp://down1.chinaunix.net/distfiles/libiconv-1.14.tar.gz
tarzxf libiconv-1.14.tar.gz
cdlibiconv-1.14
./configure--prefix=/usr/local/libiconv
make&& make install
2) 安装libmcrypt
cd/usr/src
wgethttp://down1.chinaunix.net/distfiles/libmcrypt-2.5.7.tar.gz
tarzxf libmcrypt-2.5.7.tar.gz
cdlibmcrypt-2.5.7
./configure
make
make install
2) 安装mhash
cd/usr/src
wgethttp://down1.chinaunix.net/distfiles/mhash-0.9.3.tar.gz
tarzxf mhash-0.9.3.tar.gz
cdmhash-0.9.3
./configure
make
make install
4)安装mcrypt
cd/usr/src
wgethttp://down1.chinaunix.net/distfiles/mcrypt-2.6.4.tar.gz
tarzxf mcrypt-2.6.4.tar.gz
cdmcrypt-2.6.4
echo"/usr/local/lib" >>/etc/ld.so.conf
/sbin/ldconfig
./configure
make
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 -y
2)下载PHP包进行编译安装
cd/usr/src
wgethttp://down1.chinaunix.net/distfiles/php-5.3.6.tar.bz2
tarjxf php-5.3.6.tar.bz2
cdphp-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
make
makeinstall
补充:
报错: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/bin
ln -sf /usr/local/php/sbin/* /usr/local/sbin
五.配置Nginx支持php环境
方法一,充当介质。将访问php页面的请求转交给其他服务器处理
方法二。通过本身自带的fpm模块来调用本机的php环境
1. 启用php-fpm进程
cd /usr/local/php5/etc
cp php-fpm.conf.default php-fpm.conf
vim 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-fpm
chmod o+x /etc/init.d/php-fpm
service php-fpm start
chkconfig --add php-fpm
chkconfig 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_index index.php ; php首页文件
include fastcgi.conf; 包括fastcgi.conf样本配置
}
4.重启Nginx服务
service nginxrestart
六.测试。
在Nginx网站根目录里编写测试文件index.php
echo"<?php phpinfo(); ?>" >>/usr/local/nginx/html/test.php
本文出自 “Study-Everyday” 博客,请务必保留此出处http://studys.blog.51cto.com/9736817/1632494
原文地址:http://studys.blog.51cto.com/9736817/1632494