LNMP是linux、nginx、mysql、php的简写;LNMP与LAMP环境一样也是用来做web网站后台的,nginx是轻量级的,进程间的通讯使用php-fpm独立使用;apache则是比较臃肿的,调用php的模块来完成的,需要加载很多模块,运行起来相对较慢。
一、安装MySQL
我们平时安装MySQL都是源码包安装的,但是由于它的编译需要很长的时间,我们这里选择安装二进制免编译包。你可以到MySQL官方网站去下载 http://dev.mysql.com/downloads/ 具体版本根据你的平台和需求而定,目前比较常用的为mysql-5.0/mysql-5.1, 5.5版本虽然已经发布有段日子了,但是貌似用在线上跑服务的还是少数。
具体的步骤如下:
1、下载mysql安装文件到/usr/local/src 目录下
1 2 | [root@yong src]
--2015-04-27 09:23:01-- http: //syslab .comsenz.com /downloads/linux/mysql-5 .1.40-linux-i686-icc-glibc23. tar .gzResolving syslab.comsenz.com... 101.227.130.115Connecting to syslab.comsenz.com|101.227.130.115|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 123633020 (118M) [application /octet-stream ]Saving to: “mysql-5.1.40-linux-i686-icc-glibc23. tar .gz”
|
2、解压下载的文件
3、把解压完的数据移动到/usr/local/mysql
提醒注意:不用自己手动添加/mysql目录,移动的同时会把原目录改名的。
4、建立mysql用户,禁止登录系统;
5、初始化数据库,新建/data目录及/data/mysql子目录,并设置/data/mysql目录的所属主所属组为mysql用户;
1 2 3 4 5 6 7 8 | [root@yong src]
[root@yong mysql]
[root@yong mysql]
[root@yong mysql]
Installing MySQL system tables...
OK
Filling help tables...
OK
|
--user定义数据库的所属主,--datadir 定义数据库的安装路径,建议放到大存储空间。看到两个"OK"说明执行正确,否则请仔细查看错误信息,查询哪里出了问题;
6、拷贝配置文件
很多模板配置文件在/support-files/目录下;
根据内存大小选择:
my-small.cnf (内存 <= 64M)
my-medium.cnf (内存 128M )
my-large.cnf (内存 512M)
my-huge.cnf (内存 1G-2G)
my-innodb-heavy-4G.cnf (内存 4GB)
7、拷贝启动脚本文件并修改属性
1 2 | [root@yong mysql]
[root@yong mysql]
|
8、修改启动脚本
需要修改的地方有"basedir=/usr/local/mysql"(mysql的安装目录)
”datadir=/data/mysql“ (前面初始化数据库定义的目录)
9、把启动脚本加入系统服务项,并设定开机启动,启动mysql服务;
1 2 3 4 | [root@yong mysql]
[root@yong mysql]
[root@yong mysql]
Starting MySQL.. [ OK ]
|
10、查看mysqld是否启动
1 2 3 | [root@localhost mysql]
root 1341 0.0 0.1 6680 1328 pts /0 S 20:22 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir= /data/mysql --pid- file = /data/mysql/localhost .localdomain.pid
mysql 1456 0.3 4.2 381608 43504 pts /0 Sl 20:22 0:01 /usr/local/mysql/bin/mysqld --basedir= /usr/local/mysql --datadir= /data/mysql --user=mysql --log-error= /data/mysql/localhost .localdomain.err --pid- file = /data/mysql/localhost .localdomain.pid --socket= /tmp/mysql .sock --port=3306
|
二、安装php
1、下载php5.4.37压缩包到/usr/local/src目录下;解压缩包,然后进入解压之后的目录;
1 2 3 | [root@localhost src]
[root@localhost src]
[root@localhost src]
|
2、创建php-fpm用户,并禁止登录;
1 | [root@localhost php-5.4.37]
|
3、配置php的各项参数;
1 | [root@localhost php-5.4.37]
|
配置遇到的各项错误,根据错误提示进行安装所需要的包;
提示错误,没有可接受的C编译器,安装gcc包;
checking for cc... no
checking for gcc... no
configure: error: in `/usr/local/src/php-5.4.37':
configure: error: no acceptable C compiler found in $PATH
1 | [root@localhost php-5.4.37]
|
再次执行./configure,提示错误xml2-config没有找到,安装libxml2-devel包;
configure: error: xml2-config not found. Please check your libxml2 installation
1 | [root@localhost php-5.4.37]
|
再次执行./configure,提示错误需要重新安装libcurl,安装libcurl-devel包;
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/
1 | [root@localhost php-5.4.37]
|
再次执行./configure,提示错误jpeglib.h没有找到,安装libjpeg-devel包;
configure: error: jpeglib.h not found.
1 | [root@localhost php-5.4.37]
|
再次执行./configure,提示错误png.h没有找到,安装libpng-devel包;
configure: error: png.h not found.
1 | [root@localhost php-5.4.37]
|
再次执行./configure,提示错误freetype-config没有找到,安装freetype-devel包;
configure: error: freetype-config not found.
1 | [root@localhost php-5.4.37]
|
再次执行./configure,提示错误mcrypt.h没有找到,安装libmcrypt-devel包,默认的yum源,没有这个包,需要安装epel扩展源后,才可以安装。
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
1 2 | [root@localhost php-5.4.37]
[root@localhost php-5.4.37]
|
再次执行./configure,没有错误提示,出现Thank you for using PHP,配置OK。

4、安装php,安装完成后使用echo $?查看是否安装正确;
1 2 3 | [root@localhost php-5.4.37]
[root@localhost php-5.4.37]
0
|
5、拷贝php配置文件
1 | [root@localhost php-5.4.37]
|
6、拷贝php启动脚本,php-fpm配置文件,更改php-fpm权限为755;添加php-fpm开机启动;
1 2 | [root@localhost php-5.4.37]
[root@localhost php-5.4.37]
|
另一种改名方法,使用花括号{},同一个目录下;-v 为可视化显示;
mv -v /usr/local/php/etc/{php-fpm.conf.default,php-fpm.conf}
7、更改php-fpm的权限为755;添加php-fpm到系统启动项,并设置开机启动;启动php-fpm;
1 2 3 4 5 | [root@localhost php-5.4.37]
[root@localhost php-5.4.37]
[root@localhost php-5.4.37]
Starting php-fpm done
[root@localhost php-5.4.37]
|
三、安装nginx
1、下载nginx1.6.2压缩包到/usr/local/src目录下,解压缩包,之后进入解压缩后的目录;
1 2 3 4 | [root@localhost ~]
[root@localhost src]
[root@localhost src]
[root@localhost src]
|
2、配置nginx
1 | [root@localhost nginx-1.6.2]
|
提示错误,HTTP重写模块需要PCRE库,需要安装pcre-devel包;
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
1 | [root@localhost nginx-1.6.2]
|
再次执行./configure,没有错误;
3、安装nginx,安装完成后echo $?查看是否安装正确;
1 2 3 | [root@localhost nginx-1.6.2]
[root@localhost nginx-1.6.2]
0
|
4、启动nginx,ps查看nginx的进程,nginx的用户为nobody;
1 2 3 4 | [root@localhost nginx-1.6.2]
[root@localhost ~]
root 986 0.0 0.0 3552 528 ? Ss 08:56 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx .conf
nobody 988 0.0 0.0 3752 884 ? S 08:56 0:00 nginx: worker process
|
编写nginx启动脚本,加入##号内的内容;
1 | [root@localhost nginx-1.6.2]
|
###########################################################################
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
###########################################################################
5、设置nginx权限755,加入开机启动;
1 2 3 4 5 | [root@localhost nginx-1.6.2]
[root@localhost nginx-1.6.2]
-rwxr-xr-x. 1 root root 1211 5月 11 17:37 /etc/init .d /nginx
[root@localhost nginx-1.6.2]
[root@localhost nginx-1.6.2]
|
四、配置解析php
编辑nginx配置文件,找到下面的代码,删除前面的#号,更改 fastcgi_param这一行,加入nginx存放路径;

1 2 3 4 5 6 7 8 | [root@localhost nginx-1.6.2]
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html $fastcgi_script_name;
include fastcgi_params;
}
|
保存退出,重新加载nginx