Lnmp环境搭建(源码安装)
本篇lnmp环境是指Linux下搭建的nginx+mysql+php
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。Nginx不仅可以作为web服务器,也可以作为负载均衡器。
MySQL是一款开源免费的数据软件,MySQL是一个小型关系型数据库管理系统,其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库.
PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛的运用。
实验环境
Linux 5.4
nginx-1.0.11.tar.gz
mysql-5.0.87.tar.gz
php-5.4.0.tar.gz
实验步骤
一.   升级libevent
  1.解压源码包
[root@localhost ~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/src/
2.进入源码包目录进行预编译
[root@localhost ~]# cd /usr/src/libevent-2.0.16-stable/
[root@localhost libevent-2.0.16-stable]# ./configure
3.编译
[root@localhost libevent-2.0.16-stable]# make
4.安装
[root@localhost libevent-2.0.16-stable]# make install
[root@localhost local]# pwd
/usr/local
5.编辑libeven.conf文件
[root@localhost local]# vim /etc/ld.so.conf.d/libeven.conf
/usr/local/lib
6.查看升级结果
[root@localhost local]# ldconfig -v |grep libevent
         libevent_extra-2.0.so.5 -> libevent_extra.so
         libevent_openssl-2.0.so.5 -> libevent_openssl.so
         libevent-2.0.so.5 -> libevent.so
         libevent_core-2.0.so.5 -> libevent_core.so
         libevent_pthreads-2.0.so.5 -> libevent_pthreads.so
         libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
二.安装源码安装nginx
1.在 http://nginx.org/download/下载nginx源码安装包
2.安装nginx依赖包
[root@localhost ~]# yum -y install pcre-devel
3.解压源码包
[root@localhost ~]# tar -zxvf nginx-1.1.18.tar.gz -C /usr/src/
4.进入源码包目录进行预编译
[root@localhost ~]# cd /usr/src/nginx-1.1.18/
[root@localhost nginx-1.1.18]# groupadd -r nginx
[root@localhost nginx-1.1.18]# useradd -r -g nginx -s /bin/false -M nginx
[root@localhost nginx-1.1.18]# ./configure \             执行当前目录下的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 \                       安装可以查看nginx状态的程序
> --with-http_gzip_static_module \
> --http-client-body-temp-path=/var/tmp/nginx/client/ \
>--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
> --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
> --with-pcre                                          启用正规则表达式 
5.编译
[root@localhost nginx-1.1.18]# make
6.安装
[root@localhost nginx-1.0.11]# make install
[root@localhost nginx-1.0.11]# mkdir -pv /var/tmp/nginx/client
mkdir: created directory `/var/tmp/nginx'
mkdir: created directory `/var/tmp/nginx/client'
7.启动nginx
[root@localhost nginx-1.0.11]# nginx
[root@localhost nginx-1.0.11]# netstat -tupln |grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      5962/nginx: master 
8.结果测试
在浏览器中输入服务主机的IP地址,如果安装成功会显示如下所示的默认网页:

三.安装mysql数据库
2.安装依赖
[root@localhost ~]# yum -y install ncurses-devel
3.创建mysql用户(-M 不创建home目录,-s 指定shell为不登陆)
[root@localhost ~]# useradd -M -s /sbin/nologin mysql
4.解压源码包
[root@localhost ~]# tar -zxvf mysql-5.0.87.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/mysql-5.0.87/
5..进入源码包目录进行预编译
[root@localhost mysql-5.0.87]# ./configure --prefix=/usr/local/mysql \
> --without-debug \                      取消调试模式提高性能
> --with-extra-charsets=utf8,gbk \           仅仅指定需要的默认字符集提高性能
> --enable-assembler \                                             使用汇编模式提高性能
> --with-mysqld-ldflags=-all-static \                      以静态方式编译提高性能
> --with-client-ldflags=-all-static \                     
> --with-unix-socket-path=/tmp/mysql.sock \  使用Unixsocket提高性能
> --with-ssl
6.编译
[root@localhost mysql-5.0.87]# make
7.安装
[root@localhost mysql-5.0.87]# make install
8.复制配置文件
[root@localhost mysql-5.0.87]# cp support-files/my-medium.cnf /etc/my.cnf
9.复制启动脚本
[root@localhost mysql-5.0.87]# cp support-files/mysql.server /etc/init.d/mysqld
10.给启动脚本执行权限
[root@localhost mysql-5.0.87]# chmod +x /etc/init.d/mysqld
11.为可执行的二进制文件做软连接
[root@localhost mysql-5.0.87]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
12.为动态链接库做一个软连接
[root@localhost mysql-5.0.87]# ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/
13.初始化数据库
[root@localhost mysql-5.0.87]# mysql_install_db --user=mysql
14.更改安装目录属主为root,属组为mysql
[root@localhost mysql-5.0.87]# chown -R root.mysql /usr/local/mysql/
15.更改数据库目录属主和属组都为mysql
[root@localhost mysql-5.0.87]# chown -R mysql.mysql /usr/local/mysql/var/
16.启动mysql
[root@localhost mysql-5.0.87]# service mysqld start
Starting MySQL..                                           [ OK ]
[root@localhost mysql-5.0.87]# netstat -antlp |grep 3306
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      2912/mysqld 
17.测试
[root@localhost mysql-5.0.87]# mysql
Welcome to the MySQL monitor . Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.87-log Source distribution
四.Php源码安装
1.下载源码包
安装几个源码包依赖
2.安装libmcrypt源码包
[root@localhost ~]# tar -jxvf libmcrypt-2.5.8.tar.bz2 -C /usr/src
[root@localhost ~]# cd /usr/src/libmcrypt-2.5.8/
[root@localhost libmcrypt-2.5.8]# ./configure
[root@localhost libmcrypt-2.5.8]# make
[root@localhost libmcrypt-2.5.8]# make install
安装mhash源码依赖包
[root@localhost ~]# tar -jxvf mhash-0.9.9.9.tar.bz2 -C /usr/src/
[root@localhost ~]# cd /usr/src/mhash-0.9.9.9/
[root@localhost mhash-0.9.9.9]# ./configure
[root@localhost mhash-0.9.9.9]# make
[root@localhost mhash-0.9.9.9]# make install
[root@localhost mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt* /usr/lib
[root@localhost mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.* /usr/lib
安装mcrypt源码依赖包
[root@localhost ~]# tar -zxvf mcrypt-2.6.8.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/mcrypt-2.6.8/
[root@localhost mcrypt-2.6.8]# ./configure
[root@localhost mcrypt-2.6.8]# make
[root@localhost mcrypt-2.6.8]# make install
3.解压源码包
[root@localhost ~]# tar -zxvf php-5.4.0.tar.gz -C /usr/src/
4.进入源码包目录进行预编译
[root@localhost ~]# cd /usr/src/php-5.4.0/
[root@localhost php-5.4.0]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-zlib --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --with-cur1 --with-curlwrappers --enable-fpm --enable-fastcgi --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soap
5.编译
[root@localhost php-5.4.0]# make
6.安装
[root@localhost php-5.4.0]# make install
到这里整个LAMP已经安装完成,
五.下面我们就配置php和nginx能运行php网站
1.为php创建配置文件
[root@localhost php-5.4.0]# cp php.ini-production /usr/local/php/php.ini
[root@localhost php-5.4.0]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-5.4.0]# ln -s /usr/local/php/bin/php /usr/bin/
2.配置php-fpm,编译php-fpm.conf
[root@localhost php-5.4.0]# vim /usr/local/php/etc/php-fpm.conf
修改listen那行如下
143 listen = /var/run/php-fpm/php-fpm.sock
3.启动php-fpm
[root@localhost php-5.4.0]# mkdir /var/run/php-fpm
[root@localhost php-5.4.0]# /usr/local/php/sbin/php-fpm
4.配置nginx,编辑nginx配置文件
[root@localhost php-5.4.0]# vim /etc/nginx/nginx.conf
location / {
            root   html;
            index index.php index.html index.htm; 添加index.php
        }
 
        #error_page 404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
            root   html;
        }
#添加下面内容
location ~ \.php$ {
fastcgi_pass        unix:/var/run/php-fpm/php-fpm.sock;
 fastcgi_index       index.php;
 fastcgi_param SCRIPT_FILENAME
$document_root/$fastcgi_script_name;
 include fastcgi_params;
include fastcgi.conf;
}
5.重启nginx
[root@localhost html]# pkill -1 nginx
6.创建index.php
[root@localhost html]# vim index.php
 
<?php
phpinfo()
?>
7.保存后访问 http://192.168.2.100/index.php 看到如下页面表示已经安装配置成功

 
到这里我们的LAMP环境就安全搭建成功了!