1.准备php函数的rpm包
1)保证yum源正常
2) 所有lamp的准备都要完成
rpm安装的httpd关闭,同时禁用自启动
关闭selinux和iptables
2.准备lnmp其他的源代码包
http://lnmp.org/download.html
3.安装过程
1)一键安装过程
# ./centos.sh
这一步需要设置MySQL的root密码,输入后回车进入下一步,如下图所示:
这里需要确认是否启用MySQL InnoDB,如果不确定是否启用可以输入 y ,这个可以单独在MySQL文件里关闭,输入 y 表示启用,输入 n 表示不启用。输入 y 或 n 后回车进入下一步,显示如下信息:
这一步是选择php版本,可以选择 PHP 5.3.17 或 PHP 5.2.17,安装PHP 5.3.17的话输入 y ,安装PHP 5.2.17 输入 n,输入 y 或 n 后回车,进入下一步MySQL版本的选择,如下图:
可以选择MySQL 版本 5.5.27 或 5.1.60,安装MySQL 5.5.27的话输入 y ,安装MySQL 5.1.60 输入n,输入 y 或 n 后回车,完成选择。
提示"Press any key to start...",按回车键确认开始安装。
LNMP脚本就会自动安装编译Nginx、MySQL、PHP、phpMyAdmin、Zend Optimizer这几个软件。
2)、安装完成
如果显示如下界面:
说明已经安装成功。
3)、安装失败
4)、eAccelerator,xcache,memcached,imageMagick,ionCube的安装
主要介绍eAccelerator,xcache,memcached,imageMagick,ionCube这几个PHP加速和php扩展的安装及使用,这些不一定对每个人都有用,自己按需要安装,下面会对各个程序的作用、安装方法及相关注意事项。
eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 它还有对脚本起优化作用,以加快其执行效率。
安装
进入lnmp解压后的目录,执行:./eaccelerator.sh 运行后有如下提示:
eaccelerator 0.9.5.3 支持PHP 5.2,如需此版本,输入 old 回车。
eaccelerator 0.9.6.1 支持PHP 5.2、5.3,如需此版本,输入 new 回车。
eaccelerator 1.0-dev 支持PHP 5.2、5.3、5.4,如需此版本输入 dev 回车。
提示“Press any key to start...or Press Ctrl+c to cancel” 后回车确认,脚本就会自动安装eaccelerator。
XCache 是一个国人开发的又快又稳定的 PHP opcode 缓存器. 经过良好的测试并在大流量/高负载的生产机器上稳定运行. 经过(在 linux 上)测试并支持所有现行 PHP 分支的最新发布版本。
安装
进入lnmp解压后的目录,执行:./xcache.sh 运行后有如下提示:
一个有两个版本可以选择,分别是XCache 2.0.0 和XCache 3.0.1 ,建议安装XCache 3.0.1 输入new 回车
出现“Press any key to start...or Press Ctrl+c to cancel“,再次回车确认即可开始安装。
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。
安装
进入lnmp解压后的目录,执行:./memcached.sh
回车确认后就会自动安装memcache php扩展和memcached。
ImageMagick是一个用于查看、编辑位图文件以及进行图像格式转换的开放源代码软件套装。它可以读取、编辑超过100种图象格式。可用来替换GD库。
安装
进入lnmp解压后的目录,执行:./imageMagick.sh
回车确认后就会自动安装imageMagick。
设置:比如在DiscuzX里设置时路径需要填写:/usr/local/imagemagick/bin/ 并且禁用函数里的exec要去掉才可以正常使用。
5)、LNMP相关软件目录及文件位置
除去所必须的依赖包,LNMP一键安装包会安装一下软件:
默认安装软件:
· Nginx
· MySQL
· PHP
· PHPMyAdmin
· Zend Optimizer
可选软件:
注明可选的为可使用安装脚本自行安装,未注明的为会自动安装。
不一定可选包都适合你,按需安装即可。
LNMP相关软件安装目录
Nginx 目录: /usr/local/nginx/
MySQL 目录 : /usr/local/mysql/
MySQL数据库所在目录:/usr/local/mysql/var/
PHP目录 : /usr/local/php/
PHPMyAdmin目录 : /home/wwwroot/default/phpmyadmin/ (记得把配置文件验证方式改为http)。
默认网站目录 : /home/wwwroot/default/
Nginx日志目录:/home/wwwlogs/
LNMP相关配置文件位置
Nginx主配置文件:/usr/local/nginx/conf/nginx.conf
MySQL配置文件:/etc/my.cnf
PHP配置文件:/usr/local/php/etc/php.ini
6)、LNMP状态管理命令
LNMP状态管理命令:
LNMP状态管理: /root/lnmp {start|stop|reload|restart|kill|status}
Nginx状态管理:/etc/init.d/nginx {start|stop|reload|restart}
MySQL状态管理:/etc/init.d/mysql {start|stop|restart|reload|force-reload|status}
Memcached状态管理:/etc/init.d/memcached {start|stop|restart}
如重启LNMP,输入命令:/root/lnmp restart 即可,单独重启mysql:/etc/init.d/mysql restart
4. 修改Nginx配置文件
vi /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes 1;
#启动进程,通常设置成和cpu的数量相等。相当于cpu个数
error_log logs/nginx_error.log;
#错误日志
pid /usr/local/nginx/nginx.pid;
#主进程PID保存文件
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
#文件描述符数量
events
{
use epoll;
#网络I/O模型,建议linux使用epoll,FreeBSD使用kqueue
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections 51200;
#单个工作进程最大允许连接数
}
http
#整体环境配置
{
include mime.types;
default_type application/octet-stream;
#设定mime类型,文件传送类型由mime.type文件定义
server_names_hash_bucket_size 128; #保存服务器名字的hash表大小
client_header_buffer_size 32k; #客户端请求头部缓冲区大小
large_client_header_buffers 4 32k; #最大客户端头缓冲大小
client_max_body_size 50m; #客户端最大上传文件大小(M)
sendfile on;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文 件,对于普通应用,必须设为 on。如果用来进行下载等应用磁盘IO重负载应用,可 设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
#高效文件传输
tcp_nopush on;
#这个是默认的,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的 传输出去,这样有助于解决网络堵塞。(只在sendfile on时有效)
keepalive_timeout 60;
#连接超时时间
tcp_nodelay on;
#禁用nagle算法,也即不缓存数据。有效解决网络阻塞
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 256k;
#fastcgi设置
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;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
#开启gzip网络压缩
#limit_zone crawler $binary_remote_addr 10m;
server_tokens off;
#隐藏nginx版本号(curl -I 192.168.4.154可以查看,更加安全)
#log format
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#定义日志格式
server
{
listen 80;
#监听80端口
server_name www.lnmp.org;
#服务器名
index index.html index.htm index.php;
#默认网页文件
root /home/wwwroot/default;
#网页主目录
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
#分析文件是否存在,如果不存在报404错误
fastcgi_pass unix:/tmp/php-cgi.sock;
#指定FastCGI服务器监听端口与地址。这里使用unix socket
#nginx以unix-domain-socket方式连接fastcgi(php)。更快,适合大流量访问
#另外一种 nginx连接fastcgi的方式是http方式的[ 127.0.0.1:9000 方式连接 fastcgi(php)
fastcgi_index index.php;
#默认网页文件
include fcgi.conf;
#fcgi.conf配置文件生效
}
location /status {
stub_status on;
access_log off;
}
#开启status状态监测
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
#静态文件处理,保存期30天
location ~ .*\.(js|css)?$
{
expires 12h;
}
#js和css文件处理,保存期12小时
access_log /home/wwwlogs/access.log access;
#正确访问日志
}
include vhost/*.conf;
#vhost/下子配置文件生效
}
5.配置开机启动nginx,php-fpm,ulimit
1)nginx
vi /etc/rc.local
/usr/local/nginx/sbin/nginx
2)php-fpm
vi /etc/rc.local
/usr/local/php/sbin/php-fpm start
3)ulimit
vi /etc/profile
ulimit -SHn 65535
4)mysql
vi /etc/rc.local
/usr/local/mysql/bin/mysqld_safe --user=mysql &
一键安装包已经完成自启动,加入了chkconfig管理,可以直接使用service命令和chkconfig命令管理
6.检查nginx配置文件语句错误
/usr/local/nginx/sbin/nginx -t
7.平滑重启nginx进程
1)pkill -HUP nginx
2)kill -HUP `pgrep -uroot nginx`
Pgrep -uroot nginx 取出nginx主进程PID
3)/usr/local/nginx/sbin/nginx -s reload
8.配置nginx虚拟主机
sina和sohu域名事先解析
Vi /usr/local/nginx/conf/nginx.conf
==èwww.sina.com公司网站
server
{
listen 80;
server_name www.sina.com;
index index.html index.htm index.php;
root /home/wwwroot/sina;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
}
==èwww.sohu.com公司网站
server
{
listen 80;
server_name www.sohu.com;
index index.html index.htm index.php;
root /home/wwwroot/sohu;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
}
重启nginx
最后在客户端测试虚拟主机www.baidu.com和www.sina.com两家公司网站
9.列表页显示
server
{
listen 80;
server_name www.sina.com;
index index.html index.htm index.php;
root /home/wwwroot/sina;
autoindex on;
10.nginx状态监控
location /nginxstatus{
stub_status on;
access_log off;
}
#客户端访问网址:http://IP/nginx/status
11.rewrite正则过滤
location ~ \.php$ {
proxy_pass http://127.0.0.1;
}
Rewrite指令最后一项参数为flag标记,支持的flag标记如下:
Last
停止执行当前这一轮的ngx_http_rewrite_module指令集,然后查找匹配改变后URI的新location;
Break
停止执行当前这一轮的ngx_http_rewrite_module指令集;
Redirect
在replacement字符串未以“http://”或“https://”开头时,使用返回状态码为302的临时重定向;
Permanent
返回状态码为301的永久重定向。
Last和break用来实现uri重写,浏览器地址栏的url地址不变,但在服务器访问的路径发生了变化,redirect和permanent用来实现url跳转,浏览器地址栏会显示跳转后的url地址,使用alias指令时必须使用last标记,使用proxy_pass指令时要使用break标记,last标记在本条rewrite规则执行完毕后,会对其所在的server{}标签重新发起请求,而break标记则在本条规则匹配完成后,终止匹配,不再匹配后面的规则.
例1:域名跳转
输入www.sina.com,跳转到www.sohu.com
server
{
listen 80;
server_name www.sina.com;
index index.html index.htm index.php;
root /home/wwwroot/sina;
if ($http_host = www.sina.com) {
rewrite (.*) http://www.sohu.com permanent;
}
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
}
server
{
listen 80;
server_name www.sohu.com;
index index.html index.htm index.php;
root /home/wwwroot/sohu;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
}
例2:文件跳转
server
{
listen 80;
server_name www.sina.com;
index index.html index.htm index.php;
root /home/wwwroot/sina;
rewrite inde /indexa.html last;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
}