lnmp架构搭建


系统环境:rhel6.5                 192.168.2.108

软件包:

                nginx-1.4.2.tar.gz

                mysql-5.5.20.tar.gz

                 php-5.5.20.tar.bz2

                libiconv-1.14.tar.gz

                mcrypt-2.6.8.tar.gz

                libmcrypt-2.5.7.tar.gz

                mhash-0.9.9.9.tar.gz

一.nginx安装

1.安装基本的编译环境,依赖包

yum install -y gcc gcc-c++ make pcre-devel openssl-devel
2.tar zxf nginx-1.4.2.tar.gz

3.编译安装

cd nginx-1.4.2
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module
#--with-http_stub_status_module 可以用来启用Nginx的NginxStatus功能,以监控Nginx的当前状态

make && make install

注:在编译前,可以先做如下更改

vim nginx-1.4.2/src/core/nginx.h
define NGINX-VER   "centrald"                修改版本号,避免被攻击
vim nginx-1.4.2/auto/cc/gcc

#CFLAGS="$CFLAGS -g"    注释 debug 功能,可以减少编译占用资源
4.修改配置文件

vim /usr/local/lnmp/nginx/conf/nginx.conf
woker_processes 1;
events {
use epoll;    启用高效模式
worker_connections 1024;
}
5.创建软链接

ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin
6.启动服务

nginx
7.检查nginx 配置是否错误

nginx -t
8.访问测试页面 http://192.168.2.108 是否报错

/usr/local/lnmp/nginx/html           nginx默认发布目录


二.mysql安装

1.yum install -y  ncurses-devel bison openssl-devel zlib-devel cmake

 2.tar mysql-5.5.20.tar.gz

3.编译安装mysql

cd mysql-5.5.20

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_SSL=yes -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DMYSQL_TCP_PORT=3306

make &&make install

注:参数模块解释

#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \             #安装目录
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \                                #数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \        #Unix socket 文件路径
-DWITH_MYISAM_STORAGE_ENGINE=1 \                                            #安装 myisam 存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \                                            #安装 innodb 存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \                                            #安装 archive 存储引擎

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \                                        #安装 blackhole 存储引擎
-DWITH_PARTITION_STORAGE_ENGINE=1 \                                            #安装数据库分区

-DENABLED_LOCAL_INFILE=1 \                                                                #允许从本地导入数据
-DWITH_READLINE=1 \                                                                        #快捷键功能
-DWITH_SSL=yes \                                                                                #支持 ssl
-DDEFAULT_CHARSET=utf8 \                                                                使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \                                            #校验字符
-DEXTRA_CHARSETS=all \                                                                    #安装所有扩展字符集
-DMYSQL_TCP_PORT=3306 \                                                                #MySQL 监听端口

4.增加mysql用户,不允许其登录

useradd -M -s /sbin/nologin mysql
5.cd /usr/local/lnmp/mysql

6.chown -R mysql.mysql .
7. cd /usr/local/lnmp/mysql/support-files/

(1)将mysql的启动脚本复制到/etc/init.d/下

cp mysql.server /etc/init.d/mysql

(2 )复制mysql的配置文件,根据自己实际机器的内存大小进行选择

cp my-medium.cnf /etc/my.cnf

10.cd ..

 cd scripts/

进行数据库的初始化

./mysql_install_db --user=mysql --basedir=/usr/local/lnmp/mysql/ --datadir=/usr/local/lnmp/mysql/data/

11.添加mysql环境变量

(1)vim /root/.bash_profile

PATH=$PATH:/usr/local/lnmp/mysql/bin

(2)使环境变量生效

source /root/.bash_profile

12

cd /usr/local/lnmp/mysql/

 chown root * -R

chown -R mysql data/

13启动mysql数据库

service mysql start

三。PHP模块构建

yum install net-snmp-devel curl-devel libxml2-devel libpng-devel libjpeg-devel freetype-devel gmp-devel openldap-devel -y                            #软件包的依赖性
1.编译安装libiconv   加强系统对支持字符编码转换的功能
  cd libiconv-1.14

./configure --prefix=/usr/local/lnmp/modules/libiconv/
 make &&make install

2.

(1)tar zxf libmcrypt-2.5.7.tar.gz

(2)cd libmcrypt-2.5.7

./configure --prefix=/usr/local/lnmp/modules/libmcrypt/

make && make install
(3)cd libltdl/

 ./configure --libdir=/usr/local/lnmp/modules/libmcrypt/ --enable-ltdl-install

make && make install

3.mhash 是 php 加密算法扩展库

(1)tar zxf mhash-0.9.9.9.tar.gz

(2)cd mhash-0.9.9.9
./configure --prefix=/usr/local/lnmp/modules/mhash/
 make && make install
