天天看点

利用×××lnmp环境

LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。

Mysql是一个小型关系型数据库管理系统。

PHP是一种咋服务器端执行的嵌入HTML文档的脚本语言。

这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。

下面我们通过实例搭建lnmp

安装nginx前准备工作:

首先安装pcre包

[root@localhost ~]# yum list all |grep pcre

This system is not registered with RHN.

RHN support will be disabled.

pcre.i386 6.6-2.el5_1.7 installed

pcre-devel.i386 6.6-2.el5_1.7 rhel-server

[root@localhost ~]# yum install pcre-devel.i386

Installed:

pcre-devel.i386 0:6.6-2.el5_1.7

Complete!

安装nginx需要的包

libevent-2.0.16-stable.tar.gz

nginx-1.0.11.tar.gz

解压包

[root@localhost ~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/local/src/

[root@localhost ~]# cd /usr/local/src/

[root@localhost src]# ll

总计 4

drwxr-xr-x 8 nxl nxl 4096 2011-11-19 libevent-2.0.16-stable

检测预编译环境

[root@localhost src]# cd libevent-2.0.16-stable/

[root@localhost libevent-2.0.16-stable]# ./configure

修改库路径

[root@localhost local]# vim /etc/ld.so.conf.d/libevent.conf

/usr/local/lib

~

编译

[root@localhost libevent-2.0.16-stable]# make

将形成的文件放到合适位置

[root@localhost libevent-2.0.16-stable]# make install

手工调用库

[root@localhost libevent-2.0.16-stable]# ldconfig -v

查看调用结果

[root@localhost libevent-2.0.16-stable]# ldconfig -pv |grep libevent

libevent_pthreads-2.0.so.5 (libc6) => /usr/local/lib/libevent_pthreads-2.0.so.5

libevent_openssl-2.0.so.5 (libc6) => /usr/local/lib/libevent_openssl-2.0.so.5

libevent_extra-2.0.so.5 (libc6) => /usr/local/lib/libevent_extra-2.0.so.5

libevent_core-2.0.so.5 (libc6) => /usr/local/lib/libevent_core-2.0.so.5

libevent-2.0.so.5 (libc6) => /usr/local/lib/libevent-2.0.so.5

libevent-1.1a.so.1 (libc6) => /usr/lib/libevent-1.1a.so.1

安装nginx

解压缩

[root@localhost ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src/

进入目录

[root@localhost src]# cd nginx-1.0.11/

创建组

[root@localhost nginx-1.0.11]# groupadd -r nginx

创建帐号

[root@localhost nginx-1.0.11]# useradd -r -g nginx -s /bin/false -M nginx

[root@localhost nginx-1.0.11]# ./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/ \

./configure: error: invalid option " "

[root@localhost nginx-1.0.11]# --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

> --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

> --with-pcre

[root@localhost nginx-1.0.11]# make

[root@localhost nginx-1.0.11]# make install

启动服务

[root@localhost sbin]# nginx

查看端口

[root@localhost sbin]# netstat -tupln |grep nginx

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11745/nginx: master

测试nginx

利用×××lnmp环境

2.安装mysql

解压软件包

tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local

建立软连接

[root@localhost local]# ln -s mysql-5.5.15-linux2.6-i686 mysql

[root@localhost local]# cd mysql

创建组和账号

[root@localhost mysql]# groupadd mysql

[root@localhost mysql]# useradd -r -g mysql mysql

[root@localhost mysql]# cd /usr/local/

改变文件所有者和所有组

[root@localhost mysql]# chown -R mysql .

[root@localhost mysql]# chgrp -R mysql .

[root@localhost mysql]# ll

总计 76

drwxr-xr-x 2 mysql mysql 4096 08-20 22:15 bin

-rw-r--r-- 1 mysql mysql 17987 2011-07-14 COPYING

drwxr-xr-x 4 mysql mysql 4096 08-20 22:15 data

drwxr-xr-x 2 mysql mysql 4096 08-20 22:16 docs

drwxr-xr-x 3 mysql mysql 4096 08-20 22:15 include

-rw-r--r-- 1 mysql mysql 7604 2011-07-14 INSTALL-BINARY

drwxr-xr-x 3 mysql mysql 4096 08-20 22:16 lib

drwxr-xr-x 4 mysql mysql 4096 08-20 22:14 man

drwxr-xr-x 10 mysql mysql 4096 08-20 22:15 mysql-test

-rw-r--r-- 1 mysql mysql 2552 2011-07-14 README

drwxr-xr-x 2 mysql mysql 4096 08-20 22:15 scripts

drwxr-xr-x 27 mysql mysql 4096 08-20 22:15 share

drwxr-xr-x 4 mysql mysql 4096 08-20 22:16 sql-bench

drwxr-xr-x 2 mysql mysql 4096 08-20 22:16 support-files

进行初始化

[root@localhost mysql]# scripts/mysql_install_db --user=mysql

Installing MySQL system tables...

OK

Filling help tables...

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'

./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:

./bin/mysql_secure_installation

which will also give you the option of removing the test

databases and anonymous user created by default. This is

strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems with the ./bin/mysqlbug script!

[root@localhost mysql]#

改回所有者和所有组

[root@localhost mysql]# chown -R root .

drwxr-xr-x 2 root mysql 4096 08-20 22:15 bin

-rw-r--r-- 1 root mysql 17987 2011-07-14 COPYING

drwxr-xr-x 5 root mysql 4096 08-20 22:32 data

drwxr-xr-x 2 root mysql 4096 08-20 22:16 docs

drwxr-xr-x 3 root mysql 4096 08-20 22:15 include

-rw-r--r-- 1 root mysql 7604 2011-07-14 INSTALL-BINARY

drwxr-xr-x 3 root mysql 4096 08-20 22:16 lib

drwxr-xr-x 4 root mysql 4096 08-20 22:14 man

drwxr-xr-x 10 root mysql 4096 08-20 22:15 mysql-test

-rw-r--r-- 1 root mysql 2552 2011-07-14 README

drwxr-xr-x 2 root mysql 4096 08-20 22:15 scripts

drwxr-xr-x 27 root mysql 4096 08-20 22:15 share

drwxr-xr-x 4 root mysql 4096 08-20 22:16 sql-bench

drwxr-xr-x 2 root mysql 4096 08-20 22:16 support-files

data目录除外,所有者和所有组都是mysql

[root@localhost mysql]# chown -R mysql data

drwxr-xr-x 2 root mysql 4096 08-20 22:15 bin

-rw-r--r-- 1 root mysql 17987 2011-07-14 COPYING

drwxr-xr-x 5 mysql mysql 4096 08-20 22:32 data

drwxr-xr-x 2 root mysql 4096 08-20 22:16 docs

drwxr-xr-x 3 root mysql 4096 08-20 22:15 include

-rw-r--r-- 1 root mysql 7604 2011-07-14 INSTALL-BINARY

drwxr-xr-x 3 root mysql 4096 08-20 22:16 lib

drwxr-xr-x 4 root mysql 4096 08-20 22:14 man

drwxr-xr-x 10 root mysql 4096 08-20 22:15 mysql-test

-rw-r--r-- 1 root mysql 2552 2011-07-14 README

drwxr-xr-x 2 root mysql 4096 08-20 22:15 scripts

drwxr-xr-x 27 root mysql 4096 08-20 22:15 share

drwxr-xr-x 4 root mysql 4096 08-20 22:16 sql-bench

drwxr-xr-x 2 root mysql 4096 08-20 22:16 support-files

拷贝配置文件

[root@localhost mysql]# cp support-files/my-medium.cnf /etc/my.cnf

启动mysql

[root@localhost mysql]# bin/mysqld_safe --user=mysql &

[1] 3782

[root@localhost mysql]# 120820 22:42:26 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdoma

120820 22:42:26 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

[root@localhost mysql]# netstat -tupln |grep mysql

tcp        0      0 :::3306                     :::*                        LISTEN      4020/mysqld        

为使方便启动mysql可以用下列方式

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld

[root@localhost mysql]# service mysqld stop

Shutting down MySQL.120820 22:48:10 mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended

                                                           [确定]

[1]+ Done                    bin/mysqld_safe --user=mysql

[root@localhost mysql]# service mysqld start

Starting MySQL..                                           [确定]

tcp        0      0 :::3306                     :::*                        LISTEN      4359/mysqld 

每次开机自动启用

[root@localhost mysql]# chkconfig --add mysqld

[root@localhost mysql]# chkconfig --list |grep mysql

mysqld            0:关闭      1:关闭      2:启用      3:启用      4:启用      5:启用      6:关闭

[root@localhost mysql]# chkconfig mysqld

bin/            docs/           lib/            README          sql-bench/     

COPYING         include/        man/            scripts/        support-files/ 

data/           INSTALL-BINARY mysql-test/     share/         

[root@localhost mysql]# chkconfig mysqld on

调用库文件

[root@localhost mysql]# cd /etc/ld.so.conf.d/

[root@localhost ld.so.conf.d]# vim mysql.conf

/usr/local/mysql/lib

~                       

调用头文件

[root@localhost ld.so.conf.d]# cd /usr/local/mysql

drwxr-xr-x 5 mysql mysql 4096 08-20 22:48 data

drwxr-xr-x 3 root mysql 4096 08-20 23:05 lib

[root@localhost mysql]# cd /usr/include/

[root@localhost include]# ln -s /usr/local/mysql/include/ mysql

3.安装php

[root@localhost ~]# tar -jxvf php-5.3.7.tar.bz2 –C /usr/local/src

[[email protected]]#./configure --prefix=/usr/local/php --with-mysqli=/usr/local/mysql/bin/mysql_config -enable-mbstring=all --with-xml --with-png --with-jpeg --with-zlib –enable-fpm --with-gd --with-freetype --with-libevent-dir=/usr/local/

[root@localhost php-5.3.7]# make

将编译生成的文件放在相应位置

[root@localhost php-5.3.7]# make install

为php提供配置文件

[root@localhost php-5.3.7]# cp php.ini-production /usr/local/php/etc/php.ini

为php-fpm提供配置文件

[root@localhost php-5.3.7]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@localhost php-5.3.7]# cd /usr/local/php/etc/

[root@localhost etc]# vim php-fpm.conf

启动fastcgi

[root@localhost etc]# /usr/local/php/sbin/php-fpm &

[1] 15333

[root@localhost etc]# vim /etc/nginx/fastcgi.conf

[1]+ Done                    /usr/local/php/sbin/php-fpm

[root@localhost etc]# vim /etc/nginx/fastcgi_params

编辑nginx配置文件;

[root@localhost nginx-1.0.11]# vim /etc/nginx/nginx.conf

location / {

 44             root   /usr/html;

 45             index index.php index.html index.htm;

 46         }

 47

 48         #error_page 404              /404.html;

 49

 50         # redirect server error pages to the static page /50x.html

 51         #

 52         error_page   500 502 503 504 /50x.html;

 53         location = /50x.html {

 54             root   html;

 55         }

 56

 57         # proxy the PHP scripts to Apache listening on 127.0.0.1:80

 58         #

 59         #location ~ \.php$ {

 60         #    proxy_pass   http://127.0.0.1;

 61         #}

 62

 63         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

 64         #

 65         location ~ \.php$ {

 66             root           /usr/html;

 67             fastcgi_pass   127.0.0.1:9000;

 68             fastcgi_index index.php;

 69             fastcgi_param SCRIPT_FILENAME /usr/html$fastcgi_script_name; 70             include        fastcgi_params;

 71         #}

 72

测试:

Nginx调用php

[root@localhost ~]# cd /usr/html/

[root@localhost html]# mv index.html index.php

[root@localhost html]# vim index.php

<html>

<head>

<title>Welcome to nginx!</title>

</head>

<body bgcolor="white" text="black">

<center><h1>Welcome to nginx!</h1></center>

</body>

</html>

<?php

phpinfo();

?>

测试结果如下

利用×××lnmp环境

Php调用mysql

$link=mysql_connect('127.0.0.1','root','');

if ($link)

 echo "you are connect";

else

 echo "you are not connect"

继续阅读