lnmp搭建以及nginx日志分割


1.实验环境:

lnmp版本操作系统
nginx-1.18.0centos 7.4
php-7.1.9centos 7.4
mysql-5.7.12centos 7.4
boost_1_59_0.tar.gzcentos 7.4

搭建实验环境

mysql源码安装

[root@localhost ~]# wget --no-check-certificate https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
[root@localhost ~]# tar xf boost_1_59_0.tar.gz -C /usr/src && mkdir -p /usr/local/boost && cp -rf /usr/src/boost_1_59_0/* /usr/local/boost
[root@localhost ~]# wget http://dev.mysql.com/Downloads/MySQL-5.7/mysql-5.7.12.tar.gz
[root@localhost ~]# ls
mysql-5.7.12.tar.gz
[root@localhost ~]# yum -y install gcc gcc-c++ ncurses bison libgcrypt perl cmake ncurses-devel
[root@localhost ~]# tar xf mysql-5.7.12.tar.gz -C /usr/src/ && cd /usr/src/mysql-5.7.12/
[root@localhost mysql-5.7.12]# groupadd mysql
[root@localhost mysql-5.7.12]# useradd -r -g mysql mysql
[root@localhost mysql-5.7.12]# cmake  \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_USER=mysql \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DENABLE_DOWNLOADS=1 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost \
-DSYSCONFDIR=/etc && make -j `cat /proc/cpuinfo | grep processor | wc -l` && make install
[root@localhost ~]# ls /usr/local/mysql
bin  COPYING  data  docs  include  lib  man  mysql-test  README  share  support-files
[root@localhost ~]# chown -R mysql. /usr/local/mysql
[root@localhost ~]# cp /usr/src/mysql-5.7.12/support-files/my-default.cnf /etc/my.cnf
[root@localhost ~]# cp /usr/src/mysql-5.7.12/support-files/mysql.server /etc/init.d/
[root@localhost ~]# chmod +x /etc/init.d/mysql.server
[root@localhost ~]# cat <<EOF >>/usr/lib/systemd/system/mysqld.service
[Unit]
Description=mysqldapi
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/mysql/logs/mysqld.pid
ExecStart=/etc/init.d/mysql.server start
ExecReload=/etc/init.d/mysql.server restart
ExecStop=/etc/init.d/mysql.server stop
PrivateTmp=Flase

[Install]
WantedBy=multi-user.target
EOF
[root@localhost ~]# echo -e "export MYSQL_HOME=/usr/local/mysql \nexport PATH=\$PATH:\$MYSQL_HOME/bin" >>/etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin
[root@localhost ~]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@localhost ~]# cat <<EOF >/etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character_set_server=utf8
init_connect='SET NAMES utf8'
log-error=/usr/local/mysql/logs/mysqld.log
pid-file=/usr/local/mysql/logs/mysqld.pid
skip-name-resolve
EOF
[root@localhost ~]# mkdir /usr/local/mysql/logs
[root@localhost ~]# chown -R mysql. /usr/local/mysql/logs
[root@localhost ~]# chkconfig --add mysql.server
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl enable mysqld
 mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
   Active: active (running) since  2022-05-17 09:44:51 CST; 1 day 4h ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1009 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           ├─1028 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr...
           └─1141 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql ...

5 17 09:44:48 localhost.localdomain systemd[1]: Starting LSB: start an...
5 17 09:44:51 localhost.localdomain mysqld[1009]: Starting MySQL.. SUCC...
5 17 09:44:51 localhost.localdomain systemd[1]: Started LSB: start and...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# netstat -anput | grep  mysqld
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2885/mysqld
[root@localhost ~]# mysqladmin -uroot -p password "123123"
[root@localhost ~]# mysql -u root -p123123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1192
Server version: 5.6.36 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql>exit

nginx源码安装

[root@localhost ~]# wget http://www.nginx.org/download/nginx-1.18.0.tar.gz
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1039530 (1015K) [application/octet-stream]
正在保存至: “nginx-1.18.0.tar.gz”

100%[==================================>] 1,039,530   48.8KB/s 用时 23s    

