nics 发表于 2015-8-21 11:33:10

LNMP环境的搭建

引言


因为要在内网搭建模拟测试环境,所以这里记录一下吧,以后也方便.所谓的LNMP即ubuntu + nginx + mysql + php的标配,其中nginx + fpm取代了apche + mod_php



ssh服务器设置

修改端口号,防止被轻易攻击


Port****
禁止root用户登陆




PermitRootLogin no
只允许公私钥认证的方式登陆,防止口令暴力破解




RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      %h/.ssh/authorized_keys
PasswordAuthentication no
修改密钥长度




ServerKeyBits 2048

  



修改语言环境变量

1.修改local配置文件


sudo vim /etc/default/locale
LANG=zh_CN.UTF-8
LANGUAGE=zh_CN:en_US:en
LC_CTYPE="zh_CN.UTF-8"
LC_NUMERIC=zh_CN.UTF-8
LC_TIME=zh_CN.UTF-8
LC_COLLATE="zh_CN.UTF-8"
LC_MONETARY=zh_CN.UTF-8
LC_MESSAGES="zh_CN.UTF-8"
LC_PAPER=zh_CN.UTF-8
LC_NAME=zh_CN.UTF-8
LC_ADDRESS=zh_CN.UTF-8
LC_TELEPHONE=zh_CN.UTF-8
LC_MEASUREMENT=zh_CN.UTF-8
LC_IDENTIFICATION=zh_CN.UTF-8
LC_ALL=
2.导入修改后配置


source /etc/default/locale

安装php&&php扩展



预安装软件


apt-get install make gcc g++ automake libtool libmysqlclient18 libxml2-dev libexpat1-dev mysql-client

1.安装php和fpm

sudo apt-get install php5 php5-fpm

2.安装memcache扩展

sudo apt-get install php5-memcache php5-memcached

3.安装mysql扩展

sudo apt-get install php5-mysql

4.安装http扩展

sudo apt-get install libcurl4-openssl-dev
sudo apt-get install libmagic-dev
sudo pecl install pecl_http具体使用见之前文章



5.安装oauth扩展

sudo apt-get install libpcre3-dev
sudo pecl install oauth(需要perl-compatible regular expression library)

6.安装gd扩展

sudo apt-get install php5-gd

7.安装redis扩展

安装phpredis扩展,点击打开链接






rsync+shell部署文件和目录


来一个部署的脚本吧,大家可以参考一下里面的思想,主要是灾备

#!/bin/bash -
#1.参数配置
sour_dir="/home/wangzhengyi"
file_name="test1"
datetime=`date -d '+0 days' +%Y-%m-%d-%H-%I`
dest_dir="/srv/www/test2"
back_dir="/tmp"
if [ ! -d $dest_dir ];then
mkdir -p $dest_dir
chown -R www-data.www-data $dest_dir
fi

#2.备份源目录
if [ -d $dest_dir/$file_name ]
then
cp -r$dest_dir/$file_name $back_dir/$file_name$datetime
fi

#3.更新目录
rsync -rvz --delete $sour_dir/$file_name/ $dest_dir/$file_name/
if [ $? = 0 ]
then
chown -R www-data.www-data $dest_dir/$file_name
fi

搭建nginx虚拟主机




共享一下我的nginx.conf配置


user www-data;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
worker_rlimit_nofile 204800;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 768;
# multi_accept on;
}
http {
include       /etc/nginx/mime.types;
default_typeapplication/octet-stream;
charsetutf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 2k;
large_client_header_buffers 4 4k;
client_max_body_size 8m;
log_formatmain'$server_name$remote_addr$remote_user[$time_local]"$request"'
'$status$body_bytes_sent"$http_referer"'
'"$http_user_agent""$http_x_forwarded_for"';
access_log/var/log/nginx/access.logmain;
error_log /var/log/nginx/error.log;
sendfile      on;
tcp_nopush   on;
keepalive_timeout60;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
tcp_nodelay on;
gzipon;
include /etc/nginx/conf.d/*.conf;
}


根据需要搭建虚拟主机


这里由于隐私大家网上搜一下就行,而且我之前有文章写过搭建nginx+fpm虚拟主机的方法



修改/etc/hosts文件


修改/etc/hosts文件,保持通信双方的相同测试环境,具体见链接http://blog.iyunv.com/zinss26914/article/details/8442853
部署nginx&&fpm按天分割脚本


http://blog.iyunv.com/zinss26914/article/details/8256144,之前写的一篇博客,crontab定时运行即可




部署nginx定期迁移脚本


http://blog.iyunv.com/zinss26914/article/details/8425838,crontab定期运行即可,程序里判断每两周执行一次




搭建mysql
  



安装mysql


sudo apt-get installmysql-server
配置mysql默认编码


http://blog.iyunv.com/zinss26914/article/details/8035902

mysql远程连接




[*]修改bind-address = server_ip
[*]授权远程访问 grant all privileges on*.* to 'user'@'remote_ip'identified by 'password';
[*]刷新权限使生效 flush privileges

共享mysql导出指定数据库上传到指定服务器的脚本


#!/bin/bash -
#1.数据库备份参数配置
mysqlhost="your_mysql_ip"
mysqluser="****"
mysqlpasswd="****"
mysqldb="db_name"
tablenames=("table_name1")
dbtime=`date -d '+0 days' +%Y%m%d`
export_directory="/home/user/sql/$dbtime"
remote_dir="/home/user/sql/back/"
ip_array=("192.168.1.***")
#定义数据备份目录
if [ ! -d $export_directory ]
then
mkdir -p $export_directory
fi
cd $export_directory

#2.mysqldump备份数据库数据
for tablename in ${tablenames
[*]}
do
#定义数据备份文件名
dbfile="$tablename-$dbtime.sql"
#导出mysql数据
mysqldump -h$mysqlhost -u$mysqluser -p$mysqlpasswd $mysqldb $tablename >$dbfile
#采用tar压缩备份存储的sql文件
tar -zcPpf $tablename-$dbtime.tar.gz$dbfile
#删除大容量文件
if [ $? = 0 ]
then
rm $dbfile
fi
#同步数据库备份到指定服务器
for ip in ${ip_array
[*]}
do
port="your_port"
scp -P $port $tablename-$dbtime.tar.gz username@$ip:$remote_dir
done
done

搭建redis




[*]ubuntu的ppa源同步redis版本与线上服务器保持一直,否则不能兼容rdb文件
[*]增加redis数据库连接密码
[*]修改bind的ip地址,允许远程访问



搭建coreseek全文搜索引擎


参考官方文档搭建即可,http://www.coreseek.cn/products-install/install_on_macosx/
页: [1]
查看完整版本: LNMP环境的搭建