tid=21242&參考自 : http://blog.csdn.net/yanzi1225627/article/details/491236591 G" B& H0 X7 N
8 `& a* r6 y* f
目标:搭建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
( W5 X7 R; B8 k/ F: S1 r 直接将所有待安装的依赖安装完。 然后下载nginx ,mysql, php的源代码: 可用 google search engine 搜尋最新版本 将这三份tar.gz文件通过scp命令弄到服务器上/www/software目录下。 二,安装nginx解压缩文件,然后进到nginx-1.8.0里,输入命令: ./configure --user=www --group=www --prefix=/www/nginx' c% E" W; i9 l4 T
然后make,make install就安装完毕了。 安装完后第一件事,创建www的用户和分组,否则会遇到http://blog.itblood.com/nginx-emerg-getpwnam-www-failed.html 的错误。 执行: /usr/sbin/groupadd -f www
: v( Z7 O8 h3 E1 S7 z- y/usr/sbin/useradd M -s /sbin/nologin -g www www //-M 不建立使用者目錄 nginx命令在/www/nginx/sbin/下,拷贝到/etc/init.d/一份,接下来设置开机启动。 chmod 755 /etc/init.d/nginx0 ^4 e! C9 p% k0 J, k8 M- k$ ? W3 H: H
7 e* l& t8 Q- l. M7 echkconfig --add nginx
' I4 u! ^+ J m( `+ P! [& t) d6 p2 z2 e+ b
chkconfig nginx on/ q% T/ g2 ` x: I5 N
然后 cd /etc/rc.d/init.d/ 目录下新建nginx,内容如下: #!/bin/bash; `5 u0 [! x; X; p' d; _
# nginx Startup script for the Nginx HTTP Server
7 w- U8 @& C: H) |# E$ N, X+ o# it is v.0.0.2 version.
: Q q* w8 ?4 S2 }! w# chkconfig: - 85 15
, U0 y- E/ t h( ]# description: Nginx is a high-performance web and proxy server./ L: ^6 F* d+ ?0 h( V, X. Z) ^
# It has a lot of features, but it's not for everyone.
& y& j! l, j& W6 z9 i9 r& S# processname: nginx4 |- }/ g$ F: u h+ y
# pidfile: /var/run/nginx.pid
6 q5 a! x0 p0 @$ G# config: /usr/local/nginx/conf/nginx.conf! Z1 Q- u6 o( I5 @
nginxd=/www/nginx/sbin/nginx3 g% P8 q( c, N9 t
nginx_config=/www/nginx/conf/nginx.conf: e- W0 I8 n4 ]7 y
nginx_pid=/www/nginx/logs/nginx.pid% t* a% ~( `0 g8 H5 n/ m
RETVAL=0
# K; N! B9 W% W- b: T( R& x0 |prog="nginx"2 Q1 ~$ I; W1 G& _
# Source function library.
; B* t( G9 o6 z( k. /etc/rc.d/init.d/functions
! q# Z& D0 @ L' X# F# k& V# Source networking configuration.) V4 B7 _( w( ~. O& e- Q$ G( G4 b
. /etc/sysconfig/network8 S, H7 C& B* h; [" g% {
# Check that networking is up.
9 p' @" l. q# B. @8 n2 b[ ${NETWORKING} = "no" ] && exit 0
7 ?( S0 n" f) Z& _[ -x $nginxd ] || exit 0
: m" L9 k5 e$ F. Z/ @# Start nginx daemons functions.
; H6 C/ T ~: i- h$ G) |) {start() {
# i: J. i& c7 U& M/ yif [ -e $nginx_pid ];then
8 d1 E8 K: |- t9 ?) S% ]* c' Kecho "nginx already running...."
# a) h* i5 z2 Dexit 1" C( K5 K$ q/ i6 I# ~; E+ z' ?2 U
fi1 C$ j+ l* h( E1 L- M4 `& `
echo -n $"Starting $prog: ") L# ]8 B" U" ^/ g* @2 x
daemon $nginxd -c ${nginx_config}& z. v N8 i1 v, q1 V! M
RETVAL=$?( [# d# @; _$ W$ y
echo
) E. u4 B5 R; T[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
% J0 h7 H `" ?5 }1 ureturn $RETVAL! y! T3 U3 @ S/ d; ], H
}
' I" t# R- z1 g( Q' I* i5 G# _/ {# Stop nginx daemons functions.
9 ?6 M8 W$ A. hstop() {
1 R- r9 K; @" `# L! h+ n. }echo -n $"Stopping $prog: "
7 _3 C( i/ Z Q% @. pkillproc $nginxd* |% v3 T% i4 \4 Z( @3 r
RETVAL=$?
Z3 L" _6 C, c2 v7 ~echo
7 I+ {* ` ?* A[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
! N5 T- L* w9 t8 w$ [}
4 ~' ^ M, C1 w& h. D- b$ Areload() {& W$ P5 c( k4 t2 O8 B/ g
echo -n $"Reloading $prog: "
3 J: p! e3 t% J! r0 T#kill -HUP `cat ${nginx_pid}`/ O% j& F$ ~4 m8 J0 M4 d8 M
killproc $nginxd -HUP: C% M2 T7 `0 N) \) `
RETVAL=$?( R- g7 R. \( o$ \' }* e
echo
3 N/ } s3 o+ y5 @- p}
# E! w0 C) q/ R3 I2 [; e# f# See how we were called.3 [* _0 z$ Z. `7 Z
case "$1" in2 O1 Y* y6 V) _. x4 X' o
start)
2 Q4 g- b! c5 ?: J7 _" |5 J0 \! astart
' E+ b1 h- s- u' I;;
$ x; C2 i! B2 \ m5 lstop)2 ]2 M- c+ ]! G7 P- Y- D) |, ]7 E
stop+ u/ }1 K) n) y/ ^5 H/ M3 F, e
;;, H$ w& Z @& c' @ y; ]7 q/ Z4 c
reload)
/ x. f# m0 s" u) yreload; f7 l% ?' M( J, T" K
;;9 h4 O9 f; B: h# G- h
restart)
( @- J/ o; L3 Y4 Fstop4 k* S/ y, d6 V( J5 U
start
3 W N& V* |9 L# l9 A5 ^, \" H;;. i8 Y5 p& |- s7 C
status)% C) E% p6 b' L) {
status $prog
4 o5 N* h4 Y/ E: t( [4 hRETVAL=$?
E* ~6 _- E" p5 h;;" E" u7 u, x0 e( I8 l$ {: k6 i
*)) s- D( O+ |; i8 ]( s
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
( o. d8 d7 j% }exit 1) v9 c4 X+ Z) D1 V5 B
esac
* `* G0 M& @( @, ?exit $RETVAL1 R/ D r: ~& G
2 g* i: \6 ? K* R
注意:如果nginx的安装路径不是在/www/nginx下,则适当修改就好。 8 w$ r4 |9 V( g u& ]. U g( A
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:
5 x; s' R4 a0 L! M) r1,是更改默认web根目录在/www/web的问题 2,是与php的整合,默认nginx是不认php得 对于1,nginx默认web根目录在 nginx安装路径下的html文件夹,我们把他改到/www/web目录下。 进到/www/nginx/conf目录下,vim nginx.conf,将 # [" J2 @6 K$ f
location / {! @# Z1 g; `1 t" J& ^) _
root html;( k2 q: L9 r- n! O. ~
index index.php index.html index.htm;. Z' X0 h# M% ?" @; s' x/ q8 t
}( x" a+ \) s7 R% m
修改为: N6 `) U& P3 I4 k! v; g
4 N! D; W8 }. C; U
location / { root /www/web; index index.html index.php; } 注意,增加了对index.php的识别。将 ! L& {- q; T" @, s
location ~ \.php$ {8 t' O/ S! B6 k" ?! _
root html;! O4 ]8 H) Y. C
fastcgi_pass 127.0.0.1:9000;
, _, u# M+ H9 w* A fastcgi_index index.php;
* u! u/ W; g& S0 u+ U/ ?5 _9 X fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; t* r5 s3 t y. l. y4 c
include fastcgi_params;" i: \1 `# g% v1 c% Z5 W, _' d( _
}
6 ?. {5 F5 Z3 T& p7 `7 K修改为:
' ~5 H5 y% n/ g0 M% Y
1 B( f2 {3 M- {/ D$ k: N) _6 k 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解压缩并进到源码目录,执行:
0 N7 W2 A* ?: E/ j/ W' o$ _" l% j#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql 之后make make install安装。安装完毕后需要做以下几个事:1,检查/etc/下是否存在my.conf, 如果有的话通过mv命令改名为 my.cnf.backup ps:此步骤非常重要!!! 2,创建mysql用户和分组 #/usr/sbin/groupadd mysql
+ ]& A* J: A8 s6 r+ K- H6 u$ E6 x
% g" ~2 L( {) d" O- a9 u }( X#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。2 k* c5 L- z/ m6 b4 m# n2 x
执行 cat /etc/passwd 查看用户列表3 g/ s' M( |% l7 R! v3 T
cat /etc/group 查看用户组列表 chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。
6 W I" o; ^# `1 w4 d 3,进到/www/mysql,创建系统自带的数据库。 scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
6 Q# F1 g* b; O4 O# H9 u2 o0 F 4,添加服务,启动MySQL cp support-files/mysql.server /etc/init.d/mysql
& c4 ]: R$ Y6 `chkconfig mysql on
) \$ S8 W% @' a& @7 Hservice mysql start --启动MySQL! ?. q9 f! V- o
5,设置root密码 为了让任何地方都能用mysql/bin下的命令,vim /etc/prifile 添加: PATH=/www/mysql/bin: $PATH
) N9 f+ j# y/ Q3 U: `! I) Yexport PATH% V# c, P" P. e8 C6 R
保存后source /etc/profile 执行:
9 v, v( u Q( W- e3 x# F r' W9 Fmysql -uroot mysql> SET PASSWORD = PASSWORD('root');7 v/ _8 W# L- {3 g) N
/ {3 M7 _; o+ O8 n- k4 g; w设置root用户的密码为root。 6,为了支持远程访问数据库,执行; " ^8 W; N+ [, B+ X: I
mysql> grant all on *.* to xroot@"%" identified by "xroot”; mysql> flush privileges; //更新权限 这样就创建了一个用户名为xroot,密码为xroot的用户,可以远程访问数据库。 四,安装php(php-fpm)解压并进入源码: $ N% `2 t- z9 D6 y* N+ m( d Q9 g/ F% O
#./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里: $ c2 v0 F! P5 q. T! S0 ^
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
) v4 ^1 H% j+ `% e' ]- D
/ J4 }( z, L/ F& r2 q1 _% crpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm2 F+ Z- E) `8 g& o4 d/ W
Create /etc/yum.repos.d/php-fpm.repo file and add:
# C' U0 ~+ e6 h0 }- [nginx]
7 {! G- f5 ]) q7 } - name=nginx repo
Q! x1 [: I1 i; b) a \# u - baseurl=http://nginx.org/packages/centos/$releasever/$basearch/2 `* F q% K0 w% }0 w/ D
- gpgcheck=00 K' ]( f. i+ a, m5 _3 [6 ]! p
- enabled=1
複製代碼 S0 m- c# Z7 i. _& m* r
yum --enablerepo=remi,remi-test install php-fpm" Z9 v# c) H7 N( j S7 T* [
9 Y* O: N+ m- E) g
' y4 R U1 b- z$ M
五,安装svn配置post-commit此步作用是代替ftp,方便开发人员开发并同步代码。可以直接通过yum安装即可。 # rpm -qa subversion //检查是否自带了低版本的svn
) r* E. n$ ]: ?% @4 X# q6 N/ o #yum remove subversion //卸载低版本的svn
8 f/ ?. ~! X Z0 M Q$ l2 b3 j # yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql //安装svn2 n9 t' v- V/ B/ b8 ?' f( 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]
0 o r$ u- Y3 n& Y# harry = harryssecret' s4 P; m' a* m
# sally = sallyssecret) n w3 X+ Q) ]
wangning=wangning
0 g: @/ _# j4 Z3 ~yanzi=yanzi
1 b1 U I3 a/ f4 | #vim authz //设置权限 , q7 [7 N5 z! z5 s
[/] wangning = rw yanzi = rw # &joe = r # * = #vim svnserve.confanon-access = none
$ T3 p) H: G# @+ o! o& gauth-access = write {1 E2 d9 L5 d" b( [) s) v
### The password-db option controls the location of the password0 v' C$ n# [. w+ ~7 n6 m9 X& ]
### database file. Unless you specify a path starting with a /,
, I( h/ h( g1 t9 [0 _/ N### the file's location is relative to the directory containing
- K, T1 ^- l; }1 y$ W1 n! P" q6 e" H### this configuration file.% U$ n0 I7 e A; a4 J
### If SASL is enabled (see below), this file will NOT be used.% t. t4 \2 i6 Y
### Uncomment the line below to use the default password file.4 Y0 q2 L$ _+ v6 d1 r! O/ f- A/ Z
password-db = passwd
( b& N0 o( S: A$ o% z9 Y; X### The authz-db option controls the location of the authorization
$ w. J: |( U. j6 B& {7 C" d### rules for path-based access control. Unless you specify a path- x- J) U4 f7 @
### starting with a /, the file's location is relative to the the
! }4 w# Q: i+ i H( e### directory containing this file. If you don't specify an2 s" h" B4 o. q$ _% m$ Z9 z
### authz-db, no path-based access control is done.# v9 Y* k* Z; U: h8 o' I5 j* c
### Uncomment the line below to use the default authorization file.
! ]0 R" {% z' M( k& x+ }4 }, ?authz-db = authz4 k$ K* {% Q3 B2 F& ]/ v, B6 x
### This option specifies the authentication realm of the repository.
* e C+ z5 j+ p( F D& F1 C### If two repositories have the same authentication realm, they should
q8 k$ O5 e1 { z. P6 w### have the same password database, and vice versa. The default realm
9 n9 v- {: _6 _### is repository's uuid.8 m7 u* Q5 E$ _ N
realm = My First Repository
6 L1 x: L- C3 c# O' { 注意:上面这些有效行前面不能有空格。 3,启动及停止svn #svnserve -d -r /www/wwwwvn //启动svn
5 T* q/ t7 l% }# s #killall svnserve //停止
2 L. V% z, z, x- h# g& p* v 待启动svn后,可以在外面测试了。 svn checkout svn://192.1.15.222 --username xxx6 e9 U" P( t1 T0 s, a
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- I0 g- Y1 k: F6 x" i
b, # cd /www/wwwsvn/hooks/,然后cp post-commit.tmpl post-commit vim post-commit,在里面输入:
7 G1 q% f7 r. A8 j0 j* S( }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" :
" h$ b. w% r0 Y' n& \#system-config-securitylevel 選擇DISABLE後按OK,必須重開機
0 G" c. s, a1 z, j% j/ ` Z執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能
vi /etc/sysconfig/selinux SELINUX=enforcing 改成--> SELINUX=disabled
( C6 n& E1 R3 V/ p, xvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
' r1 P! R/ _$ V" a8 Y |