2022-05-18 11:46:13 (43.9 KB/s) - 已保存 “nginx-1.18.0.tar.gz” [1039530/1039530])
[root@localhost ~]# ls
nginx-1.18.0.tar.gz
[root@localhost ~]# yum -y install zlib-devel pcre-devel openssl-devel
[root@localhost ~]# tar xf nginx-1.18.0.tar.gz -C /usr/src/ && cd /usr/src/nginx-1.18.0/
[root@localhost nginx-1.18.0]# useradd -M -s /sbin/nologin www
[root@localhost nginx-1.18.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@localhost nginx-1.18.0]# ./configure \
--prefix=/usr/local/nginx/  \
--user=www   \
--group=www  \
--with-pcre \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-stream_proxy && make && make install
[root@localhost nginx-1.18.0]# cd
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# nginx 
[root@localhost ~]# netstat -anput | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1033/nginx: master  

php源码安装

[root@localhost ~]# wget -O php-7.1.9.tar.gz http://hk1.php.net/get/php-7.1.9.tar.gz/from/this/mirror
[root@localhost ~]# ls
php-7.1.9.tar.gz
[root@localhost ~]# yum -y install libxml2-devel bzip2-devel libcurl-devel libjpeg-devel libjpeg openssl opessl-devel libpng-devel libpng gmp gmp-devel readline readline-devel libxslt libxslt-devel freetype freetype-devel
[root@localhost ~]# tar xf php-7.1.9.tar.gz -C /usr/src/ && cd /usr/src/php-7.1.9
[root@localhost php-7.1.9]# cat <<EOF >>php_install.sh
./configure \
--prefix=/usr/local/php7 \
--with-gd \
--with-zlib \
--with-curl \
--with-mysql=/usr/local/mysql \
--with-config-file-path=/usr/local/php7/etc \
--enable-mbstring \
--enable-fpm  \
--with-openssl \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql=mysqlnd \
--with-mysql-sock \
--enable-mysqlnd \
--enable-zip \
--with-bz2 \
--enable-xmlreader \
--enable-xmlwriter \
--with-xmlrpc \
--with-libxml-dir=/usr \
--with-freetype-dir \
--with-png-dir=/usr/lib \
--enable-soap \
--enable-sockets \
--enable-pcntl \
--enable-exif \
--enable-bcmath \
--with-mhash \
--enable-ftp \
--enable-opcache \
--enable-fileinfo \
--with-pear \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-calendar \
--with-jpeg-dir=/usr/lib \
&& make clean && make && make install
EOF
[root@localhost php-7.1.9]# chmod +x php_install.sh
[root@localhost php-7.1.9]# ./php_install.sh
[root@localhost php-7.1.9]# cp php.ini-development /usr/local/php7/etc/php.ini
[root@localhost php-7.1.9]# cd /usr/local/php7/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@localhost etc]# cd
[root@localhost ~]# cat <<EOF >>/etc/init.d/php-fpm
#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/usr/local/php7
exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF -c /usr/local/php7/etc/php.ini --pid $php_fpm_PID"


wait_for_pid () {
	try=0

	while test $try -lt 35 ; do

		case "$1" in
			'created')
			if [ -f "$2" ] ; then
				try=''
				break
			fi
			;;

			'removed')
			if [ ! -f "$2" ] ; then
				try=''
				break
			fi
			;;
		esac

		echo -n .
		try=`expr $try + 1`
		sleep 1

	done

}

case "$1" in
	start)
		echo -n "Starting php-fpm "

		$php_fpm_BIN --daemonize $php_opts

		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		fi

		wait_for_pid created $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	stop)
		echo -n "Gracefully shutting down php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -QUIT `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed. Use force-quit"
			exit 1
		else
			echo " done"
		fi
	;;

	status)
		if [ ! -r $php_fpm_PID ] ; then
			echo "php-fpm is stopped"
			exit 0
		fi

		PID=`cat $php_fpm_PID`
		if ps -p $PID | grep -q $PID; then
			echo "php-fpm (pid $PID) is running..."
		else
			echo "php-fpm dead but pid file exists"
		fi
	;;

	force-quit)
		echo -n "Terminating php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -TERM `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	restart)
		$0 stop
		$0 start
	;;

	reload)

		echo -n "Reload service php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -USR2 `cat $php_fpm_PID`

		echo " done"
	;;

	configtest)
		$php_fpm_BIN -t
	;;

	*)
		echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
		exit 1
	;;

