參考自 : http://blog.csdn.net/yanzi1225627/article/details/49123659
* |; u$ U* u; g/ v" A
- R. M* c' Y. X5 k( ~5 u" y- y% L目标:搭建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 cmake9 r$ i& P/ i! }$ Z# U% n* v; 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
; H9 l' `7 p# X; Q+ U* X 然后make,make install就安装完毕了。 安装完后第一件事,创建www的用户和分组,否则会遇到http://blog.itblood.com/nginx-emerg-getpwnam-www-failed.html 的错误。 执行: /usr/sbin/groupadd -f www
/ ^* |& W! j; M, d% Q3 l' Y/usr/sbin/useradd M -s /sbin/nologin -g www www //-M 不建立使用者目錄 nginx命令在/www/nginx/sbin/下,拷贝到/etc/init.d/一份,接下来设置开机启动。 chmod 755 /etc/init.d/nginx2 Z! J1 f( ~5 ]# A6 r5 ^/ ]3 O
7 W( c% z$ x( Mchkconfig --add nginx6 F! h0 c% ^$ f4 C
( S! S0 u- q% `1 d: Z4 E# w2 o
chkconfig nginx on4 E9 \3 v* l$ m9 o
然后 cd /etc/rc.d/init.d/ 目录下新建nginx,内容如下: #!/bin/bash0 g; r7 g" y' x8 d) D
# nginx Startup script for the Nginx HTTP Server& x' c/ f" Y" h' L7 _5 R' Q
# it is v.0.0.2 version.- {" n0 @1 v/ Y' [8 H% h/ N
# chkconfig: - 85 15
: m' T( P& ~$ v$ ?/ V3 _# description: Nginx is a high-performance web and proxy server. |2 Q" C ^' R4 X
# It has a lot of features, but it's not for everyone." C3 p. t$ E. @( R+ R% i
# processname: nginx8 [! l3 C, k/ [& N+ X* {( J
# pidfile: /var/run/nginx.pid! Z, M2 q D$ ~: \
# config: /usr/local/nginx/conf/nginx.conf4 l# P- H- O- |! V1 L% c. L6 z7 b
nginxd=/www/nginx/sbin/nginx# r; i6 n6 p1 U, c# [) C
nginx_config=/www/nginx/conf/nginx.conf
, `) L5 }- @* r1 n" [" {- z( l% Onginx_pid=/www/nginx/logs/nginx.pid
5 s& k9 H1 D, l, ~! z) j) ZRETVAL=0( c% s4 O% R) l
prog="nginx"- k& l7 {- L$ R* c- \
# Source function library.
0 k- x) t$ J/ ~9 h" I" ]2 l# u. /etc/rc.d/init.d/functions% ?( d- s! ~1 s3 O; X4 }
# Source networking configuration.
$ ?8 ]( h4 r5 v2 `* n/ o# S. o. /etc/sysconfig/network# S# ^0 Q9 X% e7 ?. K y
# Check that networking is up.
0 r2 c F$ U: }6 y2 ^! ^: m, ~! Q+ I# H[ ${NETWORKING} = "no" ] && exit 00 H4 q Z( b: y R" p9 }
[ -x $nginxd ] || exit 0
6 t$ q1 P7 u' E) d3 E# Start nginx daemons functions.+ ~% F4 W3 J6 E6 ]( ^
start() {
/ ]' B% n3 k5 E+ y! ~if [ -e $nginx_pid ];then
0 J! J+ D* D& b- W7 X. y3 hecho "nginx already running....") w7 u/ F3 T3 U9 X! ?' C. V( x
exit 1
# `! W% `9 P/ rfi
; h4 E7 A6 Y0 uecho -n $"Starting $prog: "
7 d" z: T% W( \9 Ydaemon $nginxd -c ${nginx_config}% o# X% U6 Y2 J" D
RETVAL=$?. D- E' w% @1 W+ H- D# N7 c
echo- i. v- W% X0 f
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
: V$ d* q3 q* z8 F: yreturn $RETVAL5 i1 e4 H: O- k' k: A& B) v
}
) [7 p( f6 g) p# Stop nginx daemons functions.- a# b& Y! U. ]' @0 ~9 O
stop() {! \$ c( ?% r, } M9 O3 y9 ~8 s
echo -n $"Stopping $prog: "
. {$ s& N2 o1 g/ }killproc $nginxd) w- x" m) s1 l3 E/ o3 i) `6 k7 o. m% a
RETVAL=$?% u/ v! q6 u: O l
echo
: x% t; b4 L4 E7 z z! M F; B[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
$ f5 v5 i$ z) E/ \; d9 |* A( ?}; ?/ S1 y) M0 v1 [0 {; g6 h
reload() {$ k4 O0 W/ G. o# H0 B4 q8 q& f+ `
echo -n $"Reloading $prog: ": ~) f- t1 S! ]! q! N7 {/ W
#kill -HUP `cat ${nginx_pid}`! r7 U3 } i3 k3 ]6 m5 l) T
killproc $nginxd -HUP* w! s6 Y3 z+ `3 d/ G6 c o* q
RETVAL=$?
/ {1 a# a3 K. E, Vecho9 x0 F9 R2 E# h1 x- O6 u
}, G4 H3 U7 Y* j: M
# See how we were called.
3 {( k6 m: P3 O! ncase "$1" in
" H$ c# k7 a) |6 C# Sstart)
; y! T; ?* Z0 B1 ustart
3 J& z. }% z) w$ n4 c;;1 m6 _! _- j8 H! u( R8 F" {6 Q ?
stop)" W M) z7 z, m" l5 M) P, V* j
stop
" v3 i( s; |) j" f: ^8 i% ?;;
- L2 U8 K. w) yreload)
, O- }* n7 s8 Qreload
8 F0 `: H6 o0 D+ ^0 L;;
- ?7 M5 M0 {( c# Rrestart)) ]- ^: _( Y# G; z
stop
3 Y6 W: R& H. j6 i" sstart
9 k0 {+ f! p1 g: M& q5 p3 e' W;;
$ L! a7 n6 j9 g* [! k$ s2 Vstatus)2 ]* X. h' I- k3 J/ y: W6 G
status $prog" n+ [3 q0 t+ k. K, ?, H
RETVAL=$?) `) j: a) X8 w; u! I {/ t' ~4 G) y
;;, H0 n7 U6 q, n, ~, U2 E' p; g
*)
, k: }; u4 H, @) M q+ oecho $"Usage: $prog {start|stop|restart|reload|status|help}"
) r1 w( q6 {" `# q+ ]: c- Nexit 1
6 k8 i) S3 }6 w4 Y) E9 Xesac
2 t0 O% s- b4 v2 r5 M( i" a; Mexit $RETVAL1 ~+ y" Q# L" A) P7 J9 O
+ Q1 X- s" ]' } J
注意:如果nginx的安装路径不是在/www/nginx下,则适当修改就好。
$ v" x7 |* N$ [. o# L1 qchmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:
3 e0 @4 U$ B v3 g1,是更改默认web根目录在/www/web的问题 2,是与php的整合,默认nginx是不认php得 对于1,nginx默认web根目录在 nginx安装路径下的html文件夹,我们把他改到/www/web目录下。 进到/www/nginx/conf目录下,vim nginx.conf,将 : w6 h$ u; z- f- ]
location / {
3 `) z* v4 a0 m, w' B* i5 z root html;3 Y, f! k I. d! V, ?
index index.php index.html index.htm;9 }& X9 v# S$ I' z) Z: y$ q+ t
}6 V1 }2 O& W0 U% Z
修改为:: z; d' o# W/ ]2 @9 q/ L+ h y7 O
( A) Z' \1 ~9 e& c$ v" a7 a8 g
location / { root /www/web; index index.html index.php; } 注意,增加了对index.php的识别。将
6 J8 x3 T9 G! d' c. zlocation ~ \.php$ {, g. J5 y" B- K! Z/ w' X& d/ J
root html;
. \( F2 ^! M+ n# Q* \ fastcgi_pass 127.0.0.1:9000;/ h2 q; Y* s6 u* U: V$ v
fastcgi_index index.php;
/ C4 b: u/ J: T2 o/ c4 I fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;5 s W$ E( u( u. ^) o2 T
include fastcgi_params;
( K4 v* A. S) R }7 p# Z/ m6 D' p6 A# t- o( W
修改为:
) T4 o9 E) ^* J5 q5 c8 P6 {1 G; L6 T( u1 Z. [
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解压缩并进到源码目录,执行: " s. w; { I, t! {: N" N/ i
#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql 之后make make install安装。安装完毕后需要做以下几个事:1,检查/etc/下是否存在my.conf, 如果有的话通过mv命令改名为 my.cnf.backup ps:此步骤非常重要!!! 2,创建mysql用户和分组 #/usr/sbin/groupadd mysql
% ]5 Q' K# K! Q* Q6 }
6 P, Q- P8 C3 k0 ?$ }; X k! A#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。
+ j# ~, K. u5 e+ o4 \8 ^& U 执行 cat /etc/passwd 查看用户列表
, K$ ^! I. u- \7 C7 Ecat /etc/group 查看用户组列表 chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。+ k* D5 k0 C2 f) N
3,进到/www/mysql,创建系统自带的数据库。 scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
: j$ _$ l) z; [! m 4,添加服务,启动MySQL cp support-files/mysql.server /etc/init.d/mysql$ z. b- L- |" [7 ]6 _/ F
chkconfig mysql on
- P6 r7 f5 H3 b* Xservice mysql start --启动MySQL
# i4 o, H5 K( o% ^, @, ~6 \) F, N 5,设置root密码 为了让任何地方都能用mysql/bin下的命令,vim /etc/prifile 添加: PATH=/www/mysql/bin: $PATH2 ?. b2 Z4 s( l. _# M
export PATH
9 ?& R/ k5 W+ h% w& `2 k( { 保存后source /etc/profile 执行:
! b0 p) p# L. C2 jmysql -uroot mysql> SET PASSWORD = PASSWORD('root');
0 m* w2 v% F6 W7 O. x% T$ [# _
; s. f- Y' {) O) M设置root用户的密码为root。 6,为了支持远程访问数据库,执行;
" ]$ M8 X+ s9 J6 U- H% ]4 umysql> grant all on *.* to xroot@"%" identified by "xroot”; mysql> flush privileges; //更新权限 这样就创建了一个用户名为xroot,密码为xroot的用户,可以远程访问数据库。 四,安装php(php-fpm)解压并进入源码:
5 b0 ^0 Q$ T- ?# [+ I#./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里: 9 o3 B4 x$ b; C0 n2 b
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
, V2 {" l/ x% G& C% V9 T7 Y2 G
# @' ?! S+ S2 o! v# X9 Rrpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
' f7 Y5 N. e" `Create /etc/yum.repos.d/php-fpm.repo file and add:
# @2 S0 g! U! @4 a- [nginx]
% [7 Y9 `8 Q2 K" S$ { - name=nginx repo
8 R0 R* N- m* J9 n - baseurl=http://nginx.org/packages/centos/$releasever/$basearch/$ M' W3 h5 E% b3 G, r
- gpgcheck=0
2 R$ p. [% J, \4 ^ - enabled=1
複製代碼 : @4 R+ z: d8 _. x [
yum --enablerepo=remi,remi-test install php-fpm
/ o# @% [& T$ S: ^! H' t8 x( {1 Z5 @" c( j ?
. c7 ?* [/ i# r% H }五,安装svn配置post-commit此步作用是代替ftp,方便开发人员开发并同步代码。可以直接通过yum安装即可。 # rpm -qa subversion //检查是否自带了低版本的svn5 [+ i/ C3 y! r4 v+ Z
#yum remove subversion //卸载低版本的svn n7 c1 u9 `- _) i0 D- m; n6 N
# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql //安装svn
d' e7 F, d% G P 通过# 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]9 ]" J: \+ K' ^4 i
# harry = harryssecret
# [/ X( A, a/ |0 \' `! h# sally = sallyssecret
, o. H) q7 ]2 y5 l2 s8 Jwangning=wangning+ k5 J4 G" y, O0 M* t; x0 E
yanzi=yanzi2 O5 j% ?5 Q! `) Z0 e: V; L% c
#vim authz //设置权限 ; _- w( d* \+ G
[/] wangning = rw yanzi = rw # &joe = r # * = #vim svnserve.confanon-access = none: x/ F0 w8 l; o; w9 m& }
auth-access = write+ b# `9 l! d+ J2 A3 x9 a9 L! C
### The password-db option controls the location of the password
@; i6 | ~% V: Z### database file. Unless you specify a path starting with a /,
2 t6 ?+ Z0 b/ r1 B) u### the file's location is relative to the directory containing/ j! M0 e! z1 V! b% }
### this configuration file.
6 D: n8 W" o! C) N### If SASL is enabled (see below), this file will NOT be used.9 D! z7 |) D9 E- u& F/ p% W0 M
### Uncomment the line below to use the default password file.$ _* w( b$ y6 U8 T3 w1 L
password-db = passwd: E9 P* G6 U4 E; O
### The authz-db option controls the location of the authorization) P3 `' e: r% }7 y
### rules for path-based access control. Unless you specify a path
" q4 s9 L5 m% A) z* Z8 U* Q3 F8 r### starting with a /, the file's location is relative to the the+ V7 A8 `2 b; p+ t
### directory containing this file. If you don't specify an) S3 h& w$ H7 J( o* O
### authz-db, no path-based access control is done.
. i3 f; M$ P4 |" Y8 m### Uncomment the line below to use the default authorization file.
* g! @8 r7 h1 o0 D. t# ?' F) D1 L* }authz-db = authz* x. ~* W9 \! R/ Q6 M+ l. l
### This option specifies the authentication realm of the repository.
+ i8 E* e+ L2 P/ X! d% K- r### If two repositories have the same authentication realm, they should
. u% p* {# |# ?3 b6 n### have the same password database, and vice versa. The default realm! \! C' V; L+ q$ ^
### is repository's uuid.
# e* K# x7 o orealm = My First Repository( t8 u( ]9 p% M: e3 g8 `
注意:上面这些有效行前面不能有空格。 3,启动及停止svn #svnserve -d -r /www/wwwwvn //启动svn2 r1 s4 y6 }0 e, h* s6 ^
#killall svnserve //停止" Y* l4 M. K8 L$ X
待启动svn后,可以在外面测试了。 svn checkout svn://192.1.15.222 --username xxx/ @: u$ R& l/ }* {
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/web5 }6 a# E! E" `6 K Z
b, # cd /www/wwwsvn/hooks/,然后cp post-commit.tmpl post-commit vim post-commit,在里面输入:
' O* u$ ?; F ~$ n+ z- f; ]2 Texport 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" :: e0 K; j5 o5 h5 f. e' f. a
#system-config-securitylevel 選擇DISABLE後按OK,必須重開機
O3 ]! F7 C& R4 j* _! @執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能
vi /etc/sysconfig/selinux SELINUX=enforcing 改成--> SELINUX=disabled
/ u2 b0 Z$ H' P; uvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
5 y8 h c' U" K6 i7 ` |