打zZ~瞌睡 Menu

总结与Linux下相关环境搭建相关的问题

CentOS源码编译安装Apache + PHP + MySQL

1. 安装编译器和库文件:

#有些是中途安装AMP的过程中发现没有安装,作为总结,写在最前边
sudo yum install -y cmake.x86_64 gcc-c++.x86_64 ncurses.x86_64 ncurses-devel.x86_64 libxml2-devel.x86_64 libcurl-devel.x86_64 libjpeg-turbo-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 autoconf.noarch

 

2. 安装MySQL
2.1 下载MySQL:可以在http://dev.mysql.com/downloads/mysql/查看对应版本

wget http://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.50.tar.gz

2.2 创建mysql用户

groupadd mysql
useradd -r -g mysql mysql

2.3 创建mysql目录

mkdir /usr/local/mysql
mkdir /usr/local/mysql/data
chown -R mysql:mysql /usr/local/mysql/data

tar -zxvf mysql-5.5.50.tar.gz
cd mysql-5.5.50
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1
make
make install

cp support-files/my-medium.cnf /etc/my.cnf

/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
/usr/local/mysql/bin/mysqld_safe &
cd /usr/local/mysql/ #重要,否则会报 Can't find a 'mysql' client in PATH or ./bin
bin/mysql_secure_installation

 

3. 安装apache

wget http://mirrors.tuna.tsinghua.edu.cn/apache//httpd/httpd-2.2.31.tar.gz
wget http://apache.fayea.com//apr/apr-1.5.2.tar.gz
wget http://apache.fayea.com//apr/apr-util-1.5.4.tar.gz
wget http://downloads.sourceforge.net/project/pcre/pcre/8.39/pcre-8.39.zip

tar -zxvf apr-1.5.2.tar.gz
./configure && make && make install

tar -zxvf apr-util-1.5.4.tar.gz
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install

unzip pcre-8.39.zip
./configure --prefix=/usr/local/pcre && make && make install

tar -zxvf httpd-2.2.31.tar.gz
./configure --prefix=/usr/local/apache2 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --with-included-apr --enable-deflate --enable-expires --enable-headers --enable-rewrite --enable-so
make && make install

 

4. 安装PHP

wget http://am1.php.net/get/php-5.5.38.tar.gz/from/this/mirror
tar -zxvf php-5.5.38.tar.gz
./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs --with-zlib --with-curl --enable-exif --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --with-gettext --with-mhash --enable-mbstring --with-mysql --with-mysqli --enable-zip --with-pear
make && make install
sudo cp php.ini-production /usr/local/php5/lib/php.ini

修改/usr/local/apache2/conf/httpd.conf,添加:

AddType application/x-httpd-php .php .phtml
AddType application/x-httpd-php-source .phps

重启apache:

sudo /usr/local/apache2/bin/apachectl start|graceful

 

5. 安装xcache

wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz
tar -zxvf xcache-3.2.0.tar.gz
cd xcache-3.2.0
/usr/local/php5/bin/phpize
./configure --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer --with-php-config=/usr/local/php5/bin/php-config
make && make install

建立缓存文件

touch /tmp/xcache
chmod 0777 /tmp/xcache

拷贝xcache管理后台到可访问目录

cp -R htdocs/ /var/www/html/xcache

生成密码

http://{ip}/xcache/cacher/mkpassword.php
Username: xcache
Password: 123456
记住生成后的密码,替换下边的{generated password}

将以下代码写入/usr/local/php/lib/php.ini

[xcache-common]
extension = xcache.so

