1 2 3 4 5 6 7 8 9 10 11 12
| 1二、lnmp+tomcat环境搭建
2
32 搭建lnmp、tomcat+jdk环境
43 三个站点分别为:discuz论坛、dedecms企业网站以及zrlog博客
54 由于机器有限,尽可能地把三个站点放到同一台服务器上,然后做负载均衡集群,要求所有站点域名解析到一个ip上,也就是说只有一个出口ip
65 需要共享静态文件,比如discuz需要共享的目录是 data/attachment,dedecms需要共享upload(具体目录,你可以先上传一个图片,查看图片所在目录)
76 设计合理的目录、文件权限,比如discuz的data目录需要给php-fpm进程用户可写权限,其他目录不用写的就不要给写权限(目录755,文件644,属主属组root)
89 php-fpm服务要求设置慢执行日志,超时时间为2s,并做日志切割,日志保留一月
910 所有站点都需要配置访问日志,并做日志切割,要求静态文件日志不做记录,日志保留一月
1018 给三个站点的后台做二次认证,增加安全性
11
12 |
1. LNMP环境搭建
1.1 安装mysql,这是因为php需要用到mysql的驱动库,所以只需要安装即可,不需要进行配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| 1cd /usr/local/src/
2yum install -y epel-release wget perl-Module-Install.noarch libaio*
3wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
4tar -zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
5mv mysql-5.6.35-linux-glibc2.5-x86_64 ../mysql
6cd /usr/local/mysql
7mkdir /data/
8useradd mysql
9./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
10cp support-files/mysql.server /etc/init.d/mysqld
11编辑启动脚本
12vim /etc/init.d/mysqld
13basedir=/usr/local/mysql #定义软件安装目录
14datadir=/data/mysql #定义数据目录
15chown 755 /etc/init.d/mysqld #设置权限为755 默认就是755
16chkconfig --add mysqld #加入到系统启动服务里面去(开机启动)
17同样可以使用启动
18/etc/init.d/mysqld resart
19service mysqld start
20/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --use=mysql --datadir=/data/mysql &
21#--defaults-file=/etc/my.cnf 指定配置文件
22#--use=mysql 指定启动用户
23#--datadir=/data/mysql 指定数据文件
24#& 丢到后台启动
25ps aux |grep mysql #查看进程
26netstat -lntp #查看监听端口
27mysql配置文件
28
29#使用本机自带的配置文件/etc/my.cnf更改为以下内容
30[root@test mysql]# cat /etc/my.cnf
31[mysqld]
32datadir=/data/mysql
33socket=/tmp/mysql.sock
34# Disabling symbolic-links is recommended to prevent assorted security risks
35symbolic-links=0
36# Settings user and group are ignored when systemd is used.
37# If you need to run mysqld under a different user or group,
38# customize your systemd unit file for mariadb according to the
39# instructions in http://fedoraproject.org/wiki/Systemd
40
41[mysqld_safe]
42#log-error=/var/log/mariadb/mariadb.log
43#pid-file=/var/run/mariadb/mariadb.pid
44
45#
46# include all files from the config directory
47#
48#!includedir /etc/my.cnf.d
49
50 |
1.2 安装PHP-fpm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 1cd /usr/local/src/
2yum -y install epel-release wget gcc gcc-c++ libmcrypt-devel libmcrypt libcurl-devel libxml2-devel openssl-devel bzip2-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel
3wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
4tar -zxvf php-5.6.30.tar.gz
5cd php-5.6.30/
6./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-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 --with-pear --with-curl --with-openssl
7make && make install
8cp php.ini-production /usr/local/php-fpm/etc/php.ini
9 vi /usr/local/php/etc/php-fpm.conf //写入如下内容
10 cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
11 chmod 755 /etc/init.d/php-fpm
12 chkconfig --add php-fpm
13 chkconfig php-fpm on
14 service php-fpm start
15 ps aux |grep php-fpm
16
17 编辑配置文件php-fpm.conf
18[global]
19pid = /usr/local/php-fpm/var/run/php-fpm.pid
20error_log = /usr/local/php-fpm/var/log/php-fpm.log
21[www]
22listen = /tmp/php-fcgi.sock
23listen.mode = 666
24user = php-fpm
25group = php-fpm
26pm = dynamic
27pm.max_children = 50
28pm.start_servers = 20
29pm.min_spare_servers = 5
30pm.max_spare_servers = 35
31pm.max_requests = 500
32rlimit_files = 1024
33
34 |
1.3 安装nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| 1cd /usr/local/src
2 wget http://nginx.org/download/nginx-1.12.1.tar.gz
3 tar zxf nginx-1.12.1.tar.gz
4 ./configure --prefix=/usr/local/nginx
5 make && make install
6 vim /etc/init.d/nginx
7chmod 755 /etc/init.d/nginx
8 chkconfig --add nginx
9 chkconfig nginx on
10 cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak
11 vim nginx.conf //写入如下内容
12 /usr/local/nginx/sbin/nginx -t
13 /etc/init.d/nginx start
14 netstat -lntp |grep 80
15nginx.conf
16
17user nobody nobody;
18worker_processes 2;
19error_log /usr/local/nginx/logs/nginx_error.log crit;
20pid /usr/local/nginx/logs/nginx.pid;
21worker_rlimit_nofile 51200;
22
23events
24{
25 use epoll;
26 worker_connections 6000;
27}
28
29http
30{
31 include mime.types;
32 default_type application/octet-stream;
33 server_names_hash_bucket_size 3526;
34 server_names_hash_max_size 4096;
35 log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
36 ' $host "$request_uri" $status'
37 ' "$http_referer" "$http_user_agent"';
38 sendfile on;
39 tcp_nopush on;
40 keepalive_timeout 30;
41 client_header_timeout 3m;
42 client_body_timeout 3m;
43 send_timeout 3m;
44 connection_pool_size 256;
45 client_header_buffer_size 1k;
46 large_client_header_buffers 8 4k;
47 request_pool_size 4k;
48 output_buffers 4 32k;
49 postpone_output 1460;
50 client_max_body_size 10m;
51 client_body_buffer_size 256k;
52 client_body_temp_path /usr/local/nginx/client_body_temp;
53 proxy_temp_path /usr/local/nginx/proxy_temp;
54 fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
55 fastcgi_intercept_errors on;
56 tcp_nodelay on;
57 gzip on;
58 gzip_min_length 1k;
59 gzip_buffers 4 8k;
60 gzip_comp_level 5;
61 gzip_http_version 1.1;
62 gzip_types text/plain application/x-javascript text/css text/htm
63 application/xml;
64
65 server
66 {
67 listen 80;
68 server_name localhost;
69 index index.html index.htm index.php;
70 root /usr/local/nginx/html;
71
72 location ~ \.php$
73 {
74 include fastcgi_params;
75 fastcgi_pass unix:/tmp/php-fcgi.sock;
76 fastcgi_index index.php;
77 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
78 }
79 }
80}
81开机启动脚本
82
83#!/bin/bash
84# chkconfig: - 30 21
85# description: http service.
86# Source Function Library
87. /etc/init.d/functions
88# Nginx Settings
89
90NGINX_SBIN="/usr/local/nginx/sbin/nginx"
91NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
92NGINX_PID="/usr/local/nginx/logs/nginx.pid"
93RETVAL=0
94prog="Nginx"
95
96start()
97{
98 echo -n $"Starting $prog: "
99 mkdir -p /dev/shm/nginx_temp
100 daemon $NGINX_SBIN -c $NGINX_CONF
101 RETVAL=$?
102 echo
103 return $RETVAL
104}
105
106stop()
107{
108 echo -n $"Stopping $prog: "
109 killproc -p $NGINX_PID $NGINX_SBIN -TERM
110 rm -rf /dev/shm/nginx_temp
111 RETVAL=$?
112 echo
113 return $RETVAL
114}
115
116reload()
117{
118 echo -n $"Reloading $prog: "
119 killproc -p $NGINX_PID $NGINX_SBIN -HUP
120 RETVAL=$?
121 echo
122 return $RETVAL
123}
124
125restart()
126{
127 stop
128 start
129}
130
131configtest()
132{
133 $NGINX_SBIN -c $NGINX_CONF -t
134 return 0
135}
136
137case "$1" in
138 start)
139 start
140 ;;
141 stop)
142 stop
143 ;;
144 reload)
145 reload
146 ;;
147 restart)
148 restart
149 ;;
150 configtest)
151 configtest
152 ;;
153 *)
154 echo $"Usage: $0 {start|stop|reload|restart|configtest}"
155 RETVAL=1
156esac
157
158exit $RETVAL
159
160 |
配置nginx默认虚拟主机,方便以后可以直接使用: 首先把nginx配置文件里定义的虚拟主机删除:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| 1vim /usr/local/nginx/conf/nginx.conf
2#添加以下内容
3include vhost/*.conf;`
4
5mkdir /usr/local/nginx/conf/vhost
6#创建一个default.conf文件:
7vim default.conf
8添加以下内容:
9server
10{
11 listen 80 default_server;
12 server_name aaa.com;
13 index index.html index.htm index.php;
14 root /data/wwwroot/default;
15
16 location ~ \.php$
17 {
18 include fastcgi_params;
19 fastcgi_pass unix:/tmp/php-fcgi.sock;
20 fastcgi_index index.php;
21 fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
22 }
23}
24
25创建默认站点目录:
26mkdir -p /data/wwwroot/default/
27
28 |
1.4 搭建tomcat+jdk
安装jdk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| 1jdk版本1.6,1.7,1.8
2官网下载地址http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
3下载jdk8,放到/usr/local/src/目录下
4tarzxvfjdk-8u144-linux-x64.tar.gz
5mvjdk1.8.0_144/usr/local/jdk1.8
6vi/etc/profile//最后面增加
7JAVA_HOME=/usr/local/jdk1.8/
8JAVA_BIN=/usr/local/jdk1.8/bin
9JRE_HOME=/usr/local/jdk1.8/jre
10PATH=$PATH:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin
11CLASSPATH=/usr/local/jdk1.8/jre/lib:/usr/local/jdk1.8/lib:/usr/local/jdk1.8/jre/lib/charsets.jar
12source/etc/profile
13java-version
14
15 |
安装Tomcat
1 2 3 4 5 6 7 8 9 10 11 12 13
| 1cd/usr/local/src
2wgethttp://apache.fayea.com/tomcat/tomcat-8/v8.5.20/bin/apache-tomcat-8.5.20.tar.gz
3tarzxvfapache-tomcat-8.5.20.tar.gz
4mvapache-tomcat-8.5.20/usr/local/tomcat
5/usr/local/tomcat/bin/startup.sh
6psaux|greptomcat
7netstat-lntp|grepjava
8三个端口8080为提供web服务的端口,8005为管理端口,8009端口为第三方服务调用的端口,比如httpd和Tomcat结合时会用到
9tomcat不支持restart
10启动命令:/usr/local/tomcat/bin/startup.sh
11停止命令:/usr/local/tomcat/bin/shutdown.sh
12
13 |
配置虚拟主机
1 2 3 4 5 6 7 8
| 1vim/usr/local/tomcat/conf/server.xml
2<Hostname="www.zrlog.com"appBase=""
3unpackWARs="true"autoDeploy="true"
4xmlValidation="false"xmlNamespaceAware="false">
5<Contextpath=""docBase="/data/wwwroot/zrlog.com/"debug="0"reloadable="true"crossContext="true"/>
6</Host>
7
8 |
1.6 搭建3个站点
搭建dedecms
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 1创建并编辑虚拟主机配置文件
2
3vim /usr/local/nginx/conf/vhost/dedecms.com.conf
4server
5{
6 listen 80;
7 server_name www.dedecms.com;
8 index index.html index.htm index.php;
9 root /data/wwwroot/dedecms.com;
10
11 location ~ \.php$
12 {
13 include fastcgi_params;
14 fastcgi_pass unix:/tmp/php-fcgi.sock;
15 fastcgi_index index.php;
16 fastcgi_param SCRIPT_FILENAME /data/wwwroot/dedecms.com$fastcgi_script_name;
17 }
18}
19
20创建数据目录
21mkdir /data/wwwroot/dedecms.com/
22
23
24下载dedecms主程序
25[root@localhost ~]# cd /usr/local/src/
26[root@localhost /usr/local/src]# wget http://updatenew.dedecms.com/base-v57/package/DedeCMS-V5.7-UTF8-SP2.tar.gz
27[root@localhost /usr/local/src]# tar -zxvf DedeCMS-V5.7-UTF8-SP2.tar.gz
28[root@localhost /usr/local/src]# mv DedeCMS-V5.7-UTF8-SP2/uploads/* /data/wwwroot/dedecms.com/
29[root@localhost /data/wwwroot/dedecms.com]# ls
30a dede favicon.ico include install member robots.txt tags.php uploads
31data images index.php m plus special templets
32
33
34 |
搭建discuz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| 1创建并编辑虚拟主机配置文件
2vim /usr/local/nginx/conf/vhost/discuz.com.conf
3server
4{
5 listen 80;
6 server_name www.discuz.com;
7 index index.html index.htm index.php;
8 root /data/wwwroot/discuz.com;
9
10 location ~ \.php$
11 {
12 include fastcgi_params;
13 fastcgi_pass unix:/tmp/php-fcgi.sock;
14 fastcgi_index index.php;
15 fastcgi_param SCRIPT_FILENAME /data/wwwroot/discuz.com$fastcgi_script_name;
16 }
17}
18
19
20创建数据目录
21mkdir /data/wwwroot/discuz.com/
22下载discuz主程序
23
24[root@localhost ~]# cd /usr/local/src/
25[root@localhost /usr/local/src]# wget http://download.comsenz.com/DiscuzX/3.3/Discuz_X3.3_SC_UTF8.zip
26[root@localhost /usr/local/src]# unzip Discuz_X3.3_SC_UTF8.zip
27[root@localhost /usr/local/src]# mv upload/* /data/wwwroot/discuz.com/
28[root@localhost /usr/local/src]# ls /data/wwwroot/discuz.com/
29admin.php config data home.php misc.php search.php uc_client
30api connect.php favicon.ico index.php plugin.php source uc_server
31api.php cp.php forum.php install portal.php static userapp.php
32archiver crossdomain.xml group.php member.php robots.txt template
33
34 |
搭建zrlog站点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 1编辑虚拟主机配置文件
2vim /usr/local/tomcat/conf/server.xml
3<Host name="www.zrlog.com" appBase=""
4 unpackWARs= "true" autoDeploy="true"
5 xmlValidation="false" xmlNamespaceAware="false">
6 <Context path="" docBase="/data/wwwroot/zrlog.com/" debug="0" reloadable="true" crossContext="true"/>
7 </Host>
8
9
10创建数据目录
11mkdir /data/wwwroot/zrlog.com/
12
13
14下载zrlog主程序
15[root@localhost ~]$ cd /usr/local/src/
16[root@localhost /usr/local/src]$ wget http://dl.zrlog.com/release/zrlog-1.7.1-baaecb9-release.war
17[root@localhost /usr/local/src]$ unzip zrlog-1.7.1-baaecb9-release.war
18[root@localhost /usr/local/src]$ unzip zrlog-1.7.1-baaecb9-release.war -d /data/wwwroot/zrlog.com
19[root@localhost /usr/local/src]$ cd /data/wwwroot/zrlog.com
20[root@localhost /data/wwwroot/zrlog.com]$ ls
21admin assets error favicon.ico include install META-INF WEB-INF
22
23 |
2. 给三个站点的后台做二次认证,增加安全性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| 1首先安装httpd:
2yum install -y httpd
3
4
5然后使用httpd里的htpasswd 命令去生成一个用户密码文件:
6[root@localhost ~]$ htpasswd -c /usr/local/nginx/conf/htpasswd admin
7New password:
8Re-type new password:
9Adding password for user admin
10
11
12生成完成后cat一下htpasswd 文件可以看到如下内容:
13[root@localhost ~]$ cat /usr/local/nginx/conf/htpasswd
14admin:$apr1$bwCvGuw9$71cc8LnzGEG0AEiSSB1uO.
15如果还需要再次添加用户的话就不需要加上-c选项了,加上-c选项会覆盖原来的htpasswd 文件。
16
17
18
19编辑discuz的主机配置文件:
20[root@localhost ~]$ vim /usr/local/nginx/conf/vhost/discuz.com.conf
21
22 ## 添加以下内容,要记得添加在 location ~ \.php$ 上面
23 location ~ admin.php
24 {
25 auth_basic "Auth";
26 auth_basic_user_file /usr/local/nginx/conf/htpasswd; # 密码文件路径
27 }
28
29重新加载nginx的配置文件:
30/usr/local/nginx/sbin/nginx -t
31/usr/local/nginx/sbin/nginx -s reload
32
33然后使用curl访问看看是否需要认证,结果如下则没问题:
34
35
36接配置ddecms,同样的也是需要编辑主机配置文件:
37[root@localhost ~]$ vim /usr/local/nginx/conf/vhost/dedecms.com.conf
38
39 ## 配置内容如下:
40 location /dede/
41 {
42 auth_basic "Auth";
43 auth_basic_user_file /usr/local/nginx/conf/htpasswd; # 密码文件路径
44 }
45重新加载nginx
46
47
48
49
50最后是zrlog,编辑nginx的反向代理配置文件:
51[root@localhost ~]$ vim /usr/local/nginx/conf/vhost/zrlog.com.conf
52 ## 在location / 的上面添加以下这段内容:
53 location /admin/
54 {
55 auth_basic "Auth";
56 auth_basic_user_file /usr/local/nginx/conf/htpasswd;
57 proxy_pass http://zrlog_com/admin/;
58 proxy_set_header Host $host;
59 proxy_set_header X-Real-IP $remote_addr;
60 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
61 }
62
63 |
3. php-fpm服务要求设置慢执行日志,超时时间为2s,并做日志切割,日志保留一月
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| 1编辑php-fpm的配置文件,并如下添加内容:
2vim /usr/local/php-fpm/etc/php-fpm.conf
3request_slowlog_timeout = 2 # 定义超过2秒就要记录日志
4slowlog = /usr/local/php-fpm/var/log/www-slow.log # 定义日志文件的存放路径
5
6
7修改完成,测试一下配置文件的语法,并重新加载配置文件:
8/usr/local/php-fpm/sbin/php-fpm -t
9[18-Dec-2017 01:31:44] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
10
11/etc/init.d/php-fpm reload
12Reload service php-fpm done
13
14
15
16接下来我们需要模拟一个慢执行的php,需要写一个php脚本,以便验证能够记录慢执行日志:
17vim /data/wwwroot/default/sleep.php
18<?php
19 echo "test slow log";
20 sleep(3);
21 echo "done";
22?>
23
24
25使用curl对这个脚本进行访问:
26
27curl -x127.0.0.1:80 localhost/sleep.php
28test slow log
29done
30
31
32
33访问完之后查看慢执行日志,这个日志告诉了我们以下信息:
34 cat /usr/local/php-fpm/var/log/www-slow.log
35[18-Dec-2017 01:36:32] [pool www] pid 2653
36script_filename = /data/wwwroot/default/sleep.php # 哪个脚本导致的慢执行
37[0x00007f8fc62ca270] sleep() /data/wwwroot/default/sleep.php:3 # 以及是脚本中的哪行语句导致的
38
39
40
41测试完可以生成日志后,开始做日志切割,Nginx不像Apache那样有自带的日志切割工具,所以只能借助系统的切割工具或者自己写一个简单的切割脚本,在这里则介绍一下如何写一个日志切割的脚本,如下:
42vim /usr/local/sbin/nginx_log_rotate.sh
43#! /bin/bash
44d=`date -d "-1 day" +%Y%m%d`
45logdir="/usr/local/php-fpm/var/log/"
46nginx_pid="/usr/local/nginx/logs/nginx.pid"
47cd $logdir
48for log in `ls *.log`
49do
50 mv $log $log-$d
51done
52/bin/kill -HUP `cat $nginx_pid`
53
54
55
56保存退出后,我们可以使用sh -x 命令来查看这个脚本的执行过程:
57sh -x /usr/local/sbin/nginx_log_rotate.sh
58++ date -d '-1 day' +%Y%m%d
59+ d=20171217
60+ logdir=/usr/local/php-fpm/var/log/
61+ nginx_pid=/usr/local/nginx/logs/nginx.pid
62+ cd /usr/local/php-fpm/var/log/
63++ ls php-fpm.log www-slow.log
64+ for log in '`ls *.log`'
65+ mv php-fpm.log php-fpm.log-20171217
66+ for log in '`ls *.log`'
67+ mv www-slow.log www-slow.log-20171217
68++ cat /usr/local/nginx/logs/nginx.pid
69+ /bin/kill -HUP 23727
70
71
72写完脚本后,需要定期的自动执行日志切割,所以我们要设置一个任务计划:
73crontab -e
74
75## 增加以下内容,这是定义0点的时候执行这个脚本
760 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
77
78日志只保留一个月,还需要往crontab里添加以下这一行,每个月的1号就删除一次旧的日志文件:
79* * 1 * * /usr/bin/find /usr/local/php-fpm/var/log/ -name *.log.* -type f -mtime +30 |xargs rm
80
81 |
4. 所有站点都需要配置访问日志,并做日志切割,要求静态文件日志不做记录,日志保留一月
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| 1在nginx里,日志的格式可以在主配置文件里定义,编辑主配置文件:
2vim /usr/local/nginx/conf/nginx.conf
3
4搜索log_format,这一段就是用来定义日志格式的:
5log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
6 ' $host "$request_uri" $status'
7 ' "$http_referer" "$http_user_agent"';
8
9其中的combined_realip是日志的名称,这个名称可以自定义,但是你定义了什么名称,后面你操作日志的时候也要使用这个名称。就像你给一个人起名叫李四,你就得用李四这个名字去叫他干活。
10
11获取到日志名称后编辑站点的虚拟主机配置文件:
12
13
14
15vim /usr/local/nginx/conf/vhost/discuz.com.conf
16
17增加以下内容:
18access_log /data/wwwroot/discuz.com/data/log/discuz.com.log combined_realip;
19
20这里的combined_realip就是在nginx.conf中定义的日志格式名字。
21
22然后重新加载配置文件:
23
24 /usr/local/nginx/sbin/nginx -t
25nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
26nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
27 /usr/local/nginx/sbin/nginx -s reload
28
29
30静态文件不记录日志的配置如下:
31
32 vim /usr/local/nginx/conf/vhost/discuz.com.conf
33 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
34 {
35 expires 7d;
36 access_log off;
37 }
38 location ~ .*\.(js|css)$
39 {
40 expires 12h;
41 access_log off;
42 }
43配置完后重新加载配置文件:
44
45 /usr/local/nginx/sbin/nginx -t
46nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
47nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
48 /usr/local/nginx/sbin/nginx -s reload
49
50
51
52
53
54至于日志切割其实就修改一下之前那个脚本文件即可,把logdir变量定义的路径换成参数的形式,然后在定时任务计划里传递相应的路径即可:
55 vim /usr/local/sbin/nginx_log_rotate.sh
56#! /bin/bash
57d=`date -d "-1 day" +%Y%m%d`
58logdir="$1"
59nginx_pid="/usr/local/nginx/logs/nginx.pid"
60cd $logdir
61for log in `ls *.log`
62do
63 mv $log $log-$d
64done
65/bin/kill -HUP `cat $nginx_pid`
66
67
68测试一下脚本:
69 sh -x /usr/local/sbin/nginx_log_rotate.sh "/data/wwwroot/discuz.com/data/log/"
70++ date -d '-1 day' +%Y%m%d
71+ d=20171217
72+ logdir=/data/wwwroot/discuz.com/data/log/
73+ nginx_pid=/usr/local/nginx/logs/nginx.pid
74+ cd /data/wwwroot/discuz.com/data/log/
75++ ls discuz.com.log
76+ for log in '`ls *.log`'
77+ mv discuz.com.log discuz.com.log-20171217
78++ cat /usr/local/nginx/logs/nginx.pid
79+ /bin/kill -HUP 23727
80
81没问题之后修改任务计划内容如下:
82
83 crontab -e
840 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh "/usr/local/php-fpm/var/log/"
850 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh "/data/wwwroot/discuz.com/data/log/"
86* * 1 * * /usr/bin/find /usr/local/php-fpm/var/log/ -name *.log.* -type f -mtime +30 |xargs rm
87* * 1 * * /usr/bin/find /data/wwwroot/discuz.com/data/log/ -name *.log.* -type f -mtime +30 |xargs rm
88剩下的站点都是和以上步骤一样照葫芦画瓢即可,最后将文件都同步到其他web服务器上就可以了。
89
90
91zrlog由于是代理配置文件所以配置内容不太一样,以下是zrlog的整体配置内容:
92
93 cat /usr/local/nginx/conf/vhost/zrlog.com.conf
94upstream zrlog_com
95{
96 ip_hash;
97 server localhost:8080;
98}
99server
100{
101 listen 80;
102 server_name www.zrlog.com;
103
104 location /admin/
105 {
106 auth_basic "Auth";
107 auth_basic_user_file /usr/local/nginx/conf/htpasswd;
108 proxy_pass http://zrlog_com/admin/;
109 proxy_set_header Host $host;
110 proxy_set_header X-Real-IP $remote_addr;
111 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
112 }
113
114 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
115 {
116 expires 7d;
117 access_log off;
118 proxy_pass http://zrlog_com;
119 proxy_set_header Host $host;
120 proxy_set_header X-Real-IP $remote_addr;
121 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
122 }
123 location ~ .*\.(js|css)$
124 {
125 expires 12h;
126 access_log off;
127 proxy_pass http://zrlog_com;
128 proxy_set_header Host $host;
129 proxy_set_header X-Real-IP $remote_addr;
130 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
131 }
132
133 location /
134 {
135 proxy_pass http://zrlog_com;
136 proxy_set_header Host $host;
137 proxy_set_header X-Real-IP $remote_addr;
138 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
139 }
140 access_log /data/wwwroot/zrlog.com/logs/zrlog.com.log combined_realip;
141}
142
143 |
5. 设计合理的目录、文件权限,比如discuz的data目录需要给php-fpm进程用户可写权限,其他目录不用写的就不要给写权限(目录755,文件644,属主属组root)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| 1discuz的目录、文件权限之前在安装的时候分配好了,现在把install目录给删除即可:
2
3cd /data/wwwroot/discuz.com
4rm -rf install/
5
6
7然后设置dedecms的目录、文件权限,下面是dedecms官网的目录安全配置说明:
81、目录权限
9我们不建议用户把栏目目录设置在根目录, 原因是这样进行安全设置会十分的麻烦, 在默认的情况下,安装完成后,目录设置如下:
10(1) data、templets、uploads、a或5.3的html目录, 设置可读写,不可执行的权限;
11(2) 不需要专题的,建议删除 special 目录, 需要可以在生成HTML后,删除 special/index.php 然后把这目录设置为可读写,不可执行的权限;
12(3) include、member、plus、后台管理目录 设置为可执行脚本,可读,但不可写入(安装了附加模块的,book、ask、company、group 目录同样如此设置)。
13
142、其它需注意问题
15(1) 虽然对 install 目录已经进行了严格处理, 但为了安全起见,我们依然建议把它删除;
16(2) 不要对网站直接使用MySQL root用户的权限,给每个网站设置独立的MySQL用户帐号,许可权限为:
17
18代码如下 复制代码
19SELECT, INSERT , UPDATE , DELETE
20CREATE , DROP , INDEX , ALTER , CREATE TEMPORARY TABLES
21
22我尝试按照说明去修改权限结果出现网站无法访问的问题,于是实践过后发现只需要更改以下几个目录的权限即可:
23
24cd dedecms.com/
25chmod 766 ./uploads
26chmod 766 ./a
27chmod 755 ./plus
28chmod 644 data/common.inc.php
29rm -rf install/
30mv ./special/ /tmp/
31
32
33zrlog的就默认即可,因为默认都是755、644的权限。
34
35
36
37 |