一:安装前的准备工作 (1)源码安装就是指从官方网上下载的最新版本的源代码, LNMP是指linux系统中nginx+mysql+php的结合,而LAMP是在linux系统中apache+mysql+php的结合,apache使用的机制是select,事件是轮询,所以效率比较低。LNMP中的nginx可以实现反向代理加速,占用的内存空间也比较少,它使用的机制是epoll机制,事件是触发更新,所以效率比较高。今天我们就使用最新的源代码来搭建LNMP,linux的操作系统我使用的是5.4版本,nginx是nginx-1.0.11压缩文件,mysql使用的是mysql-5.5.15压缩文件,它是绿色软件包,里面存放的不是源代码而是二进制文件,php使用的是php-5.4.13压缩文件,在LAMP中php的工作是调用apache的模块,而在LNMP中php是通过使用fastcgi与nginx进行工作,fastcgi可以启动n多个子进程,所以效率会提高,而管理fastcgi的是fpm,在php5.3之前是通过打补丁来管理的,在php5.3之后就支持fmp了。然后我们从相关的官方网上下载这些源代码。 (2)在安装nginx之前,首先安装pcre和libevent PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式库.这些在执行正规表达式模式匹配时用,与Perl 5同样的语法和语义是很有用的。 Boost太庞大了,使用boost regex后,程序的编译速度明显变慢。测试了一下,同样一个程序,使用boost::regex编译时需要3秒,而使用pcre不到1秒。因此改用pcre来解决C语言中使用正则表达式的问题。安装pcre在Linux5.4中存在该软件包并且已经安装,但是我们还要安装pcre-devel这个软件包里存放很多的库文件,所以先用rpm安装pcre-delve。 libevent是一个强大的跨平台的事件通知库,如果不想被多线程困扰,可以考虑这个平台,它从1.2.* 版本开始支持轻量级的http server 开发支持,随后陆续还推出轻量级 DNS server、RPC server 开发支持,这组事件API提供了一种当某个指定文件描述符有效或时间到达时执行某个函数的机制,在使用事件API前必须使用event_init()初始化。在Linux5.4中已经安装了libevent,但是版本比较低,所以我们从libevent的官网libevent.org 最新版本是2.0.16,下载该压缩文件。 (3)对于nginx,mysql,php的安装顺序其实没有太多要求,但是php要与nginx进行结合,还要调用mysql,所以我们把php放在最后安装,我们先安装mysql,在安装nginx,最后安装php 二:安装步骤 (1)安装mysql,它是二进制文件 把下载的压缩包传到root家目录 [iyunv@localhost ~]# ll total 171460 -rw------- 1 root root 1053 Apr 23 20:31 anaconda-ks.cfg drwxr-xr-x 2 root root 4096 Apr 25 19:07 Desktop -rw-r--r-- 1 root root 27974 Apr 23 20:30 install.log -rw-r--r-- 1 root root 4389 Apr 23 20:27 install.log.syslog -rwxrw-rw- 1 root root 837650 Apr 3 2012 libevent-2.0.16-stable.tar.gz -rwxrw-rw- 1 root root 162247449 Jul 15 2011 mysql-5.5.15-linux2.6-i686.tar.gz -rwxrw-rw- 1 root root 691501 Apr 3 2012 nginx-1.0.11.tar.gz -rwxrw-rw- 1 root root 11545777 Mar 15 16:01 php-5.4.13.tar.bz2 拆包到/usr/local目录下,一般的源代码都要解压到/usr/local/src目录下,但我们使用的是绿色软件包(二进制文件)所以拆包到/usr/local下 [iyunv@localhost ~]# tar -zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local [iyunv@localhost ~]# cd /usr/local [iyunv@localhost local]# ll drwxr-xr-x 13 root root 4096 Apr 25 10:08 mysql-5.5.15-linux2.6-i686 解压后的文件名太长不容易写,所以要更改文件名或者建立软连接,在这使用软连接 [iyunv@localhost local]# ln -s mysql-5.5.15-linux2.6-i686 mysql [iyunv@localhost local]# ll lrwxrwxrwx 1 root root 26 Apr 25 10:08 mysql -> mysql-5.5.15-linux2.6-i686 drwxr-xr-x 13 root root 4096 Apr 25 10:08 mysql-5.5.15-linux2.6-i686 [iyunv@localhost local]# cd mysql #切换到mysql目录查看文件,其中有一个安装二进制文件(INSTALL-BINARY) [iyunv@localhost mysql]# less INSTALL-BINARY #查看该文件 存放的是mysql的安装步骤 To install and use a MySQL binary distribution, the basic command sequence looks like this: 1.shell> groupadd mysql #建立组名 2.shell> useradd -r -g mysql mysql #建立帐号为系统帐号,加入到mysql组中 3.shell> cd /usr/local #切换到/usr/local 4.shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz #把mysql压缩文件解压 5.shell> ln -s full-path-to-mysql-VERSION-OS mysql #创建软连接 6.shell> cd mysql #切换到mysql目录 7.shell> chown -R mysql . #在mysql目录下所以对象改变所有者,-R表示递归 8.shell> chgrp -R mysql . #在mysql目录下所以对象改变所有组,-R表示递归 9.shell> scripts/mysql_install_db --user=mysql #产生初始化脚本 10.shell> chown -R root . #在mysql目录下把所有者还该为管理员 11.shell> chown -R mysql data #在mysql目录下只有data所有者为mysql # Next command is optional 12.shell> cp support-files/my-medium.cnf /etc/my.cnf #mysql的配置文件 13.shell> bin/mysqld_safe --user=mysql & # Next command is optional 14.shell> cp support-files/mysql.server /etc/init.d/mysql.server #mysql的控制脚本 在mysql的安装步骤中3,4,5我们在上面已经做过了,我们只做剩下的 [iyunv@localhost ~]# groupadd -r mysql #创建组为系统组 [iyunv@localhost ~]# useradd -r -g mysql mysql -M #创建系统帐号加入系统组中,-M表示不创建家目录 [iyunv@localhost ~]# cd /usr/local/mysql [iyunv@localhost mysql]# chown -R mysql . #在mysql目录下所以对象改变所有者权限,-R表示递归 [iyunv@localhost mysql]# chgrp -R mysql . #在mysql目录下所以对象改变所有组权限,-R表示递归 [iyunv@localhost mysql]# scripts/mysql_install_db --user=mysql #产生初始化脚本 Installing MySQL system tables... OK Filling help tables... OK [iyunv@localhost mysql]# chown -R root . #在mysql目录下把所有者还该为管理员 [iyunv@localhost mysql]# chown -R mysql data #在mysql目录下只有data所有者为mysql [iyunv@localhost mysql]# cp support-files/my-medium.cnf /etc/my.cnf #mysql的配置文件 [iyunv@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld #mysql的控制脚本 [iyunv@localhost mysql]# chmod a+x /etc/init.d/mysqld #改变控制脚本的权限可执行 [iyunv@localhost mysql]# chkconfig --add mysqld #添加mysql到chkconfig中 [iyunv@localhost mysql]# chkconfig --list |grep mysql #开机能够自动启动 mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off [iyunv@localhost mysql]# service mysqld start Starting MySQL........ [确定] [iyunv@localhost mysql]# netstat -tupln |grep mysql #mysql端口为3306 tcp 0 0 :::3306 :::* LISTEN 6078/mysqld [iyunv@localhost mysql]# mysql #进入mysql数据库时出错,没有mysql命令 bash: mysql: command not found [iyunv@localhost mysql]# cd bin #切换到bin下 [iyunv@localhost bin]# ll [iyunv@localhost bin]# ./mysql #执行./mysql能进入数据库 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.15-log MySQL Community Server (GPL) 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> show databases; #查看数据库 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> \q #退出数据库 Bye 我们要把它加入到搜索路径中去 [iyunv@localhost bin]# vim /etc/profile #环境变量 fi PATH=$PATH:/usr/local/mysql/bin #加入该命令在export之前 export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC [iyunv@localhost bin]# . /etc/profile #执行一次 [iyunv@localhost bin]# mysql #登陆直接输入mysql Welcome to the MySQL monitor. Commands end with ; or \g. mysql> \q Bye 接下来我们给mysql数据库设置一个管理员帐号和密码 [iyunv@localhost ~]# mysqladmin -u root -p password '123' #-u表示帐号-p表示密码 Enter password: [iyunv@localhost ~]# mysql -u root –p #输入帐号和密码登录数据库 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. mysql> \q Bye 在mysql目录下有头文件(include)和库文件(lib),我们要把这些文件导入到环境中去这些文件被调用 [iyunv@localhost ~]# cd /usr/local/mysql [iyunv@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf #在ld.so.conf.d目录下随便创建一个文件只要是.conf结尾的, /usr/local/mysql/lib #输入库文件的存放路径 [iyunv@localhost mysql]# ldconfig #ldconfig 刷新缓存文件 [iyunv@localhost mysql]# ldconfig -pv |grep mysql #查看 libtcmalloc_minimal.so.0 (libc6) => /usr/local/mysql/lib/libtcmalloc_minimal.so.0 libmysqlclient.so.18 (libc6) => /usr/local/mysql/lib/libmysqlclient.so.18 libmysqlclient.so (libc6) => /usr/local/mysql/lib/libmysqlclient.so [iyunv@localhost mysql]# ln -s include /usr/include/mysql #给头文件创建一个软连接mysql放在/usr/include下 Mysql安装完毕 (2)安装nginx A. 首先我们先安装pcre和libevent 在linux系统中已经安装了pcre,我们直接安装pcre-devel软件包 [iyunv@localhost ~]# mkdir /mnt/cdrom #建立光盘挂载点 [iyunv@localhost ~]# mount /dev/cdrom /mnt/cdrom #挂载光盘到光盘挂载点 mount: block device /dev/cdrom is write-protected, mounting read-only [iyunv@localhost ~]# cd /mnt/cdrom/Server [iyunv@localhost Server]# rpm -qa |grep pcre #查看pcre已经安装过 pcre-6.6-2.el5_1.7 [iyunv@localhost Server]# rpm -ivh pcre-devel-6.6-2.el5_1.7.i386.rpm #安装pcre-devel Preparing... ########################################### [100%] 1:pcre-devel ########################################### [100%] 然后安装libevent压缩包,压缩包已经传入到root目录下,安装源代码的步骤:配置,编译,安装 [iyunv@localhost ~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/local/src #拆包到存放源代码的位置/usr/local/src目录下 [iyunv@localhost ~]# cd /usr/local/src/libevent-2.0.16-stable/ #切换到源代码 [root@localhostlibevent-2.0.16-stable]# ./configure --prefix=/usr/local/libevent #进行安装,为了方便以后删除,我们把它放到一个目录下 [iyunv@localhost libevent-2.0.16-stable]# make #make之后直接make install [iyunv@localhost libevent-2.0.16-stable]# make install [iyunv@localhost libevent-2.0.16-stable]# cd /usr/local/libevent #在libevent目录下存放的有头文件和库文件,我们要导入到环境中能被调用 [iyunv@localhost libevent]# ll total 12 drwxr-xr-x 2 root root 4096 Apr 25 13:56 bin drwxr-xr-x 3 root root 4096 Apr 25 13:56 include drwxr-xr-x 3 root root 4096 Apr 25 13:56 lib [iyunv@localhost libevent]# vim /etc/ld.so.conf.d/libevent.conf #创建一个libevent.conf的文件 /usr/local/libevent/lib #输入库文件的存放位置 [iyunv@localhost libevent]# ldconfig #刷新缓存文件 [iyunv@localhost libevent]# ldconfig -pv |grep libevent #查看 libevent_pthreads-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_pthreads-2.0.so.5 libevent_openssl-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_openssl-2.0.so.5 libevent_extra-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_extra-2.0.so.5 libevent_core-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent_core-2.0.so.5 libevent-2.0.so.5 (libc6) => /usr/local/libevent/lib/libevent-2.0.so.5 libevent-1.1a.so.1 (libc6) => /usr/lib/libevent-1.1a.so.1 [iyunv@localhost libevent]# ln -s include /usr/include/libevent #对于头文件我们创建软连接到/usr/include的目录下创建libevent B.安装nginx [iyunv@localhost ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src #在nginx运行者身份 [iyunv@localhost ~]# groupadd -r nginx #建立组 [iyunv@localhost ~]# useradd -r -g nginx nginx –M #帐号加入到组中 [iyunv@localhost ~]# cd /usr/local/src/nginx-1.0.11/ [iyunv@localhost nginx-1.0.11]# ./configure \ #后面跟的参数,\表示续行符 > --conf-path=/etc/nginx/nginx.conf \ #指明配置文件路径 > --error-log-path=/var/log/nginx/error.log \ #出错日志存放的位置 > --http-log-path=/var/log/nginx/access.log \ #应用http访问时成功的日志 > --pid-path=/var/run/nginx/nginx.pid \ #表示进程号存放的位置 > --lock-path=/var/lock/nginx.lock \ #表示所文件的存放位置 > --user=nginx \ #运行者 > --group=nginx \ #运行者组 > --with-http_ssl_module \ #支持ssl加密,数字证书 > --with-http_flv_module \ #支持flv流媒体模式 > --with-http_stub_status_module \ #本地的信息输出 > --with-http_gzip_static_module \ #支持压缩和解压缩 > --http-client-body-temp-path=/var/tmp/nginx/client/ \ #表示客户端进行访问时临时存放的目录,我们需要创建该client目录 > --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ #反向代理加速的临时目录 > --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ #表示fastcgi的临时目录 > --with-pcre #安装pcre-devel的存放目录 [iyunv@localhost nginx-1.0.11]#make [iyunv@localhost nginx-1.0.11]#make install 编辑Nginx的控制脚本程序 [iyunv@localhost ~]# vim /etc/init.d/nginx #在inid.d目录下编辑nginx文件 #!/bin/sh #set -x NGINXD='/usr/local/nginx/sbin/nginx' #description: nginx web server #chkconfig: 2345 88 60 if [ -f /etc/init.d/functions ];then . /etc/init.d/functions fi start(){ [ -f /var/lock/subsys/nginx ]&& echo "nginx is started" && exit echo -n "starting nginx......" sleep 1 $NGINXD && RETVAL=0 ||RETVAL=1 [ $RETVAL -eq 0 ]&& touch /var/lock/subsys/nginx && echo "ok" || echo "fail" } stop(){ [ ! -f /var/lock/subsys/nginx ] && echo "nginx is stoped..." && exit echo -n "stoping nginx........" sleep 1 killproc nginx && RETVAL=0 ||RETVAL=1 [ $RETVAL -eq 0 ] && rm -rf /var/lock/subsys/nginx && echo "ok" || echo "fail" } case $1 in start) start ;; stop) stop ;; restart) stop start ;; *) echo "start|stop|restart" ;; esac [iyunv@localhost ~]# chmod a+x /etc/init.d/nginx #改变权限可执行 [iyunv@localhost ~]# service nginx start #启动nginx服务 starting nginx......ok [iyunv@localhost ~]# netstat -tupln |grep 80 #查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5595/nginx [iyunv@localhost ~]# cd /usr/local/nginx/html #切换到站点目录下 [iyunv@localhost html]# vim index.html #查看网页内容 <html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx!</h1></center> </body> </html> 安装nginx成功 Nginx也是一个服务器,nginx能够处理php,我们要修改nginx的配置文件 [iyunv@localhost ~]# vim /etc/nginx/nginx.conf location / { root html; index index.php index.html index.htm; } 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; } [iyunv@localhost ~]# cd /usr/local/nginx/html #修改网页内容 [iyunv@localhost html]# ll total 8 -rw-r--r-- 1 root root 383 Apr 25 16:33 50x.html -rw-r--r-- 1 root root 151 Apr 25 19:32 index.html [iyunv@localhost html]# mv index.html index.php [iyunv@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(); ?> 与mysql结合,编写网页 <html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center><h1>Welcome to nginx!</h1></center> </body> </html> <?php $link=mysql_connect('127.0.0.1','root','123'); if($link) echo "ok"; else echo "not"; ?> 重启服务 (3)安装php源代码 [iyunv@localhost ~]# tar -jxvf php-5.4.13.tar.bz2 -C /usr/local/src [iyunv@localhost ~]# cd /usr/local/src/php-5.4.13/ [iyunv@localhost php-5.4.13]#./configure \ > --prefix=/usr/local/php \ #安装目录 > --enable-fpm \ #支持fastcgi > --enable-sockets \ #服务 > --with-mysql=/usr/local/mysql \ #支持mysql > --with-mysqli=/usr/local/mysql/bin/mysql_config \ #mysql的接口文件 > --enable-mbstring \ #多种字符串 > --enable-xml \ #字符格式 > --with-png-dir \ #图像和图形相关的 > --with-png \ > --with-jpeg-dir \ > --with-zlib \ #支持压缩 > --with-freetype-dir \ > --with-config-file-path=/etc/php \ #放置php的配置文件的 > --with-config-file-scan-dir=/etc/php5.d #额外功能的控制文件 [iyunv@localhost php-5.4.13]#make [iyunv@localhost php-5.4.13]#make install 1.Php的配置文件,进入源码目录 [iyunv@localhost ~]# cd /usr/local/src/php-5.4.13/ [iyunv@localhost php-5.4.13]# mkdir /etc/php /etc/php5.d [iyunv@localhost php-5.4.13]# cp php.ini-production /etc/php/php.ini 2.php-fpm的控制脚本,fpm也是一个服务器来管理fastcgi [iyunv@localhost php-5.4.13]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 改变权限可执行 [iyunv@localhost php-5.4.13]# chmod a+x /etc/init.d/php-fpm 3.产生php-fpm的配置文件,进入php的安装目录下的etc [iyunv@localhost php-5.4.13]# cd /usr/local/php/etc/ You have new mail in /var/spool/mail/root [iyunv@localhost etc]# ll total 28 -rw-r--r-- 1 root root 1152 Apr 25 20:01 pear.conf -rw-r--r-- 1 root root 21590 Apr 25 20:01 php-fpm.conf.default #配置文件样例 [iyunv@localhost etc]# cp php-fpm.conf.default php-fpm.conf [iyunv@localhost ~]# service php-fpm start #开启服务 Starting php-fpm done [iyunv@localhost ~]# netstat -tupln |grep php-fpm #查看端口 tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 7041/php-fpm.conf) [iyunv@localhost ~]# ps aux |grep fpm #查看fpm进程 root 7041 0.0 0.4 30428 2228 ? Ss 10:06 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf) nobody 7042 0.0 0.3 30428 2060 ? S 10:06 0:00 php-fpm: pool www nobody 7043 0.0 0.3 30428 2060 ? S 10:06 0:00 php-fpm: pool www root 7711 0.0 0.1 4788 660 pts/1 R+ 10:44 0:00 grep fpm
|