源码搭建LNMP基础环境

实验环境:
redhat 企业版 5.4
已安装的软件包组
Development Libraries
Development Tools
Legacy Software Development
X Software Development
GNME Software Devleopment
KDE Software Development
所需要的源码包
cmake-2.8.7.tar.gz
libevent-2.0.18-stable.tar.gz
mysql-5.5.22.tar.gz
nginx-1.1.18.tar.gz
pcre-8.12.tar.gz
以上源码下载地址
http://nginx.org/download/nginx-1.1.18.tar.gz
http://www.cmake.org/files/v2.8/cmake-2.8.7.tar.gz
http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/mysql-5.5.22.tar.gz
http://cdnetworks-kr-1.dl.sourceforge.net/project/pcre/pcre/8.12/pcre-8.12.tar.gz
http://down1.chinaunix.net/distfiles/libevent-2.0.18-stable.tar.gz

实验准备
将以上软件包全部解压到/usr/src目录下
[root@localhost ~]tar -zxvf cmake-2.8.7.tar.gz -C /usr/src/
[root@localhost ~]tar -zxvf libevent-2.0.18-stable.tar.gz -C /usr/src/
[root@localhost ~]tar -zxvf pcre-8.12.tar.gz -C /usr/src/
[root@localhost ~]tar -zxvf mysql-5.5.22.tar.gz -C /usr/src/
[root@localhost ~]tar -zxvf nginx-1.1.18.tar.gz -C /usr/src/
[root@localhost ~]tar -zxvf php-5.4.0.tar.gz -C /usr/src/
[root@localhost ~]cd /usr/src/
[root@localhost src]# ll
总计 48
drwxr-xr-x 9 root root 4096 04-08 13:51 cmake-2.8.7
drwxr-xr-x 2 root root 4096 2009-10-01 debug
drwxr-xr-x 3 root root 4096 03-19 17:03 kernels
drwxr-xr-x 8 500 500 4096 11-19 04:21 libevent-2.0.16-stable
drwxr-xr-x 31 7161 wheel 4096 03-03 03:44 mysql-5.5.22
drwxr-xr-x 8 1001 1001 4096 12-15 22:05 nginx-1.0.18
drwxr-xr-x 7 1169 1169 4096 04-08 14:01 pcre-8.12
drwxr-xr-x 14 501 games 4096 02-29 15:37 php-5.4.0
drwxr-xr-x 7 root root 4096 03-19 17:20 redhat
安装cmake,pcre,libevent
按照默认配置安装
[root@localhost src]# cd pcre-8.12/
[root@localhost pcre-8.12]# ./configure
[root@localhost pcre-8.12]# make && make install
[root@localhost pcre-8.12]# cd ../cmake-2.8.7/
[root@localhost cmake-2.8.7]#./configure
[root@localhost cmake-2.8.7]#make && make install
[root@localhost cmake-2.8.7]# cd ../libevent-2.0.16-stable/
[root@localhost libevent-2.0.16-stable]#./configuire
[root@localhost libevent-2.0.16-stable]#make && make install
将库文件指明到系统默认库文件路径下
[root@localhost libevent-2.0.16-stable]# cd /usr/local/
[root@localhost local]# vim /etc/ld.so.conf.d/libevent.conf ---->此文件名可以随便起
        /usr/local/lib/
查看是否成功加载
[root@localhost libevent-2.0.16-stable]# ldconfig -v |grep libevent
libevent_extra-2.0.so.5 -> libevent_extra.so
libevent_core-2.0.so.5 -> libevent_core.so
libevent_pthreads-2.0.so.5 -> libevent_pthreads.so
libevent-2.0.so.5 -> libevent.so
libevent_openssl-2.0.so.5 -> libevent_openssl.so

[root@localhost libevent-2.0.16-stable]# ldconfig -v |grep pcre
libpcreposix.so.0 -> libpcreposix.so.0.0.0
libpcre.so.0 -> libpcre.so.0.0.1
libpcrecpp.so.0 -> libpcrecpp.so.0.0.0
libpcre.so.0 -> libpcre.so.0
安装nginx
创建nginx系统用户及组
[root@localhost nginx-1.1.18]# groupadd -r nginx
[root@localhost nginx-1.1.18]# useradd -r -g nginx -s /bin/false -M nginx
配置并安装nginx
[root@localhost nginx-1.1.18]# ./configure \
> --prefix=/usr/local/nginx \
> --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/lock/nginx.lock \
> --user=nginx \
> --with-http_ssl_module \
> --with-http_flv_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
> --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
> --with-pcre --group=nginx 

