搭建LNMP平台
网站运行平台 L Linux操作系统 (rhel6.5)
N Nginx 提供网站服务和反向代理服务的软件
M MySQL 提供数据库服务软件
P PHP 动态网站编程语言 ,最后安装,安装过程中 要指定Mysql的安装位置
在ip地址是 192.169.0.30的服务器部署LNMP(源码包)
安装环境准备:
1 配置yum源
2 安装编译工具 rpm -q gcc gcc-c++ make
3 安装“开发工具”软件包组 yum -y groupinstall “开发工具”
4 service httpd stop ; chkconfig –level 35 httpd off#查看清理web服务
5 rpm -qa | grep –color -i mysql-server#查看清理sql服务
service mysqld stop ; chkconfig –level 35 mysqld off (系统自带包的服务名)
service mysql stop ; chkconfig –level 35 mysql off
rm -rf /etc/my.cnf
6 php 不是服务,是解释php代码文件的程序。 http://web-ip/a.php
[root@pc205 ~]# rpm -qa | grep –color -i php
7 下载安装包 lnmp+memcached.zip
一、安装源码Nginx (nginx-1.2.0.tar.gz)
grep www /etc/passwd
useradd -s /sbin/nologin -M www
tar -zxvf nginx-1.2.0.tar.gz
cd nginx-1.2.0
./configure --help
sh nginx.sh #安装脚本,脚本中指定的安装路径要清理干净
make
make install
安装前要安装pcre-devel,不然会报错如下:
./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= option.
安装pcre-devel:
rpm -qa | grep --color -i PCRE
yum -y install pcre-devel
启动并测试nginx服务:
[root@pc205 local]# netstat -untlap | grep :80
[root@pc205 local]# /usr/local/nginx/sbin/nginx
[root@pc205 local]# netstat -untlap | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32474/nginx
echo "aaaaaaa" > /usr/local/nginx/html/a.html
elinks --dump http://localhost
elinks --dump http://localhost/a.html
二、安装源码的mysql (mysql-5.5.13.tar.gz) 配置此版本时不用make包,而是 cmake包
1、 安装cmake 工具(cmake-2.8.10.2.tar.gz)
[root@localhost cmake-2.8.10.2]# ./bootstrap --prefix=/usr/local/cmake #只用bootstrap命令,并指定安装位置
[root@localhost cmake-2.8.10.2]# make
[root@localhost cmake-2.8.10.2]# make install
[root@pc205 local]# /usr/local/cmake/bin/cmake -version
cmake version 2.8.10.2
2、安装源码mysql:
rm -rf /etc/my.cnf
grep mysql /etc/passwd
useradd -s /sbin/nologin -M mysql#添加用户
tar -zxvf mysql-5.5.13.tar.gz
cd mysql-5.5.13
sh mysql.sh #运行安装脚本
错误警告:
CMake Error at cmake/readline.cmake:82 (MESSAGE): Curses library not found. Please install appropriate package,
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first): cmake/readline.cmake:117
(FIND_CURSES) cmake/readline.cmake:213 (MYSQL_USE_BUNDLED_READLINE)
CMakeLists.txt:250 (MYSQL_CHECK_READLINE)
解决方法
yum list | grep -i curses#查看curses相关软件
rpm -qa|grep -i curses#查看已安装的curses相关软件
yum -y install ncurses-devel; #安装
rm -rf CMakeCache.txt ; #删除cmake的缓存信息,不然会重复错误
sh mysql.sh
make && make install
3、初始化mysql授权库,此操作要在创建主配置文件之前:
cd /usr/local/mysql
./scripts/mysql_install_db --user=mysql #要在mysql目录下使用相对路径,不然找不到,初始化授权库,指定所有者为mysql
ls /usr/local/mysql/data/mysql/*.frm #查看结果,数据表结构
4 、创建主配置文件 /etc/my.cnf
cd /usr/local/mysql
cp support-files/my-small.cnf /etc/my.cnf #文件夹support-files中有许多配置模板文件
5、启动数据库服务 :
[root@pc205 mysql]# netstat -utnalp | grep :3306
[root@pc205 mysql]# /usr/local/mysql/bin/mysqld_safe --user=mysql & #后台执行启动,会提示错误日志的位置和数据位置
[root@pc205 bin]# netstat -utnalp | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 58548/mysqld
使用数据库管理员从数据库服务器本机登录(默认无密码):
[root@pc205 ~]# /usr/local/mysql/bin/mysql -uroot -p
Enter password: #不须输入密码,回车
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.13 Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>#启动成功
默认各个数据库文件中只存放各自的数据结构文件,所有的数据都统一保存在一个文件里。
设置数据库管理员从本机登录的密码:
/usr/local/mysql/bin/mysqladmin -hlocalhost -uroot password "123"#重启sql后生效
重启mtsql:
jobs#查看当前终端运行的进程
kill -9 %1 (killall/pkill -9 mysqld) #关闭mysql,jobs无效时运行括号内的。
/usr/local/mysql/bin/mysqld_safe --user=mysql & #后台启动
使用密码重新登录:
[root@pc205 ~]# /usr/local/mysql/bin/mysql -uroot -p123
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.13 Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
修改数据库管理员从本机登录的密码:
[root@pc205 ~]# /usr/local/mysql/bin/mysqladmin -hlocalhost -uroot -p password "456"
Enter password: 旧密码
使用新密码登录:
[root@pc205 ~]# /usr/local/mysql/bin/mysql -uroot -p456
综上:
/usr/local/mysql/bin/mysqd_safe –user=mysql & 启动服务
/usr/local/mysql/bin/mysql -uroot -p 管理员登录
/usr/local/mysql/bin/mysqladmin -hlocalhost -uroot password “密码” 设置密码
/usr/local/mysql/bin/mysqladmin -hlocalhost -uroot -p password “新密码” 修改密码
为了管理方便,把源码mysql命令所在的路径,添加到系统环境变量PATH:
[root@pc205 ~]# PATH=/usr/local/mysql/bin:$PATH#临时更改
vim /etc/bashrc#永久更改
export PATH=/usr/local/mysql/bin:$PATH
source /etc/bashrc#刷新
[root@pc205 ~]# mysql -uroot -p456
把源码mysql添加为系统服务 ,服务名叫mysqldd:
kill -9 %1 #关闭mysql服务
cp support-files/mysql.server /etc/init.d/mysqldd #自带的启动脚本
chmod +x /etc/init.d/mysqldd #添加执行权限
chkconfig --add mysqldd #添加系统服务
service mysqldd status|start|stop
[root@pc205 mysql]# vim /etc/ld.so.conf#系统加载库文件配置,将mysql的库文件添加到系统中,让mysql服务能找到自己的库。rpm安装不会存在这些问题。
include ld.so.conf.d/*.conf
/usr/local/mysql/lib/
/usr/local/mysql/include/
[root@pc205 mysql]# ldconfig #刷新
安装源码php( php-5.4.9.tar.gz )
1 安装扩展功能软件包,默认安装在/usr/local下的lib,bin下
mhash-0.9.9.9.tar.gz 哈稀函数库
tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure && make && make install
libiconv-1.13.tar.gz 处理中文各种编码之间的转换
tar -zxvf libiconv-1.13.tar.gz
cd libiconv-1.13
./configure && make &&make install
libmcrypt-2.5.8.tar.gz 提供加密功能的库文件
tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure && make && make install
libmcrypt-2.5.8中包含另一个软件libltdl,在当前目录下继续安装:
cd libltdl
ldconfig -v#加载系统库文件
./configure -h #查看帮助信息
./configure --with-gmetad --enable-gexec --enable-ltdl-install
make && make install
将刚才安装的库文件链接到系统目录下:
ln -sv /usr/local/lib/libmcrypt* /usr/lib/
ln -sv /usr/local/lib/libmhash.* /usr/lib/
ln -sv /usr/local/lib/libiconv.* /usr/lib/
[root@pc205 local]# ldconfig -v
安装php:
tar -zxvf php-5.4.9.tar.gz
cd php-5.4.9
sh php.sh
根据错误提示继续安装功能包:
configure: error: xml2-config not found. Please check your libxml2 installation.#错误
yum -y install libxml2-devel
configure: error: Please reinstall the libcurl distribution -easy.h should be in <curl-dir>/include/curl/
yum -y install libcurl-devel#错误
configure: error: jpeglib.h not found.#错误
yum -y install libjpeg-turbo-devel
configure: error: png.h not found.#错误
yum -y install libpng-devel
configure: error: freetype.h not found.#错误
yum -y install freetype-devel
configure: error: Cannot find ldap.h#错误
yum -y install openldap-devel.i686
configure: error: LDAP SASL check failed. Please check config.log for more information.#错误
yum -y install openldap-devel
直接make会报错:
/root/桌面/lnmp+memcached/php-5.4.9/ext/xmlrpc/libxmlrpc/encodings.c:101:
undefined reference to `libiconv_close’ collect2: ld returned 1 exit
status make: * [sapi/cli/php] 错误 1
解决方法:
make ZEND_EXTRA_LIBS='-liconv' #要加参数
make install
[root@pc205 local]# ls /usr/local/php/#查看安装结果
bin etc include lib php sbin var
创建php程序的主配置文件 php.ini:
cd php-5.4.9#进入源码包
cp php.ini-production /usr/local/php/etc/php.ini #(; 是注释符号)
/usr/local/php/bin/php -m #查看自己支持的功能
三 、测试
1、客户端能否访问Nginx软件提供的网站服务
netstat -utnalp | grep :80
elinks --dump http://localhost
echo 123 > /usr/local/nginx/html/test.html
elinks --dump http://localhost/test.html
2、客户端能否通过nginx网站服务器访问.php的网页文件:
暂时无法正常显示php页面,需要nginx+Fast-cgi。
公共网关接口(Common Gateway Interface):HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,程序须运行在网络服务器上。CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量,如php,perl等。 php 安装时添加的配置项(–enable-fpm),启用Fast-cgi,使web服务能够解析php文件 。fpm是php的监听进程。
安装fpm:
[root@pc205 ~]#cd php-5.4.9/sapi/fpm/#源码目录
[root@pc205 fpm]# cp init.d.php-fpm /etc/init.d/fpm#ftp启动脚本
[root@pc205 fpm]# chmod +x /etc/init.d/fpm
cd /usr/local/php/etc/ #安装目录
cp php-fpm.conf.default php-fpm.conf #fpm主配置文件,监听本地环回9000端口
netstat -utnalp | grep :9000
[root@pc205 fpm]# /etc/init.d/fpm start|stop#开启/关闭fpm
Starting php-fpm done
[root@pc205 fpm]# netstat -untlap | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 61429/php-fpm
配置nginx:
vim /usr/local/nginx/conf/nginx.conf
location ~ \.php$ {#配置文件中存在有这些配置,解除注释
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;#
include fastcgi_params;
}
修改当前目录下的fastcgi_params:
vim fastcgi_params#在文件中完成从内置变量到标准变量的转换
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#添加一行
检查配置:
[root@pc205 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重新启动nginx:
[root@pc205 conf]# /usr/local/nginx/sbin/nginx -s stop
[root@pc205 conf]# /usr/local/nginx/sbin/nginx
测试php文件源码:
[root@pc205 php]# vim /usr/local/nginx/html/test2.php
<?php
phpinfo();
?>
3、php能否连接mysql数据库服务器:
开启mysql:
netstat -untlap | grep :3306#检查mysql启动情况
测试网页:
vim /usr/local/nginx/html/linkdb.php
<?php
$linkdb=mysql_connect("localhost","root","456");#php内置访问mysql函数
if($linkdb){
echo "link db ok";
}else{
echo "link db no";
}
?>
测试结果:
[root@pc205 etc]# elinks --dump http://localhost/linkdb.php
link db ok
LNMP+ Memcached :
在192.168.0.30服务器上运行memcached服务:
[root@pc205 ~]# /usr/local/bin/memcached -u root -d
安装连接memcached服务的连接工具 ,( memcache-2.2.5.tgz):
注意:是memcache不是memcached!!
[root@pc205 ~ ]# /usr/local/php/bin/php -m | grep --color mem# 检查是否支持memcache,无结果,不支持
tar -zxvf memcache-2.2.5.tgz
cd memcache-2.2.5
[root@pc205 memcache-2.2.5]# /usr/local/php/bin/phpize#查看php信息,能出现以下信息
Configuring for:
PHP Api Version: 20100412
Zend Module Api No: 20100525
Zend Extension Api No: 220100525
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache#配置
make
[root@pc205 memcache-2.2.5]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/#输出安装的路径信息
[root@pc205 ~ ]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/#查看
memcache.so#路径下有提供连接工具的模块
修改php的主配置文件php.ini 调用memcache.so模块:
vim /usr/local/php/etc/php.ini
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"#查找该配置,解除注释,修改配置
extension = "memcache.so"#模块名称
重新启动memcached,并检查:
[root@pc205 etc]# /etc/init.d/fpm stop
Gracefully shutting down php-fpm . done
[root@pc205 etc]# /etc/init.d/fpm start
Starting php-fpm done
[root@pc205 etc]# /usr/local/php/bin/php -m | grep --color mem
memcache#成功支持
修改nginx的主配置文件,自己接收到请求后,先找memcached ,在memcahced里找不到时再到自己的网页目录下去找:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
set $memcached_key $uri;#uri是url剃掉主机名,赋值给memcached_key
memcached_pass 127.0.0.1:11211;#转发给memcached
default_type text/html;
error_page 404 @fallback;#必须以@首字符命名
}
error_page 404 @fallback;
error_page 500 502 503 504 /50x.html;
location @fallback {
root html;#到root下查找网页
index index.php; #指定首页名叫index.php
}
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
}
}
测试网页文件:
[root@pc205 etc]# cat /usr/local/nginx/html/mem.php
<?php
$memcache=new Memcache; #生成memcache实例
$memcache->connect('localhost',11211) or die ('could not connect!!');#调用memcache
$memcache->set("name","jim");
$var=$memcache->get("name");#memcached数据存放
echo $var;
?>
测试结果:
elinks --dump http://localhost/mem.php
jim
未解决:访问网站首页错误!!!!!