分类 服务器技术 下的文章

今天老高更新了一下nginx,顺便把nginx的安装编译过程记录一下,分享给大家!

一并送上之前老高的博文:

将nginx配置为服务

nginx配置详解

下载

官网下载地址:http://nginx.org/en/download.html

准备

安装nginx时必须先安装相应的编译工具

yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

centos没有安装make编译器

yum -y install gcc automake autoconf libtool make

建立nginx 组

groupadd -r nginx
useradd -s /sbin/nologin -g nginx -r nginx

注释:

zlib:nginx提供gzip模块,需要zlib库支持 openssl:nginx提供ssl功能 pcre:支持地址重写rewrite功能

修改版本号

再编译之前修改默认的版本号是个不错的习惯

vim src/core/nginx.h

#define NGINX_VERSION      "0.0.0"
#define NGINX_VER          "phpergao" NGINX_VERSION

编译

高大上的编译参数

./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_spdy_module

安装

make && make install

添加服务

以下是service的脚本。

注:脚本中部分变量值可能需要修改以找到对应的文件,如果出现文件夹找不到的错误,请使用mkdir -p创建对应路径

# 先执行 vim /etc/init.d/nginx
# 再copy以下脚本

#!/bin/sh
#
# nginx        Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# description: nginx is an HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -L $0 ]; then
    initscript=`/bin/readlink -f $0`
else
    initscript=$0
fi

sysconfig=`/bin/basename $initscript`

if [ -f /etc/sysconfig/$sysconfig ]; then
    . /etc/sysconfig/$sysconfig
fi

nginx=${NGINX-/usr/sbin/nginx}
prog=`/bin/basename $nginx`
conffile=${CONFFILE-/etc/nginx/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/var/run/nginx.pid}
SLEEPMSEC=${SLEEPMSEC-200000}
UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}
RETVAL=0

start() {
    echo -n $"Starting $prog: "

    daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} ${prog}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

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

upgrade() {
    oldbinpidfile=${pidfile}.oldbin

    configtest -q || return
    echo -n $"Starting new master $prog: "
    killproc -p ${pidfile} ${prog} -USR2
    echo

    for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do
        /bin/usleep $SLEEPMSEC
        if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
            echo -n $"Graceful shutdown of old $prog: "
            killproc -p ${oldbinpidfile} ${prog} -QUIT
            RETVAL=$?
            echo
            return
        fi
    done

    echo $"Upgrade failed!"
    RETVAL=1
}

configtest() {
    if [ "$#" -ne 0 ] ; then
        case "$1" in
            -q)
                FLAG=$1
                ;;
            *)
                ;;
        esac
        shift
    fi
    ${nginx} -t -c ${conffile} $FLAG
    RETVAL=$?
    return $RETVAL
}

rh_status() {
    status -p ${pidfile} ${nginx}
}

# See how we were called.
case "$1" in
    start)
        rh_status >/dev/null 2>&1 && exit 0
        start
        ;;
    stop)
        stop
        ;;
    status)
        rh_status
        RETVAL=$?
        ;;
    restart)
        configtest -q || exit $RETVAL
        stop
        start
        ;;
    upgrade)
        rh_status >/dev/null 2>&1 || exit 0
        upgrade
        ;;
    condrestart|try-restart)
        if rh_status >/dev/null 2>&1; then
            stop
            start
        fi
        ;;
    force-reload|reload)
        reload
        ;;
    configtest)
        configtest
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
        RETVAL=2
esac

exit $RETVAL
mkdir -p /var/tmp/nginx/client
chmod 755 /etc/init.d/nginx
chkconfig --add nginx

使用

# 重启nginx
service nginx restart

现在访问你的IP或者域名就可以访问了!

Welcome to nginx!

参考链接:

http://blog.sina.com.cn/s/blog_6f2274fb01012nq5.html http://www.cnblogs.com/suihui/archive/2013/04/13/3018557.html

服务器为了安全设置,使用普通用户登陆,再su至root。

