您的位置:首页 > 运维架构 > 网站架构

源码安装LNMP 架构

2012-09-17 20:52 267 查看

源码安装mysql
新安装的linux 6.3
/etc/init.d/postfix stop 停止postfix
卸载mysql rpm -e mysql-libs --nodeps #需要将postfix 服务关闭,要不然就启动不了。
rpm -qa | grep php
rpm -qa | grep apache
下载最新版本的mysql 5.5
tar zxf cmake-2.8.4.tar.gz
cd cmake-.2.8.4
./configure 等一会,时间有点长
gmake && gmake install

tar zxf mysql-..
cd mysql
yum install cmake -y
yum install gcc gcc-c++(没安装会报错:

CMake Error at /usr/share/cmake/Modules/CMakeCXXInformation.cmake:17 (GET_FILENAME_COMPONENT):
get_filename_component called with incorrect number of arguments

) make ncurses-devel bison(必须安装,否则报

Warning: Bison executable not found in PATH
-- Configuring incomplete, errors occurred!


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 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all
如果cmake 的过程中出错,则需删除 CMakeCache.txt 缓存文件

cmake完后,会警告Googletest没有安装,这个不用管;
make & make install 编译时间也会很长
重新编译时,需要清除旧的对象文件和缓存信息
make clean
rm -f CMakeCache.txt
cd /usr/local/lnmp/mysql/support-file/
cp my-large.cnf /etc/my.cnf #根据你的主机内存复制mysql配置文件
cp mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
cd /usr/local/lnmp/
useradd -u 27 -M mysql #-M表示不创建家目录
chown mysql.mysql mysql/ -R
cd /usr/local/lnmp/mysql/scripts/
./mysql_install_db --user=mysql --basedir=/usr/local/lnmp/mysql/ --datadir=/usr/local/lnmp/mysql/data/ 初始化数据库
ll /usr/local/lnmp/myslq/data 此时就有一些初始化文件生成drwxr-xr-x. 2 mysql mysql 4096 Jul 12 12:13 mysql
-rw-rw----. 1 mysql mysql 27813 Jul 12 12:13 mysql-bin.000001
-rw-rw----. 1 mysql mysql 995349 Jul 12 12:13 mysql-bin.000002
-rw-rw----. 1 mysql mysql 38 Jul 12 12:13 mysql-bin.index
drwx------. 2 mysql mysql 4096 Jul 12 12:13 performance_schema
drwxr-xr-x. 2 mysql mysql 4096 Jul 12 12:10 test

cd /usr/local/lnmp/mysql
chown root . -R
chown mysql data -R
/etc/init.d/mysqld start
此时会显示
Starting MySQL..... SUCCESS!
如果mysql不能正常启动,比如pid文件不存在,先把mysql相关目录文件删除,再重新编译
ll /usr/local/lnmp/mysql/data
会有这个文件生成
srwxrwxrwx. 1 mysql mysql 0 Jul 7 08:20 mysql.sock
chkconfig --level 3 mysqld on
cd
vi .bash_profile
在PATH后面添加成下面的样子:
PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
source .bash_profile
echo $PATH
ln -s /usr/local/lnmp/mysql/lib /usr/local/lnmp/mysql/lib64
#不然php编译的时候(./configure)找不到mysql的库文件

mysql_secure_installation #按提示完成mysql安全设置,生产环境推荐使用
mysql -pwestos

yum install gperf readline-devel time zlib-devel -y
rpmbuild -tb mysql-5.5.12.tar.gz 将tar 包 编译成rpm 包


使nginx支持php(php-fpm):nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回给nginx。PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多有点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm(也可以带上--enable-fastcgi )参数即可开启PHP-FPM。

cd /usr/local/phpcp etc/php-fpm.conf.default etc/php-fpm.conf修改vi etc/php-fpm.conf.default etc/php-fpm.conf修改user及group的值。
修改nginx配置文件以支持php-fpm:

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000; #fastcgi默认端口为9000
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#注意将原先的/scripts$fastcgi_script_name 修改为 $document_root$fastcgi_script_name;否则会报错:
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
include fastcgi_params;
}
在/usr/local/nginx/html下创建index.php文件:

<?php
phpinfo();
?>
启动php-fpm:

/usr/local/php/sbin/php-fpm (手动打补丁的启动方式/usr/local/php/sbin/php-fpm start)
启动nginx:
/usr/local/nginx/sbin/nginx


安装nginx 处理php网页时,需安装php 和fastcgi。
nginx 处理jsp网页时,需安装jdk(java)和tomcat,由tomcat处理jsp网页。
然后由nginx 同时支持 静态网页,php网页,jsp网页:
vim /usr/local/lnmp/nginx/conf/nginx.conf

location / {
root html; #nginx处理静态网页
index index.html index.htm;
}

location ~ \.php$ {
root html; #将php网页交给9000端口的程序处理
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf; 如果不修改此处,则php网页打不开
}

location ~ \.jsp$ {
proxy_pass http://127.0.0.1:8080; #将jsp网页交给8080端口的程序处理,tomcat 处理jsp网页的默认端口是8080,并且client访问时不用在IP后面添加端口号。
}
curl -I taobao.com 查看server所使用的类型
HTTP/1.1 302 Found
Server: Tengine
Date: Sat, 07 Jul 2012 09:36:25 GMT
Content-Type: text/html
Content-Length: 260
Connection: keep-alive
Location: http://www.taobao.com/ Expires: Sun, 08 Jul 2012 09:36:25 GMT
Cache-Control: max-age=86400

源码安装nginx
yum install pcre-devel openssl-devel -y 支持http的rewrite module,如果没有安装,会报 error: the HTTP rewrite module requires the PCRE library。

下载最新稳定版的nginx

useradd -s /sbin/nologin -M nginx # -M 表示不创建nginx用户家目录

tar zxf nginx-1.2.1.tar.gz
cd nginx-1.2.1
vi auto/cc/gcc 关闭debug
# debug
#CFLAGS="$CFLAGS -g" 注释掉这行,去掉 debug 模式编译,编译以后程序只有几百 k
vi src/core/nginx.h
改成这样:#define NGINX_VER "nginx" 修改此行,去掉后面的“NGINX_VERSION”,为了安全,这样编译后外界无法获取程序的版本号
yum install openssl-devel -y #不安装编译时会报错的
./configure --user=nginx --group=nginx --prefix=/usr/local/lnmp/nginx --with-http_stub_status_module --with-http_ssl_module
make & make install
du -sh /usr/local/lnmp/nginx 如果不关闭debug的话,则生成4.4M的目录

vi /usr/local/lnmp/nginx/conf/nginx.conf
user nginx;
worker_processes 2;
events {
use epoll;
worker_connections 1024;

}
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

location /status {
stub_status on;
access_log off;

}
添加上面3行,当访问192.168.0.55/status 时,会显示如下信息:
Active connections: 1
server accepts handled requests
16 16 168
Reading: 0 Writing: 1 Waiting: 0

}
vi ~/.bash_profile
在PATH后添加 /usr/local/lnmp/nginx/sbin
source ~/.bash_profile
echo $PATH
nginx -t 检测nginx的语法
nginx 启动nginx
ps aux 可以看到总共有3个nginx进程
curl -I localhost
在浏览器中输入 http://192.168.0.55/ nginx -s stop 关闭nginx

nginx 的https功能

编辑nginx的配置文件

vim /usr/local/lnmp/nginx/conf/nginx.conf

server {

listen 443;

server_name localhost;

ssl on;

ssl_certificate cert.pem;

ssl_certificate_key cert.pem;

ssl_session_timeout 5m;

ssl_protocols SSLv2 SSLv3 TLSv1;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

location / {

root html;

index index.html index.htm index.php;

}

}

location / {

root html;

index index.html index.htm index.php;

}

location ~ \.php$ {

root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi.conf;

}

nginx -t

