现在的位置: 首页 > 综合 > 正文

LNMP环境搭建

2012年07月10日 综合 ⁄ 共 8553字 ⁄ 字号 暂无评论
孙国栋广告

测试环境:
操作系统:centos 5.5 32位
mysql数据库:mysql-5.1.58.tar.gz
nginx软件包:nginx-0.7.65.tar.gz
PHP软件包:php-5.3.8.tar.gz

1 安装mysql数据库
tar -zxvf mysql-5.1.58.tar.gz
cd mysql-5.1.58
./configure --prefix=/usr/local/mysql --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl--with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg
make
make install
创建MySQL数据库服务器的配置文件
cp support-files/my-medium.cnf /etc/my.cnf
cd /usr/local/mysql/
初始化mysql数据库
bin/mysql_install_db --user=mysql
修改文件及其目录的权限,使其目录或文件的宿主为root,宿组为mysql,(data的宿主为mysql)
chown -R root .
chown -R mysql data
chgrp -R mysql .
启动mysql数据库
/usr/local/mysql/bin/mysqld_safe --user=mysql &
netstat -tnl|grep 3306
设置mysql启动脚本文件
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld

/etc/init.d/mysqld restart
/usr/local/mysql/bin/mysql -uroot -p
2 安装nginx服务
安装openssl-0.9.8i.tar.gz软件包
./config --prefix=/usr/local/openssl
make &&make install
安装pcre-8.12.tar.gz软件包(可以安装光盘里面pcre和pcre-devel)
./configure --prefix=/usr/local/pcre
make &&make install
安装nginx软件包
/usr/sbin/groupadd nginx
/usr/sbin/useradd -g nginx nginx -s /sbin/nologin
./configure
--user=nginx
--group=nginx
--with-http_stub_status_module --with-openssl=/usr/local/openssl/
--with-pcre=/opt/pcre-8.12/ 该目录指定的是解压pcre后生成的目录
--prefix=/usr/local/nginx
--conf-path=/usr/local/nginx/conf/nginx.conf
--with-http_realip_module
--with-http_addition_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_stub_status_module
--with-http_sub_module
--with-http_dav_module
make && make install
测试nginx 服务是否安装成功
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx//conf/nginx.conf test is successful
启动nginx 服务
/usr/local/nginx/sbin/nginx
查看nginx 是否开启(即查看nginx的端口是否处于LISTEN)
netstat -antl |grep "LISTEN"
3 安装PHP服务
yum安装php模块所需用的库文件
yum install libmcrypt-devel libmhash mhash-devel gettext gettext-devel

tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt
make && make install

tar -zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1
./configure --prefix=/usr/local/libiconv
make && make install

tar -zxvf freetype-2.4.0.tar.gz
cd freetype-2.4.0.
./configure --prefix=/usr/local/freetype
make && make install
tar -zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b
./configure --prefix=/usr/local/jpeg/
make
make install

在执行make install的时候错误提示信息
usr/bin/install: 无法创建一般文件“/usr/local/jpeg/bin/cjpeg”
mkdir /usr/local/jpeg
系统提示那个目录无法创建,就手动创建一个,提示那个目录就手工创建一个,直到成功执行make instll命令
tar -zxvf libxml2-2.6.32.tar.gz
cd libxml2-2.6.32
./configure --prefix=/usr/local/libxm12
make && make install
tar -zxvf curl-7.21.0.tar.gz
cd curl-7.21.0
./configure --prefix=/usr/local/curl
make && make install
tar -zxvf gd-2.0.35.tar.gz
cd gd-2.0.35
./configure --prefix=/usr/local/gd
make make install
tar -zxvf php-5.3.8.tar.gz
cd php-5.3.8
./configure
--prefix=/usr/local/php
--with-config-file-path=/usr/local/php/etc
--with-gd=/usr/local/gd
--with-mysql=/usr/local/mysql/
--with-mysqli=/usr/local/mysql/bin/mysql_config
--with-jpeg-dir=/usr/local/jpeg6/ --with-zlib-dir
--enable-mbstring=all --with-pdo-mysql
--with-freetype-dir=/usr/local/freetype
--with-mcrypt
--enable-sockets
--enable-mbstring
--enable-fpm

