您的位置:首页 > 其它

lnmp安装

2016-07-10 19:31 232 查看
http://pan.baidu.com/share/link?shareid=394225228&uk=1327557510
LAMP安装的一般步骤:
php的yum库:yum install -y gcc-c++ zlib-devel pcre-devel libjpeg-devel libpng-devel freetype-devel libxml2-devel glibc-devel bzip2-devel curl-devel openssl-devel

php的编译参数:./configure --prefix=/usr/local/php --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-config-file-path=/usr/local/php/etc --with-libxml-dir --with-openssl --with-zlib-dir --with-curl --with-pear --with-pcre-dir --with-gd --with-jpeg-dir --with-png-dir --with-mcrypt --with-freetype-dir --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-iconv-dir --enable-exif --enable-ftp --enable-fpm --enable-debug --enable-gd-native-ttf --enable-mbstring --enable-soap --disable-ipv6

useradd -s /sbin/nologin -M php-fpm 先建立用户 否则会出现【www】池不能查到php-fpm用户
cp php.ini-production /usr/local/php/etc/php.ini 全局配置文件
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 启动配置文件
cp /usr/local/php/etc/php-fpm.conf-default /usr/local/php/etc/php-ftm.conf针对浏览器配置文件
。。。。。。/usr/local/php/sbin/php-fpm -t 安装完后必须进行测试
vi /usr/local/php/config/php-fpm.conf
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
[www]
listen = /tmp/www.sock #名字可以自定义 靠这文件与nginx通信 用来定义资源池所监听的sock
user = php-fpm 用来定义php-fpm以哪个用户和组来运行
group =php-fpm
pm = dynamic #动态的管理下面的内容 如pm=static是对于专用服务器 用于控制子进程数
pm.max_children = 50 #子进程最大有50个
pm.start_servers = 20 #最开始启动20个
pm.min_spare_servers = 5 #空闲的时候最小不能低于5个
pm.max_spare_servers = 35 # 最多的时候不能多与35个子进程
pm.max_requests = 500 #一个子进程在一个生命周期内处理多少个请求然后销毁
rlimit_files = 1024 # 每个进程使用的文件描述符的限制(可调大一点)
针对慢查询日志
slowlog = /tmp/www_slow.log
request_slowlog_timout = 1
php_admin_value[open_basedir] = /data/www/:/tmp/ (多个目录用:分开)设置网站域名访问的根目录。
php配置文件: 可以有多个地址池。。。。。。。。

nginx的参数配置:./configure --prefix=/usr/local/nginx --with-pcre --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module
。。。。。/usr/local/nginx/sbin/nginx -t 安装完成后一定要测试

因为vhosts目录下自带了默认的虚拟主机配置文件,访问时候会自动跳转到默认的虚拟主机,为了安全起见我们禁止所有的访问,使其访问我们自己设置的网页。添加default,表示默认主机;路径修改为空目录/tmp/1233/;deny all表示禁止所有访问,会报403错误。

nginx的参数配置:./configure --prefix=/usr/local/nginx --with-pcre --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module

在localhost下启动文件:/usr/local/nginx/html/&fastcgi_script_name
nginx的启动配置文件:vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}

reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}

restart(){
stop
start
}

configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}

case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL

NGINX的配置文件:vi /usr/local/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 6000;
}