nginx: [emerg] SSL_CTX_use_certificate_chain_file("/usr/local/lnmp/nginx/conf/cert.pem") failed (SSL: error:02001002:system library:fopen:No such file or directory error:20074002:BIO routines:FILE_CTRL:system lib error:140DC002:SSL routines:SSL_CTX_use_certificate_chain_file:system lib)

nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test failed

cd /etc/pki/tls/certs/

make cert.pem

mv cert.pem /usr/local/lnmp/nginx/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

nginx -s reload

netstat -anltp | grep :443

此时打开浏览器输入 https://desktop55/phpadmin/index.php 点击获取证书并添加

nginx的虚拟主机
http://wiki.nginx.org/ 此网站有nginx的详细配置

编辑配置文件

vim /usr/local/lnmp/nginx/conf/nginx.conf

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

将main日志功能开启

server {

listen 80;

server_name www.domain1.com;

access_log logs/domain1.access.log main;

location / {

index index.html;

root virtualhosts/domain1.com/;

}

}

cd /usr/local/lnmp/nginx/

mkdir virtualhosts

mkdir domain1.com

mkdir domain2.com

echo www.domain1.com > domain1.com/index.html

echo www.domain2.com > domain2.com/index.html

此时宿主主机 vi /etc/hosts

192.168.0.55 www.domain1.com

192.168.0.55 www.domain2.com

nginx -t

此时在宿主主机的浏览器 http://www.domain1.com http://www.domain2.com
nginx的负载均衡

编辑配置文件

vim /usr/local/lnmp/nginx/conf/nginx.conf

