tid=21242&參考自 : http://blog.csdn.net/yanzi1225627/article/details/49123659* Q Z; m8 C W/ c8 {& `
" J z, j9 [, N+ a! z/ \4 P/ t
目标:搭建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
9 h' L# z, p* O2 w7 d( n 直接将所有待安装的依赖安装完。 然后下载nginx ,mysql, php的源代码: 可用 google search engine 搜尋最新版本 将这三份tar.gz文件通过scp命令弄到服务器上/www/software目录下。 二,安装nginx解压缩文件,然后进到nginx-1.8.0里,输入命令: ./configure --user=www --group=www --prefix=/www/nginx
2 U# w6 f: c5 @ 然后make,make install就安装完毕了。 安装完后第一件事,创建www的用户和分组,否则会遇到http://blog.itblood.com/nginx-emerg-getpwnam-www-failed.html 的错误。 执行: /usr/sbin/groupadd -f www
7 p# M$ h; ]/ q. U' T+ L& J5 y/usr/sbin/useradd M -s /sbin/nologin -g www www //-M 不建立使用者目錄 nginx命令在/www/nginx/sbin/下,拷贝到/etc/init.d/一份,接下来设置开机启动。 chmod 755 /etc/init.d/nginx
9 C! {) I% n7 f5 H
# C* @; @( f+ A& Q) [9 C1 Z( K: I6 Fchkconfig --add nginx) o$ H0 W/ F( V! u, ^
' e% Z2 i6 \; m( A( B- A! u: Ichkconfig nginx on; p z9 {* \2 v6 V: o
然后 cd /etc/rc.d/init.d/ 目录下新建nginx,内容如下: #!/bin/bash/ s- D w2 M6 t4 M3 g7 S+ J" o! v
# nginx Startup script for the Nginx HTTP Server
0 Z2 R1 M1 H0 r# k# it is v.0.0.2 version./ C6 s3 z( b# Z P7 @' M
# chkconfig: - 85 15
5 i" s5 [! ] g3 A) o# description: Nginx is a high-performance web and proxy server.! A/ l4 z! v l' e* ~1 T7 V0 [& z/ K
# It has a lot of features, but it's not for everyone.3 H3 p- }$ s% f; x; Z- x) U$ K9 C
# processname: nginx" @, p" M* j& [; P" Q7 S0 R
# pidfile: /var/run/nginx.pid3 Y$ F$ Z( [2 d) O5 k
# config: /usr/local/nginx/conf/nginx.conf- C2 K( a$ P2 [8 J/ q. l% C1 W
nginxd=/www/nginx/sbin/nginx& O+ Q Q4 v; D" t2 |+ F- U
nginx_config=/www/nginx/conf/nginx.conf
1 h: p; ]# r- C' O/ _nginx_pid=/www/nginx/logs/nginx.pid
. ^3 I: L {& F( W8 s7 K1 h- @, vRETVAL=0
. H, w, I+ N- Q C9 l( b" F( G) k" `9 ~# Wprog="nginx"
5 P' r3 H& V0 _# Source function library.
* c! t/ \* |8 c! \. /etc/rc.d/init.d/functions% x1 e5 _& M& s: o4 G6 _- e/ @
# Source networking configuration.. S. M5 s r) ?! P/ A) B- T) z) j7 S
. /etc/sysconfig/network; a0 E# j2 c" R2 z* F1 L: ~
# Check that networking is up.% u( C% }4 n; B2 V! u: i' X
[ ${NETWORKING} = "no" ] && exit 07 u) \$ Q) Z+ B4 P
[ -x $nginxd ] || exit 0# @, d% G- |/ E4 k$ x
# Start nginx daemons functions.2 s# u. ~( |) V: x- l( J- m
start() {1 @9 k$ O/ U r& \- V0 t* M
if [ -e $nginx_pid ];then! U) T8 i+ m* d4 v% @/ i
echo "nginx already running...."
! t) c1 l8 F/ y4 |% h, E4 eexit 1
1 y) E- N) u& vfi$ M) P1 x# q2 [: u7 A! a8 @' r2 x* R
echo -n $"Starting $prog: "
3 ~( B/ j3 c7 i% e* \3 }daemon $nginxd -c ${nginx_config}* | a1 d: n% E* [
RETVAL=$?
r% ~ k+ I* L/ e8 `" w' u" zecho
( q/ F( s# C$ Z4 F. Q+ W* z[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx; _$ o! |9 y7 p' D4 |
return $RETVAL: j0 b7 }. o, d3 T
}, T! c1 ?0 y% b7 U" |
# Stop nginx daemons functions.
& m7 v/ @4 `) i+ x0 `stop() {) R& v) W5 k6 i, g$ Z2 o
echo -n $"Stopping $prog: "
* l) P. L+ E8 @killproc $nginxd
6 Q6 E9 C0 B/ S+ k; o$ u% r- s! URETVAL=$?0 }" u) A- ^8 z! b2 g( @
echo; q2 d2 s* z/ o& \6 ~% L* D* ]* G
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid7 |6 U; Q3 J2 r' M6 x
}: w: H0 `& n+ }' q: c
reload() {
$ j& `5 N( ^- w8 ?& ^2 {! Lecho -n $"Reloading $prog: "
5 J: Y* V& |5 c( g; r9 r5 V. H#kill -HUP `cat ${nginx_pid}`/ R8 [- Z3 q! V6 H
killproc $nginxd -HUP
2 n: p2 F5 D. hRETVAL=$?
8 j* c5 r3 B ^" }. lecho1 M, O) e0 e2 g5 q6 K# O
}
4 g, E( s9 |% u* q* V# See how we were called.* V, p9 K. `* h: ~
case "$1" in# r: h: o' D2 r* F) c4 ~
start)
6 P$ W0 q( T( `7 {start9 H) W4 _" b" g( [! j
;;
/ R" o" f" l1 C/ m4 Hstop)+ e V M' G' |; T
stop
$ |2 M% `1 q* d- g5 }) l;;0 Z* Z5 I- m9 g1 k# b6 [1 g% m
reload) b [. p5 [& Z$ G K
reload
/ h" Y' g9 I$ r- e. E$ i$ L;;
( L" x; U6 V) `restart)
+ g G" @2 {5 f, P0 C" P0 ~stop
2 M; o& B i5 j! a. M8 astart8 p( \% v2 G, n
;;
0 J3 x# t9 W8 G3 Nstatus)
6 ]( |$ Z" r, r$ Ostatus $prog& [0 }! |# r% X3 Z9 n
RETVAL=$?
c m3 m- [9 L& L5 I;;* Q+ Z' }) M. ], F3 c
*)
/ N5 _- J1 M6 S; C) s; G$ f6 Xecho $"Usage: $prog {start|stop|restart|reload|status|help}". t% ^5 C( N1 u# }9 U# ~1 x# E
exit 1
5 r9 o" x( K5 c. S( b. D- u0 a" ?esac
2 V0 k% ]7 S) C4 F$ Rexit $RETVAL
9 H2 r* B: J3 ]6 f! p4 B! H0 o2 N: e+ W6 t; v& W6 c
注意:如果nginx的安装路径不是在/www/nginx下,则适当修改就好。
7 R5 K3 T3 p1 \9 P1 achmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:: n8 F5 q5 ?5 Y3 t4 i
1,是更改默认web根目录在/www/web的问题 2,是与php的整合,默认nginx是不认php得 对于1,nginx默认web根目录在 nginx安装路径下的html文件夹,我们把他改到/www/web目录下。 进到/www/nginx/conf目录下,vim nginx.conf,将
, K" k" y" F q3 E location / {. k/ O$ P: K5 r/ L5 `
root html;' x' s4 m/ |. e- j
index index.php index.html index.htm;5 G+ J6 W ]# m) m
}! o. n7 J4 ^0 `6 m
修改为:
- } N# F( [5 z2 F! l7 H4 F* F3 G+ K( d p6 V: p* }7 \ H( E% u
location / { root /www/web; index index.html index.php; } 注意,增加了对index.php的识别。将 & X& |/ M0 h- Q8 E! }
location ~ \.php$ {5 E8 f* K$ G" `
root html;
3 `( T9 |7 Y" \# b& |; r fastcgi_pass 127.0.0.1:9000;
! L# B% O; m; k# y( v3 w fastcgi_index index.php;
5 }- b. i. M% X) x fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
: p2 b. k1 m7 S2 a1 \: t3 T include fastcgi_params;
& T* g% j0 x# p5 S! v, F) x; M }
6 l, N V W3 S8 a, W! e修改为:3 r# t3 G0 l I4 I& s6 ]
+ C! |' g# B+ J9 `
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解压缩并进到源码目录,执行:
9 W- e! Y; t$ m5 W" v5 H% e#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql 之后make make install安装。安装完毕后需要做以下几个事:1,检查/etc/下是否存在my.conf, 如果有的话通过mv命令改名为 my.cnf.backup ps:此步骤非常重要!!! 2,创建mysql用户和分组 #/usr/sbin/groupadd mysql" n0 s: [3 m' Z5 Q' Z h1 B4 C. s
0 f/ Z1 E; Y$ P4 ^# W3 c4 V#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。
+ v9 h4 K+ Q% q7 t. u/ w 执行 cat /etc/passwd 查看用户列表
+ L6 D# A# ]) B! Y7 h: a1 T3 ~4 Acat /etc/group 查看用户组列表 chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。
6 O# r1 @" G2 U+ O$ ?. p N 3,进到/www/mysql,创建系统自带的数据库。 scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql1 o3 y7 l7 `7 e- t
4,添加服务,启动MySQL cp support-files/mysql.server /etc/init.d/mysql
3 L4 u7 P) Q5 e% J. \chkconfig mysql on
; C) f ^6 _1 Eservice mysql start --启动MySQL# e9 ` x% S7 q3 C1 A; f1 q
5,设置root密码 为了让任何地方都能用mysql/bin下的命令,vim /etc/prifile 添加: PATH=/www/mysql/bin: $PATH$ m; R7 U- A- N" X( ]( Z$ r0 [: x* ^
export PATH; Y6 M: _6 T- A% Y/ \
保存后source /etc/profile 执行: 4 W1 c: w" _( e4 P; |% _- X1 f
mysql -uroot mysql> SET PASSWORD = PASSWORD('root'); @) r# }; n- Z& Y. U) S* N- y
& p: T' X( ` X" W) g
设置root用户的密码为root。 6,为了支持远程访问数据库,执行; 0 F2 k/ I; }+ C$ @1 M! Y7 ]
mysql> grant all on *.* to xroot@"%" identified by "xroot”; mysql> flush privileges; //更新权限 这样就创建了一个用户名为xroot,密码为xroot的用户,可以远程访问数据库。 四,安装php(php-fpm)解压并进入源码:
3 f% h I7 V/ p# q& z5 u' E#./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里: Y. a9 X T/ q3 l5 O" V9 x- ^4 _
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.rpm' f8 S k. j: Q/ G7 Y+ ?- o9 p
% i: j) j+ y2 n
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm' l, J& y/ C8 x' y
Create /etc/yum.repos.d/php-fpm.repo file and add:
2 v7 B3 l- I$ ^2 L+ V/ m- [nginx]+ x+ X( g2 U' q Q* i* m7 ?' c5 v
- name=nginx repo6 c; |2 M7 f1 b- y* g C- H
- baseurl=http://nginx.org/packages/centos/$releasever/$basearch/4 }/ c$ w C/ z/ y+ _ E7 O& a
- gpgcheck=0
" {# J/ t9 [1 u; y - enabled=1
複製代碼 " t s# \( U- q+ ~2 Q, R
yum --enablerepo=remi,remi-test install php-fpm
5 ?$ x I1 L" F/ b% }% _, _4 j$ N9 N# c! q: [1 T1 `* k
: Y. w0 O0 z5 U) Z, X8 G
五,安装svn配置post-commit此步作用是代替ftp,方便开发人员开发并同步代码。可以直接通过yum安装即可。 # rpm -qa subversion //检查是否自带了低版本的svn, p0 M) }+ g4 n* C/ G" M
#yum remove subversion //卸载低版本的svn: h$ G M( z- n3 u" q V! @
# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql //安装svn' G! D" v$ O& k* p5 k& v
通过# 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]
! J+ [9 Z# R* v- | v* H# harry = harryssecret4 @" d6 Z) Z7 ]( w0 o* M
# sally = sallyssecret
2 M; W ?+ f' o% w( ^+ Ywangning=wangning! u$ N! D- n! s( n& m
yanzi=yanzi
" u# b* ?0 ]4 I/ y' ` #vim authz //设置权限 , [: r& @- ^# T
[/] wangning = rw yanzi = rw # &joe = r # * = #vim svnserve.confanon-access = none8 w. a! Q. Y& A. \$ \" o( q
auth-access = write
. c( r) P6 o0 ~### The password-db option controls the location of the password" j% |6 u t+ _. @8 q% }5 }
### database file. Unless you specify a path starting with a /,8 H) u! }' t* l- O
### the file's location is relative to the directory containing
6 b) L2 D7 _, R. W T### this configuration file.. ?( l) i# Z# {3 G& p% U
### If SASL is enabled (see below), this file will NOT be used.
. f% f; @) ]8 i( s4 x: Q### Uncomment the line below to use the default password file.2 I, {) E4 ~0 z }* w% s6 `. U
password-db = passwd! T1 n+ z6 ]4 A( i
### The authz-db option controls the location of the authorization
. i, L) {& B1 H* o1 j### rules for path-based access control. Unless you specify a path
6 U# L: ^5 R! N) e### starting with a /, the file's location is relative to the the: \, J) S9 X5 h/ e8 ~" j8 B$ y: q
### directory containing this file. If you don't specify an; E4 I- O* r! o
### authz-db, no path-based access control is done.0 j! b. K. i3 }7 ]) f, N, Y
### Uncomment the line below to use the default authorization file.+ c9 s; V/ J* r7 z: a
authz-db = authz
) i* C4 e2 j$ l2 ]" {( }5 c. _### This option specifies the authentication realm of the repository./ `' [2 n' P2 K' D7 a) z/ n. l# w
### If two repositories have the same authentication realm, they should& [1 i' d7 X+ x) h8 \5 l
### have the same password database, and vice versa. The default realm! s7 u# k8 X4 N& p
### is repository's uuid.
, e9 r/ o7 i7 X% Yrealm = My First Repository
5 L8 u ^8 s' v 注意:上面这些有效行前面不能有空格。 3,启动及停止svn #svnserve -d -r /www/wwwwvn //启动svn# w/ H5 E" G7 ~8 B1 _. Q" \
#killall svnserve //停止
; |+ [2 U: H" e3 c U7 Y 待启动svn后,可以在外面测试了。 svn checkout svn://192.1.15.222 --username xxx0 ^8 h9 c2 o$ V# t/ `+ @; c! {
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
+ b$ a7 `" j4 l* p' M; R b, # cd /www/wwwsvn/hooks/,然后cp post-commit.tmpl post-commit vim post-commit,在里面输入:
) |# d/ F, n0 U I" y' |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" :
9 `, q- v% N: m. J. w3 N. G#system-config-securitylevel 選擇DISABLE後按OK,必須重開機 Y3 q5 e# E( s4 J) Z* B, J
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能 vi /etc/sysconfig/selinux SELINUX=enforcing 改成--> SELINUX=disabled * C/ O: ~% V: L
vi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
: D u4 O! A% N/ f3 g3 Y" K |