lnmp :linux ,nginx ,mysql ,php

系统环境:rhel6.0    selinux and iptables disabled  (这里我们都只用源码编译的方式)

1 step:安装 mysql

yum install gcc gcc-c++ make ncurses-devel bison  openssl-devel zlib-devel -y     ###安装编译过程中的一些工具跟开发包等。

tar zxvf cmake-2.8.4.tar.gz                                  ###解压cmake 包,用于编译my sql

cd cmake-2.8.4

./confure                                                            ### 编译,安装

gmake && gmake install

tar zxvf mysql-5.5.12.tar.gz                                  ### 解压 mysql 源码包

cd mysql-5.5.12

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql   \        ###指定安装目录

-DMYSQL_DATADIR =/usr/local/ mysql   \                ###指定数据库存放目录

-DMYSQL_UNIX_ADDR=/usr/local/mysql/data/mysql.sock  \      ###Unix socket 文件路径

-DWITH_MYSAM_STORAGE_ENGINE=1 \            ###安装myisam 存储引擎

-DWITH_INNOBASE_STORAGE_ENGINE=1 \        ###安装innobase存储引擎

-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 监听端口

make && make install

重新编译时,需 要清除旧的对象文件和缓存信息

make clean

rm -f   CmakeCache.txt

useradd  -M -u 27 -s /sbin/nologin mysql     ###  "-M "表示不创建用户家目录,“-u 27 ” mysql的系统默认uid 是27 ,“-s /sbin/nologin ”用户不能登录系统。

cd /usr/local/mysql

cp  support-files/my-large.cnf  /etc/my.cnf     ###根据你的主机内存的实际情况复制mysql配置文件,具体情况可以 查看文件里的信息,都有讲解

chown -R mysql.mysql .                          ### 修改 拥有者 拥有组 “.”代表本地所有文件

scripts/mysql_install_db --user=mysql --basedir =/usr/local/mysql --datadir=/usr/local/
mysql/data                                        ### 这一步很重要,后面很多错误都来自这里,特别是路径跟用户一定要写正确

 

chown -R root .                                      ### 把本地文件拥有者改为root

chown -R mysql data                               ###把data目录的拥有者改为mysql

cp support-files/mysql.server   /etc/init.d/mysqld          ###添加服务启动项到系统中

service  mysqld start                                  ###开启mysqld 服务

chkconfig      mysqld   on                          ###  开启mysqld开机启动项

echo "export PATH= \$PATH:/usr/local/mysql/bin " >> ~/.bash_profile      ###设置mysql的环境变量

source ~/.bash_profile                ###是 环境变量生效

mysql_secure_installation            ###按照提示完成mysql 安全设置,

ln  -s /usr/local/mysql/lib   /usr/local/mysql/lib64   ###软连接 mysql的库文件,到lib64中方便

后面 php编译时找到

2 step :Php  安装

tar zxf libiconv-1.13.1.tar.gz                ### 加强系统对支持字符编码转换的功能

cd  libiconv-1.13.1/

./configure   --libdir=/usr/local/lib64

make && make install

tar jxvf   libmcrypt-2.5.8.tar.bz2        ###mcrypt mhash 是php 加密算法扩展库

cd libmcrypt-2.5.8

./configure --libdir=/usrlocal/lib64

make && mke install

cd  libltdl

./confgure --libdir=/usr/local/lib64 --enable-ltdl-install

make && mke install

tar zxvf mhash-0.9.9.9.tar .gz

cd mhash-0.9.9.9

./configure  --libdir=/usr/local/lib64

make &&make install

ldconfig /usr/local/lib64

tar  zxvf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8/

./configure  --libdir=/usr/local/lib64

make && make install        ### ./configure时可能会报这个错:/bin/rm: cannot remove `libtoolT’: No such file or   directory 直接忽略

yum install net-snmp-devel curl-devel libxml2-devel libpng-devel libjpeg-devel freetypedevel
gmp-devel openldap-devel -y                ###安装一些依赖项

useradd   lnmp                         ###创建一个可以登录系统的用户,后面要用到

tar jxvf   php-5.3.6.tar.bz2    

cd   php-5.3.6

./configure    --prefix=/usr/local/php –with-config-file-path=/usr/local/php/etc --with-mysql =/usr/local/mysql/  --with-openssl  --with-snmp  --with-gd  --with-zlib  --with-curl  --with-libxml-dir  --with-png-dir  --with-jpeg-dir  --with-freetype-dir –without-pear   --with-gettext –with-gmp  --enable-inline-optimization    --enable-soap   --enable-ftp --enable-sockets  --enable-mbstring   --with-mysqli=/usr/local/mysql/bin/mysql_config   --enablefpm   --with-fpm-user=lnmp  -- with-fpm-group=lnmp   --with-libdir=lib64    --with-ldap   --with-ldap-sasl    --withmcrypt  --with-mhash

make ZEND_EXTRA_LIBS='-liconv'

make install

wget http://pear.php.net/go-pear.phar

/usr/local/php/bin/php   go-pear.phar

cp php.ini-production    /usr/local/php/etc/php.ini

scp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

vi /usr/local/php/etc/php.ini

cgi.fix_pathinfo=0                                    ###防止Nginx文件类型错误解析漏洞

cd /usr/local/php/etc


vi php-fpm.conf.default 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

echo "export PATH=\$PATH:/usr/local/php/bin" >> ~/.bash_profile

source ~/.bash_profile

/etc/init.d/php-fpm start
 

chkconfig php-fpm on
 

3 step:     Nginx安装
yum install -y pcre-devel
tar zxvf nginx-1.0.2.tar.gz
cd nginx-1.0.2
vi auto/cc/gcc
#CFLAGS=”$CFLAGS -g”       ###注释掉这行,去掉debug模式编译,编译以后程序只有几百k

vi src/core/nginx.h
#define NGINX_VERSION "1.0.2”
#define NGINX_VER "nginx"     ###修改此行,去掉后面的“NGINX_VERSION”,为了安全,这样编译后外界无法获取程序的版本号

./configure --user=lnmp --group=lnmp --prefix=/usr/local/nginx --withhttp_stub_status_module    --with-http_ssl_module

make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/sbin

vi /usr/local/nginx/conf /nginx.conf

user lnmp  lnmp;
worker_processes 8;
       error_log logs/error.log crit;
        pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
      use epoll;
       worker_connections 65535;

}

http {
     include mime.types;
     default_type application/octet-stream;
    server_names_hash_bucket_size 128;
     client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types   text/plain application/x-javascript text/css application/xml;
gzip_vary on;

server {
listen 80;
server_name desktop5.example.com;
location / {
root html;
index index.php index.html index.htm;
}

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;
include fastcgi.conf;
}

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
location /status {
stub_status on;
access_log off;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
}
}

nginx -t #检测语法
nginx #运行nginx
nginx -s reload #重载主配置文件
nginx -s stop #关闭nginx

写一个测试页面,去试试吧

cd /usr/local/nginx/html/

vi index.php

<?php

phpinfo();

?>

 

visit  http://desktop5.example.com