http {

upstream westos {

server 192.168.0.77:80 weight=1 max_fails=3 fail_timeout=60s;

server 192.168.0.57:80 weight=1 max_fails=3 fail_timeout=60s;

#weight 为服务器的权重,默认是1,权重越大,被访问的概率就越大;如果某台服务器在fail_timeout时间内出现了max_fail 次连接失败,则nginx会认为那个服务器已经挂了。

}

server {

listen 80;

server_name www.westos.org;

location / {

index index.html;

proxy_pass http://westos;
}

}

nginx -t

nginx -s reload

开启另外两太主机的apache,并在宿主机上 vim /etc/hosts

192.168.0.55 www.westos.org (前端机)

打开宿主机的浏览器 www.westos.org 然后刷新,再停止后端机的apache

apache 与 nginx 的压力测试:

ab redhat自带的压力测试工具

webbench 需下载的压力测试工具

ab -c 1000 (发1000次) -n 1000 (并发1000次) http://192.168.0.57/index.html 测试apache的

ab -c 1000 -n 1000 http://192.168.0.55/index.html 测试nginx的

没有优化时,nginx是apache的3到4倍,优化后大概为10倍

下载webbench后,解压

yum install ctags -y

mkdir -p /usr/local/man/man1 (也可不用创建)

cd webbench-1.5

make && make install

ll /usr/local/bin/webbench

webbench -c 3000 (发3000次) -t 10 (10秒) http://192.168.0.57/index.html
webbench -c 3000 -t 10 http://192.168.0.55/index.html

安装php:
下载libiconv-1.13.1.tar.gz #加强系统对支持字符编码转换的功能
libmcrypt-2.5.8.tar.bz2
mhash-0.9.9.9.tar.gz #mcrypt,mhash是php加密算法扩展库
mcrypt-.26.8.tar.gz
tar zxf libiconv..
cd libiconv
./configure --libdir=/usr/local/lib64
make && make install

tar jxf libmcrypt..
cd libmcrypt..
./configure --libdir=/usr/local/lib64
make && make install
cd libltdl
./configure --libdir=/usr/local/lib64 --enable-ltdl-install
make && make install

tar jxvf mhash..
cd mhash..
./configure --libdir=/usr/local/lib64
make && make install

ldconfig /usr/local/lib64 #为了解决 mcrypt 执行./configure 时报错的:configure: error: ***libmcrypt was not found。如果ldconfigure后还是报错,则如下编辑ld.so.conf
vim /etc/ld.so.conf #实现开机加载,使用动态装入器装载找到库文件
/usr/local/lib64 添加次行

tar zxf mcrypt.. #上面的libmcrypt,mhash是mcrypt的依赖包
cd mcrypt..
./configure --libdir=/usr/local/lib64 #./configure时可能会报这个错:/bin/rm: cannot remove `libtoolT’: No such file or directory 直接忽略。 或者报这个错:
configure: error: *** libmcrypt was not found,则需 ldconfig /usr/local/lib64
(最近安装一些库文件,这样可以重新加载库文件)
vim /etc/ld.so.conf #实现开机加载,使用动态装入器装载找到库文件
/usr/local/lib64 添加次行

make && make install

yum install net-snmp-devel curl-devel libxml2-devel libpng-devel libjpeg-devel freetype-devel gmp-devel -y

下载php-5.3.6
tar zxf php..
cd php..
./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-mysql=/usr/local/lnmp/mysql/ --with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config
--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 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-libdir=lib64 --with-mcrypt --with-mhash

make ZEND_EXTRA_LIBS='-liconv' #稍等,时间有点长.Zend Optimizer用优化代码的方法来提高PHP 4.0应用程序的执行速度.Zend引擎是一个开源脚本引擎.(没有安装前面那4个软件包,编译时报找不到liconv的错)
make install

wget http://pear.php.net/go-pear.phar #支持php的扩展与应用库功能,它是一个PHP扩展及应用的一个代码仓库
/usr/local/lnmp/php/bin/php go-pear.phar 或在~/.bash_profile 中的PATH后添加/usr/local/lnmp/php/bin/ source ~/.bash_profile
php go-pear.phar #添加后就可以直接执行php命令了,安装go-pear.phar
cd php-5.3.6
cp php.ini-production /usr/local/lnmp/php/etc 拷贝php的配置文件
cd /usr/local/lnmp/php/etc/
mv php.ini-production php.ini

vim php.ini
cgi.fix_pathinfo=0 大概842行,防止Nginx文件类型错误解析漏洞
date.timezone = Asia/Chongqing 843行

cp php-fpm.conf.default php-fpm.conf php-fpm的配置文件
vim php-fpm.conf
pid = run/php-fpm.pid 大概25行
pm.max_children = 50 152行
pm.start_servers = 20 157行
#在生产环境中一定要做压力测试,找到最合适的进程数组合
pm.min_spare_servers = 5 162行
pm.max_spare_servers = 35 167行
pm.max_requests = 500 173行

cd php-5.3.6/sapi/fpm
cp init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
/etc/init.d/php-fpm start
ps aux 默认启动20个进程

vim /usr/local/lnmp/nginx/conf/nginx.conf
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;
}
如果想要用反向代理,则
location ~ \.php$ {
proxy_pass http://192.168.235; 将php网页交给192.168.0.235 处理
}

location / {
root html;
index index.html index.htm index.php;
}

cd /usr/local/lnmp/nginx/html/
vi index.php
<?php
phpinfo();
?>
在浏览器中输入 desktop55/index.php

下载最新版的phpMyAdmin-3.4.2
tar jxf phpMyAdmin.. -C /usr/local/lnmp/nginx/html/
cd /usr/local/lnmp/nginx/html/
mv phpMyAdmin.. phpadmin
cd phpadmin
cp config.sample.inc.php config.inc.php
vi config.inc.php
$cfg['blowfish_secret'] = 'westos'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */

php 模块扩展:

下载memcached-1.4.5.tar.gz

memcache-2.2.5.tgz

eaccelerator-0.9.6.tar.bz2

yum install libevent-1.4.13.1.el6.x86_64.rpm 不行的话用rpm安装

将optional部分加到dvd.repo中

yum install libevent-devel -y 不行的话用rpm安装

tar zxf memcached-1.4.5 后台软件包

cd memcached-1.4.5

./configure

make && make install

cd

memcached -u root -m 50 -d 以root用户身份占50M内存在后台运行memcache

ps aux

netstat -antlp