make
make时候报下面错误
/usr/bin/ld: cannot find -lltdl collect2: ld returned 1 exit status
原因:是在编辑php时添加的“–with-mcrypt”选项造成
解决方法:
1)如果不需要mcrypt,那么编辑php时去掉该选项,然后再make、make install。
2)如果需要mcrypt,那么需要安装libltdl
libltdl在libmcrypt软件包中就有,具体过程:
cd /software/libmcrypt-2.5.8/libltdl
./configure -enable-ltdl-install
make && make install
make install
添加PHP配置文件
cp php.ini-production /usr/local/php/etc/php.ini
vi /usr/local/php/etc/php.ini
添加date.timezone = Asia/Shanghai
更改short_open_tag = Off 为short_open_tag = On (开启短格式支持)
更改expose_php = on 为expose_php = off (在curl中隐藏php版本号)
添加php-fpm配置文件
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
;pm.start_servers = 20
;pm.min_spare_servers = 5
;pm.max_spare_servers = 35
设置php-fpm启动文件
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --level 3 php-fpm on
chmod +x /etc/init.d/php-fpm
/etc/init.d/php-fpm start
Starting php-fpm done
整合php和nginx
vim /usr/local/nginx/conf/nginx.conf
user nginx nginx; #使用的用户和用户组
worker_processes 4; #指定工作衍生进程数,一般等于CPU的总核数或者总核数的两倍,例如两个四核CPU,则总核数为8
#worker_cpu_affinity 0001 0010 0100 1000;
error_log /data/logs/nginx_error.log crit; #指定错误日志存放的路径,错误日志记录级别可选项为:debug | info | notice | warn | error |crit
pid logs/nginx.pid; #指定pid存放的路径

worker_rlimit_nofile 51200; #用于绑定worker进程和CPU

events { #用来指定Nginx工作模式和及其连接上限
use epoll; #使用的网络I/O模型,Linux系统推荐采用epoll模型,FressBSD系统推荐采用kqueue模型
worker_connections 51200; #Nginx每个进程允许的连接数
multi_accept on;
}

http {
include mime.types; #实现主配置文件对所包含文件的设定,可以减少主配置文件的复杂度
default_type application/octet-stream; # HTTP核心模块指令,默认为二进制流

#charse gb2312; #设置字符集,如果一个网站有多种字符集,请不要随便设置,让程序员在HTML代码中设置

server_names_hash_bucket_size 128;
client_header_buffer_size 16k;
large_client_header_buffers 4 256k;

server_tokens off;

sendfile on; #开启高效稳健传输
tcp_nopush off; #防止网络拥塞
tcp_nodelay on; #防止网络拥塞

keepalive_timeout 60; #客户端连接保持会话的超时时间

client_header_timeout 30; #设置客户端请求头读取超时时间
client_body_timeout 30; #设置客户端请求读取主体读取超时时间
send_timeout 30; #指定响应客户端的超时时间
client_max_body_size 30M; #设置客户端能够长传的文件大小

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压缩
gzip_min_length 1000; #允许压缩页面的最小字节数
gzip_buffers 4 16k; #申请4个单位为16K的内存做为压缩结果流输出
#gzip_http_version 1.0; #用于识别HTTP版本,默认1.1
gzip_comp_level 9; #用来指定GZIP的压缩比,1压缩比最小,处理速度最快,9压缩比最大,传输速度快,但处理数据最慢,也消耗CPU。
gzip_types text/plain text/javascript application/x-javascript text/css application/xml text/xml application/xml+rss; 指定压缩类型
gzip_vary on; #前端的缓存服务器经过GZIP压缩

fastcgi_intercept_errors on;

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #指定Nginx日志输出的格式,main为此日志输出的格式的名称
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$request_time"';

#error_page 500 502 503 504 /50x.html;
#error_page 402 403 404 500 502 503 504 http://www.test.com/application/controllers/error.php;
error_page 402 403 502 503 504 /error;

server
{
listen 80;
server_name www.test.com;
index index.html index.htm index.php;
root /data/www;

location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
在/usr/local/nginx/conf/目录中创建fcgi.conf文件
vim /usr/local/nginx/conf/fcgi.conf
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
测试nginx配置文件
/usr/local/nginx/sbin/nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

优化Linux内核参数

vim /etc/sysctl.conf
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800
#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535
使配置立即生效:
/sbin/sysctl -p
4 安装eaccelerator 优化php
tar -jxvf eaccelerator-0.9.6.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
在PHP主配置文件里面添加下面参数
vim /usr/local/php/etc/php.ini
extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
建立文件夹
mkdir /tmp/eaccelerator
chmod 0777 /tmp/eaccelerator
重启php-fpm服务
service php-fpm restart
建立网站根目录
mkdir -p /data/www 新建下面测试内容
 phpinfo();
?>

测试LNMP整合是否成功

给我留言

留言无头像?