http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;
include vhosts/*.conf;
}

NGINX下的网站配置
首先建立一个默认完整
server {
listen 80 default_server;
server_name localhost;
index index.html index.htm index.php;
root /tmp/cisco;
deny all;
}
新建一个虚拟主机目录
server {
listen 80; 监听的端口
server_name www.tt.com www.tt1.com;表示服务器网站
index index.html index.htm index.php
root /data/www
}
location ~ \.php {
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_scription_name;
}

2.方法B(为带www的域名单独设一条server规则)另一种实现域名跳转的方式:
rewrite ^(.*) http://tt.com$1 permanent; 直接在主目录下添加
if ($host!='www.tt.com')域名跳转:
{
rewrite ^/(.*)$ http://www.tt.com/$1 permanent;跳转规则 (permanent永久重定向|break中止rewrite,不再匹配|redirect 返回临时重定向的http状态|last基本上都用這个flag);
}
index index.html index.htm index.php php解析
root /data/www 主目录
查看日志格式再 neigx.conf中 该格式为format
access_log /tmp/access.log format 日志保存路径及日志格式名
禁止指定user-agent
if ( $http_user_agent ~* 'curl|baidu|sina') 其中*表示不区分大小写
{
return 403;
}
测试方式:curl -A "qeasdrftgyhuj" -x192.168.0.109:80 www.test.com/forum.php -I
deny ip; 在全局下拒绝某个地址用于地址攻击
子页面用户认证 必须在主配置文件下进行(两种方式)
location ~ .*admin\.php$ 网站 或者 location ~/目录/
{
#auth_basic "cisco" 登陆提示名
#auth_basic_user_file /usr/local/nginx/etc/htpass 是密码文件存放位置
deny ip 拒绝的地址
解析文件
fastcgi_pass unix:/tmp/www.sock
include pastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
检验方式:curl -x127.0.0.1:80 -u user:passwd www.tt.com/admin.php

指定不记录文件的类型 必须放在加密文件配置文件下面
查看日志格式再 neigx.conf中 该格式为format
access_log /tmp/access.log format 日志保存路径及日志格式名
location ~ .*(gif|jpeg|png|bmp|jpg|swf|mp3|flv|zip|bz2|rar)$
{
access_log off;
expires 2d; 指定静态文件的缓存时间
valid_referers none blocked *.bt.com *.btt.com 防盗链 在静态文件图片下
if ($invalid_referer)防盗链
{
return 403;防盗链
}
}

防盗链测试方式
curl -e "http://www.baidu.com" -I -x127.0.0.1:80 http://www.test.com/static/image/common/logo_88_31.gif
指定静态文件的缓存时间
location ~\.(jss|cs)
{
access_log off;
expires 2d; 指定静态文件的缓存时间

}

location ~ (static|cache)
{
access_log off;
}

nginx 代理服务器配置

upstream linux{
server 代理服务器ip:80
server 代理服务器ip:80
}
server {
listen 80;
server_name 代理网站名称可以有多个;

location / {
proxy_pass http://代理网站地址或者是 名称 /;
proxy_set_header host $host;对于多个
#proxy_set_header x-real-ip $remote-addr;
}
}

日志切割(脚本)其中移动日志的文件名必须和日志格式中的文件名相同
vi /tmp/log.sh
!#/bin/bash
d=`date -d '-1 day' +%F`
[ -d /tmp/log ] || make /tmp/log ;判断文件是否存在
mv /tmp/access.log /tmp/log/$d.log 重新生成新的日志
/usr/local/nginx/sbin/nginx -s reload 或 /etc/init.d/nginx reload > /dev/null(在有启动脚本的情况下)
cd /tmp/log
gzip -f $d.log 压缩日志文件,不保留源文件

LNMP的扩展:
隐藏apache版本信息
servertokens productonly
serversignature off
隐藏nginx版本信息
在nginx.conf 的http模块下加入
server_tokens off;
隐藏php版本信息
编辑php.ini
把expose_php = On 修改为 expose_php = Off

nginx可以通过HTTPLimitZoneModule和HTTPCoreModule两个组件来对目录进行限速。
http {
limit_zone one $binary_remote_addr 10m;
server {
location /download/ {
limit_conn one 1;
limit_rate 300k;
}}}
limit_zone,是针对每个IP定义一个存储session状态的容器。这个示例中定义了一个10m的容器,按照32bytes/session,可以处理320000个session。limit_conn one 1;限制每个IP只能发起一个并发连接。
limit_rate 300k;对每个连接限速300k. 注意,这里是对连接限速,而不是对IP限速。如果一个IP允许两个并发连接,那么这个IP就是限速limit_rate×2。
还可以用来显示每个IP的并发数
nginx限制ip并发数,也是说限制同一个ip同时连接服务器的数量
1.添加limit_zone 这个变量只能在http使用
vi /usr/local/nginx/conf/nginx.conf
limit_zone one $remote_addr 10m;
2.添加limit_conn 这个变量可以在http, server, location使用
我只限制一个站点,所以添加到server里面
vim /usr/local/nginx/conf/vhost/taobap.cn.conf
limit_conn one 10;
3.重启nginx killall -HUP nginx

Linux下查看Nginx、Napache、MySQL、PHP的编译参数的命令如下:
1、nginx编译参数:
#/usr/local/nginx/sbin/nginx -V
2、apache编译参数:
# cat /usr/local/apache/build/config.nice
3、php编译参数:
# /usr/local/php/bin/php -i |grep configure
4、mysql编译参数:
# cat /usr/local/mysql/bin/mysqlbug|grep configure

nginx 设置自定义header
vim nginx.conf
在http模块中加入:
add_header myheader "the coments of myheader";
这里的myheader 是自定义的名字,你自己随便写。
加完后,测试:curl -I http://IP apache自定义header
vi http.conf
<ifmodule mod_headers.c>
header add myheader "3242"
</ifmodule>
apache的404跳转到首页
Errordocument 404 /404.php

Nginx 404 跳转至首页
error_page 404 /404.php;
另外也可以通过rewrite规则实现
在 rewrite rule 的最后增加
if (!-e $request_filename) {
rewrite ^(.*)$ /404.php last;
}

Nginx中禁止访问.txt文件 需要:stub_status模块
location ~* \.(txt|doc)$
{ if (-f $request_filename)
{root /home/domain/public_html/test;
break;
} }
或者:location ~*\.(txt|doc)$
{
if (-f $request_filename)
return 403;
}
$request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成

Nginx出现413 Request Entity Too Large

处理Nginx出现的413 Request Entity Too Large错误
这个错误一般在上传文件的时候出现,打开nginx主配置文件nginx.conf,找到http{}段,添加
client_max_body_size 8m;重新加载nginx的配置:nginx -t:kill -HUP nginx_pid
要是跑php的话这个大小client_max_body_size要和php.ini中的如下值的最大值一致或者稍大,这样就不会因为提交数据大小不一致出现的错误。post_max_size = 8M:upload_max_filesize = 2M
重启php-cgi:killall php_cgi:php_cgi start
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lnmp