4.扩展库安装完成后建立软链接
ln -s /usr/local/lnmp/modules/libmcrypt/include/* /usr/local/
ln -s /usr/local/lnmp/modules/libmcrypt/lib/* /usr/local/lib/
ln -s /usr/local/lnmp/modules/mhash/lib/* /usr/local/lib /
ln -s /usr/local/lnmp/modules/mhash/include/* /usr/local/include/ (这里可能会
有一些问题,可以进入目录一个一个创建软连接)
5.让系统内核去加载动态库
(1)vi /etc/ld.so.conf
/usr/local/lnmp/modules/limbcrypt/lib/
/usr/local/lnmp/modules/mhash/lib/
/usr/local/lib
(2)ldconfig /usr/local/lnmp/modules
重新加载模块(这一步很重要哦!!)

6.

(1)tar zxf mcrypt-2.6.8.tar.gz

(2)编译安装

cd mcrypt-2.6.8

./configure --prefix=/usr/local/lnmp/modules/mcrypt/ --with-libmcrypt-prefix=/usr/local/lnmp/modules/libmcrypt/ --with-libmhash-prefix=/usr/local/lnmp/modules/mhash/

 make &&make install


四.php的搭建

1安装基本的依赖库
yum install net-snmp-devel curl-devel libxml2-devel libpng-devel libjpeg-devel freetype-devel gmp-devel

(2)添加nginx用户

useradd -M  -s /sbin/nologin nginx
(3)

vi /etc/ld.so.conf
/usr/local/lnmp/modules/libiconv/lib/(保留之前添加的)
#ldconfig /usr/local/lnmp/modules/libiconv/lib/

(4)编译安装php

tar jxf php-5.5.20.tar.bz2
 cd php-5.5.20

--with-config-file-path=/usr/local/lnmp/etc
--with-mysql=/usr/local/lnmp/mysql/
--with-openssl
--with-snmp
--with-gd
--with-zlib --with-curl
--with-libxml-dir
--with-png-dir
--with-jpeg-dir
--with-freetype-dir
--with-pear
--with-gettext
--with-gmp
--enable-inline-optimization
--enable-soap
--enable-ftp
--enable-sockets
--enable-mbstring
--with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config
--enable-fpm
--with-fpm-user=nginx
--with-fpm-group=nginx
--with-mhash=/usr/local/lnmp/modules/mhash/
--with-mcrypt=/usr/local/lnmp/modules/libmcrypt

./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/etc --with-mysql=/usr/local/lnmp/mysql/ --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-pear --with-gettext --with-gmp --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mhash=/usr/local/lnmp/modules/mhash/ --with-mcrypt=/usr/local/lnmp/modules/libmcrypt

make && make install

如果make出错,添加如下这一句

make ZEND_EXTRA_LIBS='-liconv'

2
[root@desktop108 php-5.5.20]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
3
root@desktop108 php-5.5.20]# cd sapi/fpm/

4从源码包中复制php配置文件

cp init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
5

cd /usr/local/lnmp/php/etc/
vi php.ini
cgi.fix_pathinfo=0    #防止 Nginx 文件类型错误解析漏洞
extension=mysql.so

#cd /usr/local/lnmp/php/etc
#cp php-fpm.conf.default php-fpm.conf

#vi php-fpm.conf #去掉以下几行的注释
pid = run/php-fpm.pid
pm.max_children = 50
pm.start_servers = 20
#在生产环境中一定要做压力测试,找到最合适的进程数组合
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500

service php-fpm start
chkconfig php-fpm on
6添加环境变量,并生效

vim /root/.bash_profile 

PATH=$PATH:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/php/bin

 source /root/.bash_profile

7修改nginx的配置文件

cd /usr/local/lnmp/nginx/conf
vim nginx.conf
user nginx;
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.conf;

}

8.检查配置文件是否出错

[root@desktop108 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful

9.

nginx -s reload

10测试php支持

(1)cd /usr/local/lnmp/nginx/html
(2)vim index.php

<?php
        phpinfo();
?>

(3)访问http://192.168.2.108/index.php就会有php信息

注:

当关闭虚拟机重新启动的时候,发现报错

[root@desktop108 ~]# nginx -s reload
nginx: [error] invalid PID number "" in "/usr/local/lnmp/nginx/logs/nginx.pid"

进到logs文件发现的确没有nginx.pid文件

解决办法:

使用nginx -c的参数指定nginx.conf文件的位置

[root@desktop108 ~]# nginx -c /usr/local/lnmp/nginx/conf/nginx.conf
[root@desktop108 ~]# nginx -s reload



你可能感兴趣的:(linux,nginx,mysql,PHP,LNMP)