@Great-Chinese
2016-12-05T08:13:21.000000Z
字数 11367
阅读 1123
LNMP架构搭建与优化
A:下载与解压
wget http://cn2.php.net/distributions/php-5.4.37.tar.bz2
tar jxvf php-5.4.37.tar.bz2
B:配置编译参数
cd php-5.4.37 # 进入此目录中
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6
C:编译
make
echo $?
D:安装
rm -rf /usr/local/php/ # 在安装前,要把之前旧的php目录删除掉
make install
echo $?
cp php.ini-production /usr/local/php/etc/php.ini # 拷贝生产环境的配置文件
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # 拷贝启动脚本
vim /etc/init.d/php-fpm # 编辑启动的shell脚本
chmod 755 /etc/init.d/php-fpm # 授予执行权限
cd /usr/local/php/etc/
mv php-fpm.conf.default php-fpm.conf
chkconfig --add php-fpm # 加入系统服务列表中
chkconfig php-fpm on # 开机启动
useradd -s /sbin/nologin php-fpm # 增加用户
service php-fpm start # 启动
/usr/local/php/sbin/php-fpm -t # 检查是否有问题
ps aux|grep php-fpm
netstat -lnp
A:下载与解压
cd /usr/local/src # 进入此目录中
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz
B:配置编译参数
cd nginx-1.6.2 # 进入此目录中
./configure --prefix=/usr/local/nginx --with-pcre
yum install -y pcre-devel
C:编译/安装
make
make install
cd /usr/local/nginx
/usr/local/apache2/bin/apachectl stop
/usr/local/nginx/sbin/nginx # 启动nginx
ps aux |grep nginx
netstat lnp
# 打开nginx默认的配置文件
vim /usr/local/nginx/conf/nginx.conf # 打开下面的内容
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
cat /usr/local/nginx/html/index.html
cd /usr/local/nginx/html/ #进入html目录
vi info.php # 然后编辑info文件
/usr/local/nginx/sbin/nginx -t # 检查是否有错
/usr/local/nginx/sbin/nginx -s reload # 重启后,在网址上输入IP,就可以解析php了
curl localhost
curl localhost/info.php
A:编写nginx启动脚本,并加入系统服务
vim /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
B:保存后,更改权限:
chmod 755 /etc/init.d/nginx
chkconfig --add nginx # 加到启动列表中
chkconfig nginx on # 开机启动,执行此条命令
C: nginx的启动,重启,停止,检测
service nginx start
service nginx restart
service nginx stop
service nginx configtest
D:更改nginx配置
vim /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;
}
cd /usr/local/nginx/conf
mkdir vhosts
cd vhosts
mkdir /tmp/1233
# 创建虚拟主机配置文件
vim default.conf # 增加内容如下
server
{
listen 80 default_server;
server_name localhost;
index index.html index.htm index.php;
root /tmp/1233;
deny all;
}
/usr/local/nginx/sbin/nginx -t # 检查配置文件是否有错
/etc/init.d/nginx reload # 加载配置文件
curl -x127.0.0.1:80 www.baidu.com
# 创建新的虚拟主机配置文件
vim 111.conf # 增加内容如下
server
{
listen 80;
server_name 111.com;
index index.html index.htm index.php;
root /data/www;
location ~ \.php$ {
include fastcgi_params;
# fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
/usr/local/nginx/sbin/nginx -t # 检查配置文件是否有错
/etc/init.d/nginx reload # 加载配置文件
curl -x127.0.0.1:80 111.com/forum.php -I
/usr/local/php/etc/php-fpm.conf # 管理php-fpm相关服务的配置
/usr/local/php/etc/php.ini # php的全局配置
> /usr/local/php/etc/php-fpm.conf # 清空所有内容,编辑配置文件前要把旧的内容全部清除
vim /usr/local/php/etc/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
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
slowlog= /tmp/www slow.log # 可以排查网络慢的原因,优化网速
request_slowlog_timeout = 1 # 只要执行这个脚本的时间超过1s,就要记录slow.log
php_admin_value[open_basedir]=/data/www/:/tmp/ # 针对不同的域名进行不同的限制
[www1]
listen = /tmp/www1.sock
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
# 检测是否有错
usr/local/php/sbin/php-fpm -t
# 重启
/etc/init.d/php-fpm restart
ps aux |grep fpm.conf # 查看进程,默认有20个pool
ls /usr/local/nginx/conf/vhosts # 查看多个域名
cat /usr/local/nginx/conf/vhosts/111.conf # 查看指定的域名
cd /usr/local/nginx/conf/vhosts/
vim test.conf # 增加内容如下:
server
{
listen 80;
server_name www.test.com;
index index.html index.htm index.php;
root /data/www;
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
# 检查配置文件是否有错
/usr/local/nginx/sbin/nginx -t
# 重启nginx
/usr/local/nginx/sbin/nginx -s reload
etc/init.d/nginx reload
# 在主配置文件里找到错误日志
vim ../nginx.conf
# 查看错误日志,会发现/tmp/www.sock此文件权限被拒,所以网业会出现502
cat /usr/local/nginx/logs/nginx_error.log #
ls -l /tmp/www.sock
ps aux |grep nginx
# 编辑php配置文件,
vim /usr/local/php/etc/php-fpm.conf #指定监听的用户与组如下
[www]
listen = /tmp/www.sock
user = php-fpm
group = php-fpm
listen.owner = nobody
listen.group = nobody
# 检测配置文件是否有错
/usr/local/php/sbin/php-fpm -t
# 重启
/etc/init.d/php-fpm restart
cd /usr/local/nginx/conf/vhosts/
htpasswd -c /usr/local/nginx/conf/.htpasswd melody # 增加加密新用户
cat /usr/local/nginx/conf/.htpasswd
htpasswd /usr/local/nginx/conf/.htpasswd melody1 # 增加另一个加密新用户
vim test.conf # 增加内容如下:
server
{
listen 80;
server_name www.test.com
index index.html index.htm index.php;
root /data/www;
location ~ .*admin\.php$ {
auth_basic "aminglinux auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
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_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
# 检测配置文件是否有错
/usr/local/nginx/sbin/nginx -t
# 重启
/etc/init.d/nginx reload
# 用curl检测是否解析成功
curl -x127.0.0.1:80 www.test.com/admin.php
curl -x127.0.0.1:80 -umelody:123456 www.test.com/admin.php # 解析带密码的
# 编辑test.conf,来设域名的跳转
vim test.conf # 增加内容如下
listen 80;
server_name www.test.com www.aaa.com www.bbb.com;
if ($host != 'www.test.com')
{
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
# 检查nginx的配置文件是否正确
/usr/local/nginx/sbin/nginx -t
# 重启nginx
/usr/local/nginx/sbin/nginx -s reload
# 用curl检测网址是否生效
curl -x127.0.0.1:80 www.aaa.com/adajaklal -I # 301跳转为www.test.com
curl -x127.0.0.1:80 www.bbb.com/adajaklal -I # 301跳转为www.test.com
curl -x127.0.0.1:80 www.test.com/adajaklal -I # 404没有找到,因为网址不对
http://ask.apelearn.com/question/4840 # nginx的301与302如何配置的网址
flag标记可以用以下几种格式:
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
# 编辑主配置文件,
vim /usr/local/nginx/conf/nginx.conf 定义日志格式,把日志名称改成melody
log_format melody '$remote_addr $http_x_forwarded_for [$time_local]' # 远程IP,代理IP
# melody格式的日志如何用
vim test.conf # 进入此配置文件
access_log /tmp/access.log melody; # 首先指定目录位置
# 限制gif|jpg|jpeg|png|bmp|swf这些日志不去记录
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
access_log off;
}
location ~ (static|cache)
{
access_log off;
}
# 检查nginx的配置文件是否正确
/usr/local/nginx/sbin/nginx -t
# 重启nginx
/usr/local/nginx/sbin/nginx -s reload
# 在刷新前先把旧的日志内容清空
> /tmp/access.log
# 查看日志内容
cat /tmp/access.log
# 进入此目录下
cd /usr/local/nginx/conf/vhosts
# 编辑配置文件
vim /usr/local/sbin/nginx_logrotate.sh # 增加日志切割内容如下
#!/bin/bash
d=`date -d "-1 day" +%F`
[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log
mv /tmp/access.log /tmp/nginx_log/$d.log
/etc/init.d/nginx reload > /dev/null
cd /tmp/nginx_log/
gzip -f $d.log
# 执行日志文件
sh -x /usr/local/sbin/nginx_logrotate.sh
# 查看日志文件
ls /tmp/access.log
cat /tmp/access.log
ls /tmp/nginx_log/
# 进入此目录下
cd /usr/local/nginx/conf/vhosts/
# 编辑配置文件
vim test.conf # 增加静态文件过期时间内容如下
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
access_log off;
expires 15d;
}
location ~ \.(js|css)
{
access_log off;
expires 2h;
}
# 检查nginx的配置文件是否正确
/usr/local/nginx/sbin/nginx -t
# 重启nginx
/usr/local/nginx/sbin/nginx -s reload
# 进入此目录下
cd /usr/local/nginx/conf/vhosts/
# 编辑配置文件
vim test.conf # 增加防盗链内容如下
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|gz|bz2)$
{
access_log off;
expires 15d;
valid_referers none blocked *.test.com *.aaa.com;
if ($invalid_referer)
{
return 403;
}
# 检查nginx的配置文件是否正确
/usr/local/nginx/sbin/nginx -t
# 重启
/usr/local/nginx/sbin/nginx -s reload
# 用curl检查 (-e显示referer)
curl -e "http://www.test.com/111" -I -x127.0.0.1:80 'http:www.test.com/static/image/smiley/default/kiss.gif'
# 进入此目录下
cd /usr/local/nginx/conf/vhosts/
# 编辑配置文件
vim test.conf # 增加内容如下
# 白名单允许指定的IP访问,其它的拒绝
location ~ .*admin\.php$ {
allow 127.0.0.1; # 白名单格式
deny all;
# 黑名单拒绝指定的IP访问
access_log /tmp/access.log melody;
deny 127.0.0.1;
deny 192.168.31.0/24;
# 检查nginx的配置文件是否正确
/usr/local/nginx/sbin/nginx -t
# 重启
/usr/local/nginx/sbin/nginx -s reload
# 用curl检查
curl -x127.0.0.1:80 www.test.com/forum.php -I
curl -x192.168.31.127:80 www.test.com/forum.php -I
# 进入此目录下
cd /usr/local/nginx/conf/vhosts/
# 编辑配置文件
vim test.conf # 增加禁止指定user_agent的内容如下
if ($http_user_agent ~* 'curl|baidu|111') # ~*同时使用,代表不区别大小写
{
return 403;
}
# 检查nginx的配置文件是否正确
/usr/local/nginx/sbin/nginx -t
# 重启
/usr/local/nginx/sbin/nginx -s reload
# 用curl检查
curl -A "fkl" -x192.168.31.127:80 www.test.com/forum.php -I # 结果200,因为没有禁止的user_agent
curl -A "111a" -x192.168.31.127:80 www.test.com/forum.php -I # 结果403,因为有禁止的user_agent
# 进入此目录下
cd /usr/local/nginx/conf/vhosts/
# 配置单个百度代理
vim proxy.conf # 增加内容如下
server {
listen 80;
server_name www.baidu.com;
location / {
proxy_pass http://183.232.231.172/; #这里百度的IP是什么?需要PING一下
#proxy_set_header Host $host;
}
}
# PING出百度的IP
ping www.baidu.com
vim /etc/hosts # 删除之前写的百度IP
ping www.baidu.com
# 检查nginx的配置文件是否正确
/usr/local/nginx/sbin/nginx -t
# 重启
/usr/local/nginx/sbin/nginx -s reload
# 用curl检查
curl -x127.0.0.1:80 www.baidu.com # 把百度的IP指向本机,然后再去访问百度
# 使用一个域名对应多个IP,相当于负载均衡(多个机器访问百度)
dig www.baidu.com # 探测你的域名解析到了哪些IP
yum install bind* # 安装
#配置多个IP百度代理
vim proxy.conf # 增加内容如下
upstream melody{
server 183.232.231.172:80;
server 183.232.231.173:80;
}
server {
listen 80;
server_name www.baidu.com;
location / {
proxy_pass http://melody/;
proxy_set_header Host $host;
}
}
# 检查nginx的配置文件是否正确
/usr/local/nginx/sbin/nginx -t
# 重启
/usr/local/nginx/sbin/nginx -s reload
# 用curl检查
curl -x127.0.0.1:80 www.baidu.com # 把百度的IP指向本机,然后再去访问百度