參考自 : http://blog.csdn.net/yanzi1225627/article/details/49123659
1 @. O0 h6 x9 X: j; @% Z6 Q o4 R$ ` d- t+ Y0 J6 Z9 l4 o
目标:搭建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
( _! k, n- ? b7 e h 直接将所有待安装的依赖安装完。 然后下载nginx ,mysql, php的源代码: 可用 google search engine 搜尋最新版本 将这三份tar.gz文件通过scp命令弄到服务器上/www/software目录下。 二,安装nginx解压缩文件,然后进到nginx-1.8.0里,输入命令: ./configure --user=www --group=www --prefix=/www/nginx, j' H& D. R8 A3 K
然后make,make install就安装完毕了。 安装完后第一件事,创建www的用户和分组,否则会遇到http://blog.itblood.com/nginx-emerg-getpwnam-www-failed.html 的错误。 执行: /usr/sbin/groupadd -f www* g0 T2 t8 ]' g9 s
/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 G3 u/ i' @: W( p% D+ X9 n
# q4 N j. u4 s) n! |( [chkconfig --add nginx
9 G. r f2 n2 a6 J4 U7 P
! }, D6 D. ]: `8 H* F1 G" E2 qchkconfig nginx on. N0 v* K' ?: P% k \ `
然后 cd /etc/rc.d/init.d/ 目录下新建nginx,内容如下: #!/bin/bash2 i$ ~, Q$ L9 b# [9 N
# nginx Startup script for the Nginx HTTP Server @. |/ Y* r/ T9 R4 L
# it is v.0.0.2 version.
W5 F Z$ G9 v8 }8 a/ r# chkconfig: - 85 15. R3 `$ A+ }& b3 I+ B0 k
# description: Nginx is a high-performance web and proxy server. B# P* e) D9 E* j- q' Q8 R, d
# It has a lot of features, but it's not for everyone.
" L2 i/ Y- `2 j$ A9 p# processname: nginx
6 e4 \% j- _. y+ h J# pidfile: /var/run/nginx.pid
# X' {4 C [; f' i; G# q3 G# config: /usr/local/nginx/conf/nginx.conf; Y3 h: L3 c. m/ d" [8 i: |
nginxd=/www/nginx/sbin/nginx
+ m, K) c: A* _3 |6 anginx_config=/www/nginx/conf/nginx.conf
( X; z+ x* R0 g$ F- G2 i, A+ M$ e+ `7 ~2 Znginx_pid=/www/nginx/logs/nginx.pid! N9 V- ]/ i7 }% \2 ` v* r
RETVAL=0$ j0 H" _- m7 r6 ?
prog="nginx"' D# g- d5 y- x* ^& @6 \& l
# Source function library.
4 w S! k( Z) {1 _: M. /etc/rc.d/init.d/functions6 X5 Z9 U9 c+ T/ f8 K3 \
# Source networking configuration.
, w& h9 N+ U: x1 a: G/ C( s. /etc/sysconfig/network! `1 l: ^: `5 `: `# [; N3 A
# Check that networking is up.9 l, g- m1 a' |* B! {7 n" i+ T3 Y
[ ${NETWORKING} = "no" ] && exit 0% F9 U0 ~1 H1 w @
[ -x $nginxd ] || exit 0
: q8 Q( [8 f5 z5 B y# ^3 [# Start nginx daemons functions.2 G3 o4 V# l2 R+ `9 _) M2 k; |
start() {. Y" g/ s# w5 t4 _7 f- w) J0 W
if [ -e $nginx_pid ];then+ V& e/ u0 h3 ~8 B7 ~
echo "nginx already running...."$ C5 X9 X& o' q1 R6 j7 x3 z. b
exit 1
1 x9 [3 S/ y' C5 \, yfi
4 G7 Q0 x7 S2 M; w, g! vecho -n $"Starting $prog: "/ }. W3 O" z: [! V
daemon $nginxd -c ${nginx_config}# y5 `( q9 z9 [: F
RETVAL=$?
" k+ h3 X9 t2 ?0 F6 H1 ^6 }echo
7 Q0 e2 t4 m. K1 Y- a: J[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
" i% r) i N# _% x/ Mreturn $RETVAL
+ M+ [$ b/ Y4 B; n}. K- p3 u( |. s, [* A
# Stop nginx daemons functions.6 c, j* F/ y4 @4 {2 t G
stop() {
- r: P6 _1 V" A, recho -n $"Stopping $prog: "
. Z- g9 x D: A4 rkillproc $nginxd% p5 y# [/ {# d! i; G
RETVAL=$?) r. |* y5 c- R. d1 Q5 v2 E$ j1 V
echo; }' B" G" ~* H
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
1 z# M. R) q3 x' d: `/ t: K}
! H& l) {) Q! n k% q# \. ~- Oreload() {, I! |4 K. f4 `' _4 G8 t Z S ^' r
echo -n $"Reloading $prog: "
# L2 x- a o: ~. ^5 I3 Z#kill -HUP `cat ${nginx_pid}`
3 F: P) [! U2 b5 Bkillproc $nginxd -HUP7 q3 c0 {" x% u
RETVAL=$?, @; B2 P1 x6 e1 C+ X6 i
echo
9 t4 f8 Y# u0 y9 v0 Z8 w}
0 N$ `5 b1 J; U0 ~4 N- g/ d# See how we were called.5 U8 o! m, d+ @- Y9 N
case "$1" in
C" \# g8 R; ~7 P1 ]( istart)
1 y1 m: x$ E& d5 E0 |! e5 Wstart
n% Q6 z' O9 G' |. m% P;;
# f4 p( _6 n4 T" Lstop)9 a( b% e3 x2 |* H7 W
stop
; @! u. A! b* f" O;;
2 Q% U3 {7 G/ k% q- R- ]0 c% Y: Qreload)" o: f' l6 d/ O, z
reload
% I4 U6 u T0 |8 j8 S. N, s" F0 S;;
+ H0 a) y$ V1 h1 e4 z/ jrestart)! x4 r- U* S: D" f6 g7 E
stop
6 s( O7 A% ?& nstart
0 ^% n0 Q. p. D, }8 M+ i;;
( c) M& E% g: }8 M3 ^status)
, u; g- B* v9 D+ `* O4 T- ostatus $prog
0 M T& Y: u* T) SRETVAL=$?; k0 _) N3 R8 K/ N* J& i. n
;;
# B! y" J% n& F: B5 @% O*)4 e( p. t% T3 {' I; u. Z0 ?/ e8 t
echo $"Usage: $prog {start|stop|restart|reload|status|help}"9 E5 Z- g. P' }6 ^" M
exit 1
4 y! c7 M8 V7 Z6 e! ]esac
6 Q! ?7 L/ M* W8 t9 O7 h' Iexit $RETVAL
) J# r. X. `2 v6 V1 P9 N: m) P2 r+ j; D4 ]& b
注意:如果nginx的安装路径不是在/www/nginx下,则适当修改就好。 - r ?' ]/ ?8 ~1 T' F( A' K
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:" B; ^' a, U8 z6 J
1,是更改默认web根目录在/www/web的问题 2,是与php的整合,默认nginx是不认php得 对于1,nginx默认web根目录在 nginx安装路径下的html文件夹,我们把他改到/www/web目录下。 进到/www/nginx/conf目录下,vim nginx.conf,将 8 k( L: n/ Y8 q [' @) r
location / {
# w: {% D5 ^, K! Y! {+ h1 m root html;8 B) t9 s+ e* c9 m+ }
index index.php index.html index.htm;3 y! \ w8 ~# f# d3 W
}+ p- Y0 C* k" L( F7 _* ?
修改为:, z. d; ~/ A/ C" X# S& ?/ a
0 _7 c' d4 ~' I8 w! [) A location / { root /www/web; index index.html index.php; } 注意,增加了对index.php的识别。将
$ C9 V' p) J6 i( C. Flocation ~ \.php$ {
+ |; `3 i$ J. Z% C0 l+ C4 \ root html;3 j( f) W+ b( v* i% a* O
fastcgi_pass 127.0.0.1:9000;
3 B7 z) P, B( M' H' l fastcgi_index index.php;0 _% X+ k2 F2 t: R
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# \( f7 C N; Y/ B0 l2 ^1 e; } include fastcgi_params;
* M" R) H; T6 \3 T) Q6 h. K. ]* j }
/ b4 ?, k! C. t. R3 N; E修改为:/ ]) o: l8 B* \. k7 w
% } q, L, g( h 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解压缩并进到源码目录,执行: - I6 Y$ J" @; |5 a# n( P5 {+ r4 X
#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql 之后make make install安装。安装完毕后需要做以下几个事:1,检查/etc/下是否存在my.conf, 如果有的话通过mv命令改名为 my.cnf.backup ps:此步骤非常重要!!! 2,创建mysql用户和分组 #/usr/sbin/groupadd mysql
, N$ p# ?4 o$ H* Q8 [# D5 Y# R# x: _
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。 Y9 F3 K+ Q( B1 p
执行 cat /etc/passwd 查看用户列表4 f7 d) s/ h( A I! O0 L: C, z
cat /etc/group 查看用户组列表 chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。
5 K! X/ f: o: B3 z4 f! d& J6 D2 d- A 3,进到/www/mysql,创建系统自带的数据库。 scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
" y* X9 V4 {- y* j6 F 4,添加服务,启动MySQL cp support-files/mysql.server /etc/init.d/mysql
7 [, m: E/ \( h% L1 z0 K) rchkconfig mysql on
( \0 `2 c- h! }6 E3 G! F8 E& gservice mysql start --启动MySQL" I! H. w9 S" g0 t
5,设置root密码 为了让任何地方都能用mysql/bin下的命令,vim /etc/prifile 添加: PATH=/www/mysql/bin: $PATH
5 e8 M$ }, v$ \8 R+ l2 ~7 dexport PATH0 Q9 |, N& D G8 |2 p
保存后source /etc/profile 执行:
. S2 [: o, E X9 G. cmysql -uroot mysql> SET PASSWORD = PASSWORD('root');
7 G( z/ l/ t) t( H. z, b* W2 N! y% x- d+ U" Q
设置root用户的密码为root。 6,为了支持远程访问数据库,执行;
) n% p% r0 Q" N4 ~6 [mysql> grant all on *.* to xroot@"%" identified by "xroot”; mysql> flush privileges; //更新权限 这样就创建了一个用户名为xroot,密码为xroot的用户,可以远程访问数据库。 四,安装php(php-fpm)解压并进入源码:
O3 R. `1 W( }#./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里: 8 G) V' P7 P g# x$ p
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
8 X( b& Q3 N2 T( d! ]3 U) c1 M$ t1 q- N) [/ h" @( K
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm) n; {0 b' c" }+ e
Create /etc/yum.repos.d/php-fpm.repo file and add:
7 j! e) _ N5 g% {- [nginx]+ z9 t7 Z4 E, j* S6 O
- name=nginx repo, P* o3 u! W6 p4 q
- baseurl=http://nginx.org/packages/centos/$releasever/$basearch/0 F) f: U, I( p) M
- gpgcheck=0
4 {- M! m+ b s- _1 v0 F) U - enabled=1
複製代碼 $ G5 |4 |* p. t& p7 s) _% @& w
yum --enablerepo=remi,remi-test install php-fpm# Z0 W- U5 g, D3 M4 y" o% r
: s6 ?9 D6 n& E% A# |2 M4 X, q; M
7 R' m& [" k6 Y3 i' c2 E
五,安装svn配置post-commit此步作用是代替ftp,方便开发人员开发并同步代码。可以直接通过yum安装即可。 # rpm -qa subversion //检查是否自带了低版本的svn0 y; `" k: K! l( t2 q/ I: x! G
#yum remove subversion //卸载低版本的svn
) \* {0 U' M Z" V6 u # yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql //安装svn# q0 N( u" m$ G4 l% ~1 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]: U& M) h- C9 u; k: p
# harry = harryssecret
- U8 l/ E; e: l. h0 j# sally = sallyssecret
. y4 ^# o4 @9 u3 r" ewangning=wangning
9 k7 a$ Q* B$ l3 y& ~yanzi=yanzi
# N" A P. {: l7 S5 M' a9 _ #vim authz //设置权限
2 m7 t: W% ]! r* \2 A[/] wangning = rw yanzi = rw # &joe = r # * = #vim svnserve.confanon-access = none: J& j& a) e6 n0 i. I3 L
auth-access = write3 R# ~* B: Z& {1 w3 N9 k- l
### The password-db option controls the location of the password
; ~' {. Y; q6 ~8 D1 _4 |, h2 V% p### database file. Unless you specify a path starting with a /,
5 a0 \; }: n" O5 e9 n/ R+ t' n! J" W% {### the file's location is relative to the directory containing5 \* J& d/ h8 T u9 i% _2 M$ G
### this configuration file.7 g' j; z5 f! @, Q: ~7 |
### If SASL is enabled (see below), this file will NOT be used.
3 T- x$ @9 M9 b### Uncomment the line below to use the default password file.
2 {% j; ]( {" b! Epassword-db = passwd
5 ~- y+ D9 @' O) u% s I+ q### The authz-db option controls the location of the authorization
" b6 _5 Z# f" Z: n### rules for path-based access control. Unless you specify a path
1 r2 ~0 [" f- C0 ~1 U3 J' p5 O" V### starting with a /, the file's location is relative to the the" \2 } S$ `7 u/ o% k- ^
### directory containing this file. If you don't specify an; z: d) a" R, |2 b) K$ y( O
### authz-db, no path-based access control is done.
* k8 k) P# b5 {1 ] A( M### Uncomment the line below to use the default authorization file.
8 y5 T& p2 w0 T6 j* D$ T% q6 z/ Z( Uauthz-db = authz# g: J: W7 S% x3 T% R
### This option specifies the authentication realm of the repository.
+ H8 |7 s$ V" r### If two repositories have the same authentication realm, they should2 w& o/ j0 @ y
### have the same password database, and vice versa. The default realm$ u# p6 U( V4 w$ \1 E& V+ k" K% c! f
### is repository's uuid.* u% p, N9 q9 g9 A
realm = My First Repository
: u: @1 U# g4 @6 y 注意:上面这些有效行前面不能有空格。 3,启动及停止svn #svnserve -d -r /www/wwwwvn //启动svn; B7 ]7 r9 v* m* f
#killall svnserve //停止/ W% p7 L7 d+ {/ t( t8 @
待启动svn后,可以在外面测试了。 svn checkout svn://192.1.15.222 --username xxx5 f, U a, k! A! d4 Z$ S
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: F" C. W; j$ P& m
b, # cd /www/wwwsvn/hooks/,然后cp post-commit.tmpl post-commit vim post-commit,在里面输入:
0 W! m$ s/ N1 Mexport 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" :
0 P2 s" \5 a2 O- v D6 {#system-config-securitylevel 選擇DISABLE後按OK,必須重開機 7 ^! t- J! f! Z$ x
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能 vi /etc/sysconfig/selinux SELINUX=enforcing 改成--> SELINUX=disabled
% Q& \) d4 \- H+ Ivi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
, x; z0 h& o- j: ]" z* ?/ E |