lnmp(linux+nginx+mysql+php)架构的实现
系统环境: rhel6.0 x86-64 iptables and selinux off
server:192.168.0.6
rpm -qa | grep mysql
rpm -qa | grep http
rpm -qa | grep php
rpm -e mysql-libs --nodeps #通常默认已经安装了mysql-libs,此选项忽略依赖性卸载mysql-libs
---->配置软件安装环境
yum install -y gcc gcc-c++ make ncurses-devel bison openssl-devel zlib-devel cmake
---->安装mysql
tar -zxf mysql-5.5.12.tar.gz
cd mysql-5.5.12/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #安装目录
-DMYSQL_DATADIR=/usr/local/mysql/data \ #数据库存放目录
-DMYSQL_UNIX_ADDR=/usr/local/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端口监听
注:本次实现只用了红色字体的编译选项,黑色字体的根据需要选择
make
make install
rm -f CmakeCache.txt #再次编译必须删除,否则编译时报错
useradd -u 25 -M -s /sbin/nologin mysql
groupmod -g 25 mysql
chown mysql.mysql /usr/local/mysql/* -R
cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf #根据主机内存复制配置文件
[root@server5 ~]# cd /usr/loca.l/mysql/scripts/
[root@server5 ~]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@server5 ~]# chown root /usr/local/mysql -R
[root@server5 ~]# chown mysql /usr/local/mysql/data -R
[root@server5 ~]# cd /usr/local/mysql/support-files
[root@server5 support-files]# cp mysql.server /etc/init.d/mysqld
[root@server5 support-files]# /etc/init.d/mysqld start
Starting MySQL.... SUCCESS! <-----------OK!
[root@server5 ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
export PATH
~
[root@server5 ~]# source .bash_profile #使更改生效
---->安装PHP
[root@server5 ~]#cd /usr/local/mysql
[root@server5 mysql]# ln -s lib/ lib64 #避免php编译时找不到mysql的库文件
[root@server5 ~]# tar -jxf libmcrypt-2.5.8.tar.bz2
[root@server5 ~]# cd libmcrypt-2.5.8
[root@server5 libmcrypt-2.5.8]# ./configure –libdir=/usr/local/lib64
[root@server5 libmcrypt-2.5.8]# make;make install
[root@server5 libmcrypt-3.5.8]# cd libltdl
[root@server5 libltdl]# ./configure --libdir=/usr/local/mysql/lib64 --enable-ltdl-install
[root@server5 libltdl]# make;make install
[root@server5 ~]# yum install net-snmp-devel curl-devel libxml2-devel libpng-devel libjpeg-devel freetype-devel gmp-devel -y
[root@server5 ~]# useradd -M -s /sbin/nologin www
[root@server5 ~]# tar -jxf php-5.3.6.tar.bz2
[root@server5 ~]# 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 --with-pear
--with-gettext --with-gmp --with-mcrypt --enable-inline-optimization --enable-soap
--enable-ftp --enable-sockets --enable-mbstring
--with-mysqli=/usr/local/mysql/bin/mysql_config --enable-fpm --with-fpm-user=www
--with-fpm-group=www --with-libdir=lib64
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
vim /usr/local/php/etc/php.ini
cgi.fix_pathinfo=0 #防止nginx文件类型错误解析漏洞
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vim /usr/local/php/etc/php-fpm.conf
#将以下语句的注释去掉
pid = run/php-fpm.pid
error_log = log/php-fpm.log
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 15
pm.max_requests = 500
---->安装nginx
yum install pcre-devel -y
tar -zxf nginx-1.0.2.tar.gz
cd nginx-1.0.2
vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g" #注释掉这行去掉debug模式,程序编译后只有几百k
vim src/core/nginx.h
#define NGINX_VERSION "1.0.2”
#define NGINX_VER "nginx" #修改此行,去掉后面的“NGINX_VERSION”,为了安全,这样编译后外界无 法获取程序的版本号
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module
--with- http_ssl_module
make
make install
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
vim /uss/local/nginx/conf/nginx.conf
user www www;
worker_processes 8;
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_devel 2; #
gzip_types text/plain application/x-javascript text/css application/xml; #
gzip_vary on; #
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location /status {
stub_status on;
access_log off;
}
}
}
nginx -s stop| reload #nginx服务停止|重载服务
nginx -t #nginx配置文件语法检测
nginx #启起nginx服务
现在可以可以在地址栏输入192.168.0.4会看到Welcome to nginx,表示nginx启动成功
测试 观察通过不停的刷新更新请求数量的变化
在/usr/local/nginx/html创建几类定义的文件类型测试
50x.html index.html test1.jpg test.php test.txt
[root@server5 ~]# cd /usr/local/nginx/html
[root@server5 html]# curl -I
HTTP/1.1 200 OK
Server: nginx/1.0.2
Date: Sun, 15 Apr 2012 09:24:33 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.3.6
[root@server5 html]# curl -I
HTTP/1.1 200 OK
Server: nginx/1.0.2
Date: Sun, 15 Apr 2012 09:22:41 GMT
Content-Type: text/plain
Content-Length: 11
Last-Modified: Sun, 15 Apr 2012 09:22:22 GMT
Connection: keep-alive
Accept-Ranges: bytes
[root@server5 html]# curl -I
HTTP/1.1 200 OK
Server: nginx/1.0.2
Date: Sun, 15 Apr 2012 09:31:50 GMT
Content-Type: image/jpeg
Content-Length: 0
Last-Modified: Sun, 15 Apr 2012 09:25:53 GMT
Connection: keep-alive
Accept-Ranges: bytes
---->安装php功能模块扩展(可选)
wget
wget
---->安装memcache模块
Memcache是danga.com的一个开源项目,它是一个高性能的分布式的内存对象缓存系统,通过在内 存里维护一个统一的巨大的Hash表,能够用来存储各种格式的数据。可以类比于MySQL这样的服务, 而PHP扩展的Memcache实际上是连接Memcache的方式
yum install memcached -y
/etc/init.d/memcached start
netstat -antlp | grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 5913/memcached
tcp 0 0 :::11211 :::* LISTEN 5913/memcached
vim .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/php/bin
export PATH
source .bash_profile
[root@server5 ~]# tar -zxf memcache-2.2.5.tgz
[root@server5 ~]# cd memcache-2.2.5
[root@server5 memcache-2.2.5]# phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@server5 memcache-2.2.5]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@server5 memcache-2.2.5]# make && make install
[root@server5 memcache-2.2.5]# cp memcache.php /usr/local/nginx/html
[root@server5 ~]# vim /usr/local/php/etc/php.ini
....
; Dynamic Extensions ;
....
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/memcache.so #激活此选项
[root@server5 ~]# /etc/init.d/php-fpm reload
[root@server5 ~]# vim /usr/local/nginx/html/memcache.php
define('ADMIN_USERNAME','memcache'); // Admin Username 采用默认值
define('ADMIN_PASSWORD','password'); // Admin Password 采用默认值
$MEMCACHE_SERVERS[] = '192.168.0.5:11211'; // 修改此行
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; #注释此行
[root@server5 ~]# nginx -s reload #重载nginx
url= # 用默认用户名密码登录
admin_username:memcache
admin_password:password
---->安装eaccelerator功能模块
tar -jxf eaccelerator-0.9.6.tar.bz2
cd eaccelerator-0.9.6
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
mkdir /var/cache/eaccelerator
vim /usr/local/php/etc/php.ini
extension="eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/var/cache/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.log_file = "/var/log/httpd/eaccelerator_log"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.allowed_admin_path = "/usr/local/nginx/html"
cp eaccelerator-0.9.6/control.php /usr/local/nginx/html #此文件包含登录用户认证信息
lnmp的实现过程到此为止,GOOD LUCK!