[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "xcache"
xcache.admin.pass = "{generated password}"

[xcache]
xcache.shm_scheme = "mmap"
xcache.size = 64M
xcache.count = 1
xcache.slots = 8K
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size= 64M
xcache.var_count = 1
xcache.var_slots = 8K
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.test = Off
xcache.readonly_protection = On
xcache.mmap_path = "/tmp/xcache"
xcache.coredump_directory = ""
xcache.cacher = On
xcache.stat = On
xcache.optimizer = Off

[xcache.coverager]
xcache.coverager = On
xcache.coveragedump_directory = ""

访问管理地址查看缓存使用情况

http://{ip}/xcache

Ubuntu 14.04下使用pear安装File_IMC

最近有个项目要使用File_IMC扩展,使用两种方法都出错

[email protected]:~# pear install -f File_IMC-beta
downloading File_IMC-0.5.0.tgz ...
Starting to download File_IMC-0.5.0.tgz (32,082 bytes)
.........done: 32,082 bytes
could not extract the package.xml file from "/build/buildd/php5-5.5.9+dfsg/pear-build-download/File_IMC-0.5.0.tgz"
Download of "pear/File_IMC" succeeded, but it is not a valid package archive
Error: cannot download "pear/File_IMC"
Download failed
install failed
[email protected]:~# php pyrus.phar install File_IMC
Pyrus version 2.0.0a4 SHA-1: 72271D92C3AA1FA96DF9606CD538868544609A52
Using PEAR installation found at /etc/php5/pear
Sorry there was an error retrieving pear2.php.net/File_IMC from the default channel
Pyrus\Channel\Exception: package File_IMC does not exist
 Pyrus\HTTPException: Download of http://pear2.php.net/rest/p/file_imc/info.xml failed, file does not exist
Google了一下说是因为Ubuntu升级到14.04的原因,使用-Z参数下载非压缩版本安装即可
[email protected]:~# pear install -Z File_IMC-beta
downloading File_IMC-0.5.0.tar ...
Starting to download File_IMC-0.5.0.tar (Unknown size)
....................................done: 167,936 bytes
install ok: channel://pear.php.net/File_IMC-0.5.0

SVN导出两个版本之前添加或修改过的文件

在工作中,由于某种原因,部署于正式版本的代码不是由SVN直接更新的,这样在经过几次修改提交之后再发布修改的内容时会有点麻烦,需要对每个版本修改过的文件逐个检查上传。下面这个脚本文件可以导出两个版本之间添加过修改过的文件,导出后一起上传就可以了。

在工作中,由于某种原因,部署于正式版本的代码不是由SVN直接更新的,这样在经过几次修改提交之后再发布修改的内容时会有点麻烦,需要对每个版本修改过的文件逐个检查上传。下面这个脚本文件可以导出两个版本之间添加过修改过的文件,导出后一起上传就可以了。

#!/bin/bash

if [ ! $1 ] || [ ! $2 ] || [ ! $3 ] || [ ! $4 ]; then
    echo "Please enter a revision from, revision to, SVN repository, and target directory"
    exit
fi

# set up nice names for the incoming parameters to make the script more readable
revision_from=$1
revision_to=$2
repository=$3
target_directory=$4

# the grep is needed so we only get added/modified files and not the deleted ones or anything else
# if it's a modified directory it's " M" so won't show with this command (good)
# if it's an added directory it's still "A" so will show with this command (not so good)

for line in `svn diff --summarize -r$revision_from:$revision_to $repository | grep "^[AM]"`
do
    # each line in the above command in the for loop is split into two:
    # 1) the status line (containing A, M, AM, D etc)
    # 2) the full repository and filename string
    # so only export the file when it's not the status line
    if [ $line != "A" ] && [ $line != "AM" ] && [ $line != "M" ]; then
        # use sed to remove the repository from the full repo and filename
        filename=`echo "$line" |sed "s|$repository||g"`
        # don't export if it's a directory we've already created
        if [ ! -d $target_directory$filename ]; then
            directory=`dirname $filename`
            mkdir -p $target_directory$directory
            svn export -r $revision_to $line $target_directory$filename
        fi
    fi
done

# to summarize any deleted files or directories at the end of the script uncomment the following line
#svn diff --summarize -r$revision_from:$revision_to $repository | grep "^[D]"

将文件保存为svndiff,并将权限改为0755,使用方法:

# $from_version: 起始版本号
# $to_version  : 结束版本号
# $svn_repository: svn版本库地址,也可以是本地svn路径
# $target:         导出目录
svndiff $from_version $to_version $svn_repository $target

Nginx中配置htpasswd

1. 下载生成htpasswd的脚本

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

echo "====================================="
echo "# A tool like htpasswd for Nginx    #"
echo "#-----------------------------------#"
echo "# Author:Licess http://www.lnmp.org #"
echo "====================================="

#set UserName

	username=""
	read -p "Please input UserName:" username
	if [ "$username" = "" ]; then
		echo "Error:UserName can't be NULL!"
		exit 1
	fi
	echo "==========================="
	echo "UserName was: $username"
	echo "==========================="

#set password

	unpassword=""
	read -p "Please input the Password:" unpassword
	if [ "$unpassword" = "" ]; then
		echo "Error:Password can't be NULL!"
		exit 1
	fi
	echo "==========================="
	echo "Password was: $unpassword"
	echo "==========================="
password=$(perl -e 'print crypt($ARGV[0], "pwdsalt")' $unpassword)

#set htpasswd file

	htfile=""
	read -p "Please input Auth filename:" htfile
	if [ "$htfile" = "" ]; then
		echo "Error:Auth filename can't be NULL!"
		exit 1
	fi
	echo "==========================="
	echo "Auth File: /usr/local/nginx/conf/$htfile"
	echo "==========================="

	get_char()
	{
	SAVEDSTTY=`stty -g`
	stty -echo
	stty cbreak
	dd if=/dev/tty bs=1 count=1 2> /dev/null
	stty -raw
	stty echo
	stty $SAVEDSTTY
	}
	echo ""
	echo "Press any key to Creat...or Press Ctrl+c to cancel"
	char=`get_char`

if [ ! -f /usr/local/nginx/conf/$htfile.conf ]; then
  echo "Create Auth file......"
cat >/usr/local/nginx/conf/$htfile.conf<<eof
$username:$password
eof
echo "Create Auth file successful,auth file path:/usr/local/nginx/conf/$htfile.conf."
else
	echo "File already exists,please run this script again."
	exit 1
由于编辑器限制,请自行将&gt;转为 >,将&lt;转化为 <

2. 设置脚本权限为可执行权限

chmod 0755 htpasswd.sh

3. 运行脚本生成密码文件
需要输入用户名、密码和文件名,脚本会帮我们生成好密码文件。

我的脚本是生成到/usr/local/nginx/conf目录下,如有需要,请在脚本中修改

4. 在Nginx虚拟站点的配置中加入以下两句

auth_basic "Authorized users only";
auth_basic_user_file /usr/local/nginx/conf/htpasswd.conf;
这里只是简单的验证,有权限即可。当然也可以改为只限制某个用户访问

5. 重启nginx生效

sudo /usr/local/nginx/sbin/nginx -s reload

Nginx中WordPress、Joomla、Drupal的URL重写配置

Drupal

location / {
	if (!-e $request_filename) {
		rewrite ^/(.*)$ /index.php?q=$1 last;
	}
}

 

Joomla

location / {
	if ( $args ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3d)" ) {
		set $args "";
		rewrite ^.*$ http://$host/index.php last;
		return 403;
	}
 
	if ( $args ~ "base64_encode.*\(.*\)") {
		set $args "";
		rewrite ^.*$ http://$host/index.php last;
		return 403;
	}
 
	if ( $args ~ "(\&lt;|%3C).*script.*(\&gt;|%3E)") {
		set $args "";
		rewrite ^.*$ http://$host/index.php last;
		return 403;
	}
	
	if ( $args ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") {
		set $args "";
		rewrite ^.*$ http://$host/index.php last;
		return 403;
	}
	
	if ( $args ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") {
		set $args "";
		rewrite ^.*$ http://$host/index.php last;
		return 403;
	}
	
	if (!-e $request_filename) {
		rewrite (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ /index.php last;
		break;
	}
 
}

 

WordPress

location / {
	if (-f $request_filename/index.html) {
		rewrite (.*) $1/index.html break;
	}
	if (-f $request_filename/index.php) {
		rewrite (.*) $1/index.php;
	}
	if (!-f $request_filename) {
		rewrite (.*) /index.php;
	}
}

php 5.3.3 中 php-fpm 的重启、终止操作命令

php 5.3.3 源码中已经内嵌了 php-fpm,不用象以前的php版本一样专门打补丁了,只需要在configure的时候添加编译参数即可。

关于php-fpm的编译参数有 –enable-fpm –with-fpm-user=www –with-fpm-group=www –with-libevent-dir=libevent位置。

但是,php 5.3.3 下的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制:

master进程可以理解以下信号

  • INT, TERM 立刻终止
  • QUIT 平滑终止
  • USR1 重新打开日志文件
  • USR2 平滑重载所有worker进程并重新载入配置和二进制模块
kill -INT `cat /usr/local/php/var/run/php-fpm.pid`   # php-fpm 关闭
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`  # php-fpm 重启
ps aux | grep -c php-fpm                             # 查看php-fpm进程数