esac
EOF
[root@localhost ~]# chmod +x /etc/init.d/php-fpm
[root@localhost ~]# chkconfig --add php-fpm
[root@localhost ~]# chkconfig php-fpm on
[root@localhost ~]# systemctl start php-fpm
[root@localhost ~]# systemctl status php-fpm
 php-fpm.service - LSB: starts php-fpm
   Loaded: loaded (/etc/rc.d/init.d/php-fpm; bad; vendor preset: disabled)
   Active: active (running) since  2022-05-17 11:57:05 CST; 1 day 1h ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1265 ExecStop=/etc/rc.d/init.d/php-fpm stop (code=exited, status=0/SUCCESS)
  Process: 1269 ExecStart=/etc/rc.d/init.d/php-fpm start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/php-fpm.service
           ├─1271 php-fpm: master process (/usr/local/php7/etc/php-fpm.co...
           ├─1272 php-fpm: pool www
           ├─1273 php-fpm: pool www
           ├─1274 php-fpm: pool www
           ├─1275 php-fpm: pool www
           └─2264 php-fpm: pool www

5 17 11:57:05 nginx-1 systemd[1]: Starting LSB: starts php-fpm...
5 17 11:57:05 nginx-1 php-fpm[1269]: Starting php-fpm  done
5 17 11:57:05 nginx-1 systemd[1]: Started LSB: starts php-fpm.

配置nginx日志切割

[root@localhost ~]# cd /usr/local/nginx/conf
[root@localhost conf]#vim nginx.conf
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  100;

    #gzip  on;
   server {
        listen 80;
        server_name 192.168.19.202;
        root /usr/local/nginx/html;
        index  index.php index.html index.htm;
	access_log	logs/access.log main;

        location ~ \.php$ {
                fastcgi_pass    127.0.0.1:9000;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include         fastcgi_params;
                }
        location / {
                try_files $uri $uri/ /index.php?$query_string;
                }
        }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
[root@localhost conf]# cd 
[root@localhost ~]# rm -fr /usr/local/nginx/html/index.html
[root@localhost ~]# cat <<EOF > /usr/local/nginx/html/index.php
<?php
phpinfo();
>
EOF
[root@localhost ~]# nginx -s reload      #使用浏览器查看是否成功,是否能访问到php的配置页面

编写日志分割脚本
[root@localhost ~]# cat <<EOF >LOG_partition.sh
#!/bin/bash
#请自定义项目名称名称LOGNAME
#如果nginx日志目录不在该地点,请手动更改nginx默认日志路径
#将脚本写入计划任务中,并且保证服务器时间准确,具体多长时间去执行脚本可根据计划任务时间来决定
#例:*/59 23 * * * /usr/bin/bash /$LOG_PATH/LOG_partition.sh #每天2359执行计划脚本
LOG_HOME=/usr/local/nginx/logs
LOGNAME=test
LOG_NEW_HOME=/var/www/
TIME=`date "+%Y_%m_%d"`
NEWLOG=$LOG_NEW_HOME/${LOGNAME}_$TIME.log
#创建日志存放目录
if [ ! -d $LOG_WNSD_HOME ];then
	mkdir -p $LOG_WNSD_HOME;
fi
#将日志文件转移成新的日志文件
/usr/bin/cp -rf $LOG_HOME/access.log $NEWLOG && echo >$LOG_HOME/access.log
#删除30天后的日志
OVERDUE_LOG=`find $LOG_NEW_HOME/ -mtime +30 -type f -name "*.log"`
rm -fr $OVERDUE_LOG
[root@localhost ~]# chmod +x LOG_protition.sh
[root@localhost ~]# crontab -e
59/59 23 * * * /usr/bin/bash /root/LOG_protition.sh   #每天235959秒执行计划任务

:wq
[root@localhost ~]# systemctl restart crond
等一阵子,这里我没有用那么长时间的计划任务所以会很快生成新的日志
[root@localhost ~]# cd /var/www/
[root@localhost www]# ls
test_2022_05_18.log     test_2022_05_19.log      test_2022_05_20.log 
test_2022_05_21.log

ok完成

欢迎大家多多指教!

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值