[root@localhost nginx-1.1.18]# make && make install
[root@localhost nginx-1.1.18]# make && make install
启动nginx
[root@localhost nginx-1.1.18]# /usr/sbin/nginx
[root@localhost nginx-1.1.18]# netstat -utpln |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18898/nginx: master
安装mysql
添加系统用户
[root@localhost mysql-5.5.22]# groupadd mysql
[root@localhost mysql-5.5.22]# useradd -r -g mysql -M mysql
配置并安装mysql
[root@localhost mysql-5.5.22]# cmake \
> -DDCMAKE_INSTALL_PREFIX=/usr/local/mysql \
>-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock
>-DDEFAULT_CHARSET=utf8 \
>-DDEFAULT_COLLATION=utf8_general_ci \
>-DWITH_EXTRA_CHARSETS=all \
>-DWITH_MYISAM_STORAGE_ENGINE=1 \
>-DWITH_INNOBASE_STORAGE_ENGINE=1 \
>-DWITH_MEMORY_STORAGE_ENGINE=1 \
>-DWITH_READLINE=1 \
>-DENABLED_LOCAL_INFILE=1 \
>-DMYSQL_DATADIR=/data/mysql \
>-DMYSQL_USER=mysql \
>-DMYSQL_TCP_PORT=3306
----------注:mysql中的CMAKE选项


[root@localhost mysql-5.5.22]# make
[root@localhost mysql-5.5.22]# make install
修改权限
[root@localhost mysql-5.5.22]# cd /usr/local/
[root@localhost local]# chmod +w mysql
[root@localhost local]# chown -R mysql:mysql mysql
连接库文件
[root@localhost local]# ln -s mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
重做配置文件并添加启动项
[root@localhost local]# cd /usr/local/mysql/support-files/
[root@localhost support-files]# cp my-medium.cnf /etc/my.cnf
注:my-large.cnf适用于1G内存左右的服务器,可以根据自己配置情况选用my-large.cnf 或 my-huge.cnf my-medium.cnf等不同配置
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
建立放置数据与日志的目录并修改权限
[root@localhost support-files]# mkdir -p /var/mysql/data/
[root@localhost support-files]# mkdir -p /var/mysql/log/
[root@localhost support-files]# chown -R mysql:mysql /var/mysql/
初始化mysql
[root@localhost support-files]# /usr/local/mysql/scripts/mysql_install_db \
>--defaults-file=/etc/my.cnf \
>--basedir=/usr/local/mysql \
>--datadir=/var/mysql/data \
>--user=mysql
将 mysql 加入开机启动
[root@localhost support-files]#chmod +x /etc/init.d/mysqld
[root@localhost support-files]#vim /etc/init.d/mysqld


[root@localhost support-files]#chkconfig --add mysqld
[root@localhost support-files]#chkconfig mysqld on
启动 mysql
service mysqld start
[root@localhost php]# netstat -utpln |grep mysql
tcp 0 0 :::3306 :::* LISTEN 15955/mysqld
安装PHP
[root@localhost php-5.4.0]# ./configure \
>--prefix=/usr/local/php \
--with-mysql=/usr/local/mysql \
--with-openssl \
--enable-fpm \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-mbstring \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-libxml-dir=/usr \
--enable-xml \
--with-config-file-path=/etc/php \
--with-config-file-scan-dir=/etc/php \
--with-bz2 --with-curl
make&&make install
为php-fpm提供配置文件:
[root@localhost php]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:
[root@localhost php]# vim /usr/local/php/etc/php-fpm.conf


启动php-fpm
[root@localhost php]# /usr/local/php/sbin/php-fpm
[root@localhost php]# ps aux | grep php-fpm

修改环境变量中的搜索路径分别加php-fpm的路径
[root@localhost php]# vim /etc/profile


[root@localhost php]# . /etc/profile
nginx与php连接
[root@localhost ~]# vim /etc/nginx/nginx.conf


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

新建index.php的测试页面
[root@localhost ~]# vim /usr/local/nginx/html/index.php
<?
phpinfo();
?>

通过浏览器访问此测试页面


测试Mysql的连接是否正常
[root@localhost ~]# vim /usr/local/nginx/html/index.php

重新启动nginx
[root@localhost ~]# pkill -1 nginx
通过浏览器访问此测试页面