tar vxf memcache-2.2.5.tgz 功能模块软件包,以支持某些服务的memcache,如php

cd memcache..

phpize 定义安装环境,并生成一些数据,如configure

ls

./configure

make && make install

cd /usr/local/lnmp/php/etc/

vim php.ini

extension=memcache.so 大概在931行,使php支持memcache

/etc/init.d/php-fpm reload 平滑加载php

然后在浏览器 http://desktop55/index.php 找memcache 模块

cd memcache-2.2.5

cp memcache.php /usr/local/lnmp/nginx/html/

cd /usr/local/lnmp/nginx/html/

vim memcache.php memcache的管理页面

define('ADMIN_USERNAME','admin'); memcache管理页面的用户名和密码

define('ADMIN_PASSWORD','westos');

$MEMCACHE_SERVERS[] = '192.168.0.55:11211'; memcache服务器ip地址

#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array

然后在浏览器 http://desktop55/memcache.php
输入 用户名和密码 就可以打开图形化的memcache

下载memcached-1.4.5.tar.gz

memcache-2.2.5.tgz

eaccelerator-0.9.6.tar.bz2

yum install libevent-1.4.13.1.el6.x86_64.rpm #不行的话用rpm安装

将optional部分加到dvd.repo中

yum install libevent-devel -y 不行的话用rpm安装

tar zxf memcached-1.4.5

cd memcached-1.4.5

./configure

make && make install

cd

memcache -u root -m 50 -d

ps aux

netstat -antlp

tar jxf memcache-2.2.5.tgz

cd memcache..

phpize

ls

./configure

make && make install

cd /usr/local/lnmp/php/etc/

vim php.ini

extension=memcache.so 大概在931hang

然后在浏览器 http://desktop55/index.php 找memcache 模块

cd memcache-2.2.5

cp memcache.php /usr/local/lnmp/nginx/html/

vim memcache.php

define('ADMIN_USERNAME','admin'); // Admin Username

define('ADMIN_PASSWORD','westos'); // Admin Password

$MEMCACHE_SERVERS[] = '192.168.0.55:11211'; // add more as an array

#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array

然后在浏览器 http://desktop55/memcache.php
输入 用户名和密码 就可以打开图形化的memcache

yum install httpd php -y

下载eaccelerator-0.9.6.tar.bz2

cd eaccelerator-0.9.6

phpize 如果此时没有phpize这个命令,则需安装一下两个包

php-devel-5.3.3-3.el6_2.8.x86_64.rpm

php-eaccelerator-0.9.6.1-1.el6.x86_64.rpm

安装好了之后就有phpize这个命令了

which phpize

./configure --with-php-config=/usr/local/lnmp/php/bin/php-config

make

make install

vim /usr/local/lnmp/php/etc/php.ini

extension=eaccelerator.so 添加次行,大概933行

/etc/init.d/php-fpm reload

此时用浏览器访问 192.168.0.235/index.php 可以看到eaccelerator支持php加速

cp control.php /usr/local/lnmp/nginx/html/

vim /usr/local/lnmp/nginx/html/control.php

$auth = true;

$user = "admin";

$pw = "westos"; 修改eaccelerator 的网页管理密码

/etc/init.d/php-fpm reload

ll /var/log/httpd 日志目录

-rw-r--r--. 1 root root 0 Jul 14 02:52 eaccelerator_log

打开浏览器 http://desktop53/control.php
输入用户名和密码后就会显示加速控制面板

下载ImageMagick-6.6.9-9.tar.gz

tar zxf ImageMagick..

cd ..

./configure

make && make install

下载imagick-3.0.0.tgz

tar zxf imagick..

cd ..

phpize 后会生成configure

./configure

make && make install

ll /usr/lib64/php/modules/

cd /etc/php.d

vim imagick.ini

extension=imagick.so

/etc/init.d/httpd restart

打开浏览器 http://desktop53/ 看是否有imagick模块

yum install php-mysql -y 使php支持mysql
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  安装 源码 lnmp 架构