而su以后超过一定时间会超时退出到普通用户,带来了一定的麻烦。

解决办法:

OS:CENTOS 6

vi /etc/profile

# 注释
#TMOUT=300

sudo执行的第一次需要验证密码,之后一段时间不需要输入就可以执行命令,控制超时的方法:

sudo visudo

# 翻到60-70行,类似

Defaults    env_reset

#改为,30000指的是超时时间是30000min,请合理设置

Defaults    env_reset,timestamp_timeout=30000

主服务器(master)IP:192.168.0.1 从服务器(slave)IP:192.168.0.2 首先确保主从服务器上的Mysql版本相同

主服务器上操作

创建用户名为repl的一个账户

GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.2' IDENTIFIED BY 'xxxxxxxxx';

修改主数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改之后必须重启Mysql服务,如果不需要修改可不用重启。

server-id=1
log_bin = /usr/llocal/mysql/log/mysql-bin.log

之后可以得到主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复

flush tables with read lock;  这是session级,退出就隐式 unlock tables;
show master status;

生成主数据库的备份 如果mysqldump 无法识别,则在/home/mysql/.bash_profile 添加环境变量 export PATH=$PATH:/usr/local/mysql/bin mysqldump -p3306 -uroot –pxxxxxxxx test > test.sql unlock tables; 将备份出来的数据复制到从数据库

Scp test.sql 192.168.0.2:

从服务器上操作

将备份数据导入数据库

Mysql –uroot –pxxxxxxxx  test < test.sql

修改从数据库的my.cnf,增加server-id参数,如有更改需要重启

server-id=2 # 注:一定不能跟主数据库一样

指定复制使用的用户,主数据库服务器的ip,端口以及开始执行复制日志的文件和位置

CHANGE MASTER TO MASTER_HOST='192.168.1.130', MASTER_USER='repl', MASTER_PASSWORD='456123', MASTER_LOG_FILE='mysql-bin.xxxx', MASTER_LOG_POS=xxxx;

MASTER_LOG_FILE=' mysql-bin.xxxx', MASTER_LOG_POS=xxxx 这俩个参数参见主服务器 show master status

启动slave进程

Start slave;

查看从服务器状态

Show slave status;
Slave_IO_Running: Yes //此状态必须YES
Slave_SQL_Running: Yes //此状态必须YES

测试

在主数据库上插入一条数据,然后看从数据库是否有更新。然后就可以自己发挥了。总的来说就是一般用主从复制(Master-Slave)的方式来同步数据,再通过读写分离(MySQL-Proxy)来提升数据库的并发负载能力,再通过高可用性(High Availability)确保服务的稳定。

转自:http://java.dzone.com/articles/useful-subversion-pre-commit

  1. Checks whether the commit message is not empty
  2. Checks whether the commit message consists of at least 5 characters
  3. Checks if the committed files are UTF-8 compliant
  4. Checks whether the svn:eol-style property is set to LF on newly added files
  5. Checks if the committed files have no TAB characters

The UTF-8 and TAB checks are performed on the following file suffixes

  • *.java
  • *.js
  • *.xhtml
  • *.css
  • *.xml
  • *.properties (only check for TABs here, no check for UTF-8 compliance)

翻译一下:

  1. 检查提交日志是否为空
  2. 检查提交日志最少需要N个字符
  3. 检查提交文件是否是UTF-8格式
  4. 检查新文件的换行模式是否为LF
  5. 检查提交的文件是否含有TABs换行符

检查UTF-8编码和TABs换行符只针对以下后缀文件:

  • *.java
  • *.js
  • *.xhtml
  • *.css
  • *.xml
  • *.properties (只检查TABs,不检查UTF-8)

以下是代码

注意:针对Linux

#!/bin/bash

REPOS="$1"
TXN="$2"


# Make sure that the log message contains some text.
SVNLOOK=/usr/bin/svnlook
ICONV=/usr/bin/iconv

