tid=21242&參考自 : http://blog.csdn.net/yanzi1225627/article/details/49123659
1 \) l, n" [6 m: P( q6 O- O- V5 B# ]' B F) R4 b k
目标:搭建LNMP(Linux + Nginx + MySQL + PHP +SVN),其中svn是用来代替ftp,方便开发中调试同步代码 相关目录:所有软件都安装到/www/目录下,在www目录下新建web文件夹作为网站的根路径,www目录下新建wwwsvn作为svn的仓库地址。/www/software用来放nginx,mysql,php的安装包和源码。nginx运行分组和账户www:www 一,安装前的准备yum -y install ntp make openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg-6b libjpeg-devel-6b freetype freetype-devel gd gd-devel zlib zlib-devel gcc gcc-c++ libXpm libXpm-devel ncurses ncurses-devel libmcrypt libmcrypt-devel libxml2 libxml2-devel imake autoconf automake screen sysstat compat-libstdc++-33 curl curl-devel cmake/ Z2 ~* y, y) e# ?' D, z4 E
直接将所有待安装的依赖安装完。 然后下载nginx ,mysql, php的源代码: 可用 google search engine 搜尋最新版本 将这三份tar.gz文件通过scp命令弄到服务器上/www/software目录下。 二,安装nginx解压缩文件,然后进到nginx-1.8.0里,输入命令: ./configure --user=www --group=www --prefix=/www/nginx6 d* U) o" C5 r6 O( C- P) D+ M
然后make,make install就安装完毕了。 安装完后第一件事,创建www的用户和分组,否则会遇到http://blog.itblood.com/nginx-emerg-getpwnam-www-failed.html 的错误。 执行: /usr/sbin/groupadd -f www! `) K, {, @+ H0 d. Z
/usr/sbin/useradd M -s /sbin/nologin -g www www //-M 不建立使用者目錄 nginx命令在/www/nginx/sbin/下,拷贝到/etc/init.d/一份,接下来设置开机启动。 chmod 755 /etc/init.d/nginx% q& z( W3 a. f2 V' n& P
9 E! {# U+ u' d% C1 z( X
chkconfig --add nginx
5 p4 X# L0 L! h* b- `: q. O! l1 M6 M3 K! q( d2 @$ C5 N0 s
chkconfig nginx on
1 E. d# r* {8 H 然后 cd /etc/rc.d/init.d/ 目录下新建nginx,内容如下: #!/bin/bash
, d0 _$ p$ G2 V3 G# nginx Startup script for the Nginx HTTP Server
# `* T# ?" i# x* e- w% z7 r# it is v.0.0.2 version. d) [$ R) D0 k) P# o& B. r' B2 W
# chkconfig: - 85 15$ w/ r5 Q- o! U' t/ k* |
# description: Nginx is a high-performance web and proxy server.
G, ~- p# {' n( d# u9 I# It has a lot of features, but it's not for everyone.
! ~. f5 W$ T1 W" t% O A# processname: nginx
, d/ e- {" p* ]5 g# @4 m5 p' C# pidfile: /var/run/nginx.pid& t( [ k" p8 f
# config: /usr/local/nginx/conf/nginx.conf
/ K# V, ~9 G! [2 \) enginxd=/www/nginx/sbin/nginx. A) D! x6 E% Q7 d& w. `5 H
nginx_config=/www/nginx/conf/nginx.conf
( F7 A- A/ Z" T5 xnginx_pid=/www/nginx/logs/nginx.pid
6 c3 i& P/ K4 ?& j5 d0 z: y2 |RETVAL=0
9 A, S7 W& O& r, i- q* qprog="nginx"
9 r' ^& C" v0 t0 X2 O1 h N7 M" N# Source function library.
k: ?* f2 g* ?! `2 Q, J2 |. /etc/rc.d/init.d/functions$ K; m( {9 x1 V" e9 b9 X, a* n2 y
# Source networking configuration.9 ?) F% T4 u$ }1 x
. /etc/sysconfig/network1 w- }' m! c* M4 L' ?" h
# Check that networking is up.% ^3 f: A Z* ~8 r) R
[ ${NETWORKING} = "no" ] && exit 09 S, v0 W" d% S4 E0 T8 K# H- `
[ -x $nginxd ] || exit 0
* k6 x7 [( f) S \5 I8 W% O# Start nginx daemons functions.
2 r0 p- O5 X4 ~; N) [start() {
7 }; ?! o# l, W/ T0 n( pif [ -e $nginx_pid ];then7 p$ ]9 I4 T0 a* U0 b- [) X9 l5 \
echo "nginx already running...."
$ A! @7 I, L: m8 L8 _" E& U5 Eexit 13 h& w9 g5 b* \4 Q
fi+ F/ Z6 j- J# t6 e
echo -n $"Starting $prog: "
! x2 S+ W T& C8 `- Gdaemon $nginxd -c ${nginx_config}! o4 j2 H* l$ k; e7 L& J7 [1 ]4 v* `
RETVAL=$?
: l* P3 Q# l( U" |4 a/ S- Secho* R* Y: T) Q4 H6 _+ U
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
6 l: n, ]3 d9 |( B- ]7 oreturn $RETVAL8 v' ^/ H* d) [
}; @+ l* Y, C) z8 W! R) F
# Stop nginx daemons functions.
, w7 q2 A2 r. `! }: U* x- X/ v0 ustop() {
) f2 l7 j2 [+ ?) ?echo -n $"Stopping $prog: "
9 q t3 w$ I' Z! f' Q6 Y+ Z- G7 [killproc $nginxd+ z9 h3 h$ S/ S
RETVAL=$?& b5 \; L0 r: Z( \' |( F* M
echo" \7 U$ ^8 r& t. T3 W% S
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid' U0 T1 y0 I6 U0 Z. L
}
6 Z2 {( f! N0 Wreload() {, D; ?* U u+ Q0 B
echo -n $"Reloading $prog: "
* d r6 i! @# w: Q- H#kill -HUP `cat ${nginx_pid}`
7 S# o: i: A# ?killproc $nginxd -HUP
- \% \5 B; l2 w0 x1 jRETVAL=$?# p( ~, U) c/ v7 E$ `2 U4 n
echo1 t1 O0 c; f4 l! V6 h8 C
}
, G! y, U. {, y# See how we were called.
6 y- `( C6 e3 Y& S3 Ecase "$1" in) Q( @8 I" @. U: l7 l6 t
start)- B `3 t! n# ^( o* o9 I
start
) [- m' r4 E# r( i e3 X;;( v# ]) b" F$ d3 a
stop), T* o7 H. l5 b" `& h3 C, }2 P
stop
) u9 J6 r" A# Q( @2 z;;
. D. n& M3 m$ y5 E3 H% Xreload): y" n- }. b4 _0 B
reload
2 n: t* w, h3 T) ]3 |. p;;
1 s( q6 E% i" O( Krestart)
. M: Q/ {- _3 [7 \( D& ^stop
5 P3 Q" f3 f" K. C& H9 r- Y* tstart
) \ F S1 A5 w7 r) h. l;;
$ A3 T/ e7 c! X. }status)
+ Z5 [6 ]/ j P9 I/ v* pstatus $prog5 A t& c$ q( V0 M. c9 t/ W
RETVAL=$?7 o/ N- @4 b2 x
;;
7 z# Q9 z+ I: G*)
6 N$ e4 I+ h$ Y; p5 F( ?echo $"Usage: $prog {start|stop|restart|reload|status|help}"$ u1 x5 b$ J1 o
exit 1" }( [1 p6 y, o
esac, @5 h, @; U1 }. C
exit $RETVAL
2 k" h) e. p( Y. A5 K- e) i( V. ~" f( a, o: n5 j
注意:如果nginx的安装路径不是在/www/nginx下,则适当修改就好。
' h- X* N( W3 R+ r. `8 ?chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:5 n; ~' {! P8 J) Q
1,是更改默认web根目录在/www/web的问题 2,是与php的整合,默认nginx是不认php得 对于1,nginx默认web根目录在 nginx安装路径下的html文件夹,我们把他改到/www/web目录下。 进到/www/nginx/conf目录下,vim nginx.conf,将
# N- O! A0 P" p; d6 n2 w- T location / {/ F; w! }& A+ J* Y+ \# U
root html;; @9 |* T0 T2 F' c7 [8 w O
index index.php index.html index.htm;6 |9 d$ w* j4 t4 H6 H6 x
}
3 x, \: u; [& U( ^修改为:
; [4 Y0 j! g! a) R/ `
" t1 _9 z# G3 P* V, M location / { root /www/web; index index.html index.php; } 注意,增加了对index.php的识别。将 , B; P% C- M D6 m
location ~ \.php$ {
2 Z. W( x7 h' O' j/ t- ~' s root html;# ^, z% g& [( V) x; I6 p
fastcgi_pass 127.0.0.1:9000;/ n) `# b! f- s/ N# m
fastcgi_index index.php;
9 o, B( z+ L7 I o- l; G fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;" S2 g. U* Y% x; _! ~+ G+ I
include fastcgi_params;
X9 y2 x4 w m7 F/ i/ k }7 H4 Z! R( Z2 Y+ i# R
修改为:
; i# ^4 C5 `4 d' h: C* V" w, ?* t: x) T8 w% S1 z' }% M& e
location ~ \.php$ { root /www/web; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #include fastcgi_params; include fastcgi.conf; } 然后就ok了。第二个问题跟php的整合,待安装完毕php后再整。 三,安装MySQL解压缩并进到源码目录,执行:
2 k$ ^) s4 O2 ~6 [" {" Q$ o7 V#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql 之后make make install安装。安装完毕后需要做以下几个事:1,检查/etc/下是否存在my.conf, 如果有的话通过mv命令改名为 my.cnf.backup ps:此步骤非常重要!!! 2,创建mysql用户和分组 #/usr/sbin/groupadd mysql
2 n# K1 s* v, W& u O8 F- y/ f' C T# v" V0 }
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。6 J& i' D- G, I1 J. p) }
执行 cat /etc/passwd 查看用户列表
1 Z1 N6 q j" O3 o( x, n' \cat /etc/group 查看用户组列表 chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。
( [6 t+ g4 F4 j( Z) W 3,进到/www/mysql,创建系统自带的数据库。 scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
" S7 @, x' `+ v% d6 y# B( [! k 4,添加服务,启动MySQL cp support-files/mysql.server /etc/init.d/mysql
4 c( W2 L2 D+ h1 H) k& ^chkconfig mysql on
8 Y5 r7 \+ W7 O/ C2 Z% C, `service mysql start --启动MySQL
! `2 j" I6 P# L 5,设置root密码 为了让任何地方都能用mysql/bin下的命令,vim /etc/prifile 添加: PATH=/www/mysql/bin: $PATH# Q+ t6 v7 }+ }" w' t5 O
export PATH3 V% x( U& k: M6 }$ B
保存后source /etc/profile 执行: 1 Q) e( n- ?+ t; I/ V$ i
mysql -uroot mysql> SET PASSWORD = PASSWORD('root');
. i) J$ j- q* ]/ V; o# l6 c
# ]. S, L4 W8 e/ b设置root用户的密码为root。 6,为了支持远程访问数据库,执行; . [. o; L" h+ y% Z* p. F1 k0 R5 r: s
mysql> grant all on *.* to xroot@"%" identified by "xroot”; mysql> flush privileges; //更新权限 这样就创建了一个用户名为xroot,密码为xroot的用户,可以远程访问数据库。 四,安装php(php-fpm)解压并进入源码: / W% w9 B7 @6 d( S9 {% t
#./configure --prefix=/www/php --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-openssl --with-libxml-dir --with-zlib --enable-mbstring --with-mysql=/www/mysql --with-mysqli=/www/mysql/bin/mysql_config --enable-mysqlnd --with-pdo-mysql=/www/mysql --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --with-curl 然后make make install。接着需要做以下事: 1,整合nginx,启动php 进到cd /www/php/etc/ 目录下,拷贝php-fpm.conf.default 为php-fpm.conf。执行/www/php/sbin/php-fpm start 启动php-fpm。 2,配置php.ini 将安装源码里的/www/software/php-5.6.14/php.ini-production 拷贝到php的安装目录的lib文件夹下。 3,如果需要安装curl扩展的话(上面的configure已经带了),进到源码ext/curl目录下,保证电脑上已经安装了curl和curl-devel,然后: a,/www/php/bin/phpize 以下,为了方便可以把这个目录加到/etc/profile里: 7 P9 F) E4 J! ?* J0 k6 C
PATH=/www/php/bin:/www/mysql/bin: $PATH export PATH b,./configure --with-curl --with-php-config=/www/php/bin/php-config之后make make install,curl.so会生成在 /www/php/lib/php/extensions/no-debug-non-zts-20131226目录下,然后编辑php.ini找到extension_dir和extension修改即可。 使用 yum 比較方便: 以下是針對 centOS 6.x/x86_64: yum install php php-mysql php-fpm php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel 以下是針對 centOS 5.x/i386: rpm -ivh http://mirror.yandex.ru/epel/5/i386/epel-release-5-4.noarch.rpm8 W$ j4 ]7 f3 }" a! M) t4 g
$ F6 v$ e" R6 z
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
( H& _& I2 U) X* kCreate /etc/yum.repos.d/php-fpm.repo file and add:) Q6 p& y' g, z4 N: R* l y
- [nginx]
+ C' \: @! h/ f6 ~; e4 K - name=nginx repo
; D5 i. W/ x, m# S. C - baseurl=http://nginx.org/packages/centos/$releasever/$basearch/$ v, a5 S4 n/ ^4 P8 h' d, W
- gpgcheck=00 I4 Y8 g4 _) ]% I% N0 l; z
- enabled=1
複製代碼
6 g y$ o2 h+ h9 w8 L5 Hyum --enablerepo=remi,remi-test install php-fpm% q( y# b# f- h3 j! J
1 m' a7 `3 |7 E H+ f u2 k
+ q. r2 b3 r3 ^
五,安装svn配置post-commit此步作用是代替ftp,方便开发人员开发并同步代码。可以直接通过yum安装即可。 # rpm -qa subversion //检查是否自带了低版本的svn
. U' Q S2 }/ y: z1 I# x6 | #yum remove subversion //卸载低版本的svn
' S+ V2 T! M4 Y% ? # yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql //安装svn
+ p; \9 q% N" e3 F7 h 通过# svnserve --version验证是否安装成功。接下来就是创建仓库并与web目录同步。 1,mkdir -p /www/wwwsvn 此文件夹就是svn仓库. svnadmin create /www/wwwsvn 创建仓库,执行上述命令后,可以发现里面有conf, db,format,hooks, locks, README.txt等文件,说明一个SVN库已经建立。(ps:此处可以先通过svnserve -d -r /www/svndata 建立svn版本库目录,然后svnadmin在svndata目录下新建仓库) 2,配置用户和密码 在wwwsvn下进到conf文件夹,里面有三个文件:authz passwd svnserve.conf均要编辑。 #vim passwd //设置用户和密码 [users]
1 I, u" r* X/ y2 v l+ f# harry = harryssecret
* [" B9 x5 J: R# sally = sallyssecret
! ]! W! W9 A! s! a; J7 e" awangning=wangning2 p& E% n" |, A f Y
yanzi=yanzi
& N6 f5 J7 z2 I2 ~- `% d #vim authz //设置权限 8 U X8 y. H( j% p( [4 B
[/] wangning = rw yanzi = rw # &joe = r # * = #vim svnserve.confanon-access = none
3 N) s6 @& A+ Sauth-access = write
& |- Z; S0 y; t0 t### The password-db option controls the location of the password" T" j6 G9 n* s1 a) d+ F F
### database file. Unless you specify a path starting with a /,& t7 c+ k) I5 j3 R
### the file's location is relative to the directory containing+ _+ F) ?7 G6 E# r }* N
### this configuration file.
( K4 m Y: W ~1 j5 u1 l; { y7 a### If SASL is enabled (see below), this file will NOT be used.* S. T( } a- |+ h
### Uncomment the line below to use the default password file.7 ]' a3 t4 a* T7 o& h
password-db = passwd
' s1 a# _2 ^# |### The authz-db option controls the location of the authorization8 X: p1 ? h! O( x/ H! I
### rules for path-based access control. Unless you specify a path
. U& M" Q. T3 |/ q8 N- F6 r### starting with a /, the file's location is relative to the the
" w# K' m- M k7 v' {, |### directory containing this file. If you don't specify an
) W( G8 |6 J4 h& t: q) W### authz-db, no path-based access control is done.
3 F4 L* c, ^) P- @+ b: f: F### Uncomment the line below to use the default authorization file.
' m& {' T+ M' P8 _authz-db = authz+ I: ?2 k. v* l: a
### This option specifies the authentication realm of the repository.
4 z8 @3 S' f' L n% L9 P% P) U### If two repositories have the same authentication realm, they should
1 h9 H4 H7 [. ^2 ]### have the same password database, and vice versa. The default realm
7 D$ _) @6 {% G Y; I### is repository's uuid.8 R2 D* r3 {7 _$ A3 ]3 I: ^ Z! O) T" E
realm = My First Repository b- g; G: T" D3 Q
注意:上面这些有效行前面不能有空格。 3,启动及停止svn #svnserve -d -r /www/wwwwvn //启动svn' z5 s# W/ g3 d0 A; n5 o
#killall svnserve //停止% I0 i8 w r: \6 A X+ g
待启动svn后,可以在外面测试了。 svn checkout svn://192.1.15.222 --username xxx8 D v0 ], a2 s, r2 x* x
4,配置post-commit 经过上述配置后,svn的仓库地址是/www/wwwsvn, 但是web的根目录是/www/web,两者不是一个目录,无法svn push上来就看到作用。 a,首先在server的终端里,#svn co svn://192.1.15.222 /www/web 记得将/www/web目录权限修改为www:www。 chown -R www:www /www/web
) c6 _2 W" R7 [" w6 I/ N b, # cd /www/wwwsvn/hooks/,然后cp post-commit.tmpl post-commit vim post-commit,在里面输入: $ x; O6 o& Q; r6 R3 M
export LANG=zh_CN.UTF-8 svn up --username yanzi --password yanzi /www/web/ chown -R www:www /www/web/ 然后就一切ok了,在外面svn commit看看web目录里有么有对应文件吧! ps:1,svn up后面的名字和密码是之前设的svn用户。 2,上面up就是update的意思,按git的意思来理解,就是有个仓库A,然后新建了个B去跟踪A,每次A有提交的时候,让B也pull一下过来。在svn里是update。 3 , 裝完nginx php-fpm 須將 SElinux 安全政策關閉,否則網頁會出現 "Access denied" :
. c$ D5 X( w3 ]+ Y1 l( W#system-config-securitylevel 選擇DISABLE後按OK,必須重開機 ) d/ B' U4 m+ W3 \/ h& i
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能 vi /etc/sysconfig/selinux SELINUX=enforcing 改成--> SELINUX=disabled
: x" E* `9 Q, ]) o! t* C( fvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
) {8 {5 D) x2 W0 _8 c8 x+ @ |