SVNLOOKOK=1
LOGMSG=`$SVNLOOK log -t "$TXN" "$REPOS" | wc -c` 
if [ "$LOGMSG" -lt 2 ];
then
   echo -e "\t That logmessage contains at least 2 alphanumeric characters. Commit aborted!" 1>&2
  exit 1
fi


# Make sure that all files to be committed are encoded in UTF-8.
while read changeline; 
do

  # Get just the file (not the add / update / etc. status).
  file=${changeline:4}

  # Only check source files.
  if [[ $file == *.java || $file == *.xhtml || $file == *.css || $file == *.xml || $file == *.js ]] ; then
    $SVNLOOK cat -t "$TXN" "$REPOS" "$file" | $ICONV -f UTF-8 -t UTF-8 -o /dev/null
    if [ "${PIPESTATUS[1]}" != 0 ] ; then
      echo "Only UTF-8 files can be committed ("$file")" 1>&2
      exit 1
    fi
  fi
done < <($SVNLOOK changed -t "$TXN" "$REPOS")

# Check files for svn:eol-style property
# Exit on all errors.
set -e
EOL_STYLE="LF"
echo "`$SVNLOOK changed -t "$TXN" "$REPOS"`" | while read REPOS_PATH
do
 if [[ $REPOS_PATH =~ A[[:blank:]]{3}(.*)\.(java|css|properties|xhtml|xml|js) ]]
 then
  if [ ${#BASH_REMATCH[*]} -ge 2 ]
    then
  FILENAME=${BASH_REMATCH[1]}.${BASH_REMATCH[2]};

  # Make sure every file has the right svn:eol-style property set
   if [ $EOL_STYLE != "`$SVNLOOK propget -t \"$TXN\" \"$REPOS\" svn:eol-style \"$FILENAME\" 2> /dev/null`" ]
    then
    ERROR=1;
      echo "svn ps svn:eol-style $EOL_STYLE \"$FILENAME\"" >&2
   fi
  fi
 fi
 test -z $ERROR || (echo "Please execute above commands to correct svn property settings. EOL Style LF must be used!" >& 2; exit 1)
done



# Block commits with tabs
# This is coded in python
# Exit on all errors
set -e

$SVNLOOK diff -t "$TXN" "$REPOS" | python /dev/fd/3 3<<'EOF'
import sys
ignore = True
SUFFIXES = [ ".java", ".css", ".xhtml", ".js", ".xml", ".properties" ]
filename = None

for ln in sys.stdin:

    if ignore and ln.startswith("+++ "):
        filename = ln[4:ln.find("\t")].strip()
        ignore = not reduce(lambda x, y: x or y, map(lambda x: filename.endswith(x), SUFFIXES))

    elif not ignore:
        if ln.startswith("+"):
        
           if ln.count("\t") > 0:
              sys.stderr.write("\n*** Transaction blocked, %s contains tab character:\n\n%s" % (filename, ln))
              sys.exit(1)

        if not (ln.startswith("@") or \
           ln.startswith("-") or \
           ln.startswith("+") or \
           ln.startswith(" ")):

           ignore = True

sys.exit(0)
EOF

# All checks passed, so allow the commit.
exit 0

如何使用

  1. 重命名hooks文件夹下pre-commit.tmplpre-commit
  2. 修改文件内容
  3. 将文件变为可执行 chmod +x pre-commit

提交完代码自动更新

vim hooks/post-commit

#!/bin/sh
export LANG=zh_CN.UTF-8  # 设置UTF-8编码
SVN=/usr/bin/svn         # 这里配置的是svn安装bin目录下的svn文件
WEB=/var/www/html      # 要更新的目录
$SVN update $WEB --username xxxx --password xxxx

今天使用win上的phpstrom发现不能检出svn服务器上的文件,折腾了一番才发现原来是svn设置问题。

解决办法:

进入settings

按照下图设置:

phpstorm svn 设置

这里我们指定了svn的命令行路径,CollabNet\Subversion Client是CollabNet出的svn客户端命令行工具,换成其他的也可以,只要路径指定正确就行。

附下载地址:

支持各种系统的svn

推荐visualsvn