52AV手機A片王|52AV.ONE

標題: CentOS原始碼安裝搭建LNMP(包括nginx,mysql,php,svn) [打印本頁]

作者: IT_man    時間: 2015-10-31 15:13
標題: CentOS原始碼安裝搭建LNMP(包括nginx,mysql,php,svn)
參考自 : http://blog.csdn.net/yanzi1225627/article/details/49123659
/ I% U/ ]$ Y' D9 Y- O: F; @$ L# S) x9 s# p( p, A1 ]0 [

目标:搭建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 cmake5 [0 p! x( C! F7 I) n) w9 I

直接将所有待安装的依赖安装完。

然后下载nginx ,mysql, php的源代码:

可用 google search engine 搜尋最新版本

将这三份tar.gz文件通过scp命令弄到服务器上/www/software目录下。

二,安装nginx

解压缩文件,然后进到nginx-1.8.0里,输入命令:

./configure --user=www --group=www --prefix=/www/nginx
# r  J( V6 [+ i

然后make,make install就安装完毕了。

安装完后第一件事,创建www的用户和分组,否则会遇到http://blog.itblood.com/nginx-emerg-getpwnam-www-failed.html 的错误。

执行:

/usr/sbin/groupadd -f www+ _! ?) e4 _7 v  u* B* x+ J5 Y! P
/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

nginx命令在/www/nginx/sbin/下,拷贝到/etc/init.d/一份,接下来设置开机启动。

chmod 755 /etc/init.d/nginx
( T1 R& N5 I. ^" _2 q! S4 I" d0 X$ B% X! Q6 j
chkconfig --add nginx
* n8 a4 D4 X) ?7 \6 e
5 @$ V# J) u/ q- S' t4 G5 ochkconfig nginx on
& E- `: }$ j: m

然后

cd /etc/rc.d/init.d/ 目录下新建nginx,内容如下:

#!/bin/bash
# b2 s  ]2 R% @0 E8 ?# nginx Startup script for the Nginx HTTP Server4 i/ M" Z) f+ t9 M' {7 b/ V
# it is v.0.0.2 version.) D& L1 d+ g* L  h' Q* t7 }
# chkconfig: - 85 15' B+ T4 G$ E6 Q6 N; h& @$ ^3 s
# description: Nginx is a high-performance web and proxy server.
% y) E9 ?; B9 X# It has a lot of features, but it's not for everyone.
2 ~' P5 O! H2 T) V" e  i# processname: nginx
. ~8 N- f) |6 u5 f) _) ~, i3 ^1 t# pidfile: /var/run/nginx.pid* _! v7 M7 I: I1 t. T5 s
# config: /usr/local/nginx/conf/nginx.conf$ u5 v/ ?8 y6 ^
nginxd=/www/nginx/sbin/nginx2 L. H) \/ s* u' ~
nginx_config=/www/nginx/conf/nginx.conf
# r' o+ h3 C4 z/ {3 `nginx_pid=/www/nginx/logs/nginx.pid, P3 L: ?9 i' _  H# G7 E- f
RETVAL=00 g2 d1 E+ B- C0 e' j/ ~
prog="nginx"
, h7 C! X, Y) O( R4 @$ y# Source function library.
* K+ e( Q- S. G* R% \. /etc/rc.d/init.d/functions) e7 E" U9 W6 T: d) X
# Source networking configuration.  t0 o; x! b1 Z7 a) X
. /etc/sysconfig/network
6 x" @6 @7 y1 l# Check that networking is up.
& o. t8 `% ?  w[ ${NETWORKING} = "no" ] && exit 0
" z& o7 z8 T. o1 M[ -x $nginxd ] || exit 0
. f1 A; U  @5 ^1 ?7 C; i6 v# Start nginx daemons functions.! o) m) n, F8 v
start() {
5 E" _' l9 ]( a/ O& m  G6 n. {if [ -e $nginx_pid ];then3 Z* P$ t2 _' Z  n+ i9 ^' y& _. r
echo "nginx already running...."% P2 M* o$ |+ @7 Z' e
exit 1
2 P6 G  I2 M% g5 T1 j, Lfi
$ c$ b6 A: t& ~- w& A! recho -n $"Starting $prog: "
3 i) N" a$ `% C* O) S8 T$ ?( Sdaemon $nginxd -c ${nginx_config}
/ I$ d7 g- `- c- b& S9 g* qRETVAL=$?2 j4 B0 v4 a0 H
echo
. N2 n1 }5 F( p& E% t[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx* w9 {# K" g0 n
return $RETVAL6 t1 s9 X/ N$ K% P* e% ]' w: U
}
8 |7 r# G& |2 ]$ o# Stop nginx daemons functions.0 R7 O) u, V) d; O" p& n
stop() {
/ ?: ~+ r* [* U, u8 ]' l; aecho -n $"Stopping $prog: ". t- {; @' l& F" _
killproc $nginxd
* s: E  U* D. C; {8 R# [+ }& }RETVAL=$?7 W- E8 \0 h+ b' m. u1 c) k
echo, j( P6 m. M* ?; H, V8 p. e9 B8 i* K
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid  F/ b1 b* e( e5 b/ S
}
8 R4 _% _- N" x' rreload() {
* x; c6 v6 Y2 W3 z6 W! Uecho -n $"Reloading $prog: "" o$ X) C( f0 a: }7 Y7 q
#kill -HUP `cat ${nginx_pid}`
5 u5 ?; a8 x- b0 A& N' n* T4 Okillproc $nginxd -HUP
" i. x& \* ~; k! K% FRETVAL=$?
+ ?# L6 z# D# ?1 k( C7 [! necho. X2 Y* Y+ `4 w6 H( \; E
}
; N& X% u) n# R: R4 Q/ p. Z# See how we were called.: O0 y: Q; n# Y' D: J
case "$1" in
) V; h) y( g4 i, Z" vstart)
& i- H* q/ e! ?% {start
: t' m  `9 g3 a) r. _7 h) c;;
1 n% Q( a) a, [! N  `: v- z5 L+ lstop). L2 m& J4 F) G  B8 S' C) ~% X$ U3 J7 B
stop- E- R. j% h, D$ F$ d& S
;;4 Z& r- O5 ~' R8 P2 U
reload)7 z* f) p9 C# \$ m# o2 G8 {( W, Z
reload
' w: ^, D) A% W;;2 _& Y1 l! y# M0 n" l+ |
restart)
  R6 ], w  z  f7 {: R* pstop% z& m" I+ j  f! i* S" T: g# I
start$ l& _1 j- Z, N$ q- [
;;
# `8 e/ w& @3 L: l/ @7 Kstatus)
+ b8 r3 |& S. X% {2 I- S! pstatus $prog
7 `2 P2 C- D: y# e" P5 I' ORETVAL=$?
% P7 u. e% P8 P5 {+ G( E* _, f;;% w$ \3 P2 r4 P( G
*)! r0 d; d% K3 E# b
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
" g2 q$ e& `% c, ^6 }- X' Aexit 16 `, k% q1 r- C
esac8 b+ D+ i) ?# k3 G, E$ q# K  Z, X
exit $RETVAL
5 _4 J) K9 U! s3 q. v) e* k2 {9 v, V. R" Z$ w3 N1 E: t

注意:如果nginx的安装路径不是在/www/nginx下,则适当修改就好。


) \% X1 a, t" h6 J4 Achmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:0 K: C6 D! j5 {+ R

1,是更改默认web根目录在/www/web的问题

2,是与php的整合,默认nginx是不认php得

对于1,nginx默认web根目录在 nginx安装路径下的html文件夹,我们把他改到/www/web目录下。

进到/www/nginx/conf目录下,vim nginx.conf,将


8 @  V* W) ?; w# E       location / {! k' ~3 D. n( N; \  _
            root   html;
* W1 c/ }5 n# i1 x            index  index.php index.html index.htm;" d* L+ ~0 J! B. J# S
        }  D6 Q4 u; Y7 x' v, ]* w
修改为:
$ L! n6 P* h+ F" o1 f* }
% z: _  G9 @- w- R' P

        location / {

            root   /www/web;

            index  index.html index.php;

        }

注意,增加了对index.php的识别。

2 b- _6 k7 Z0 y1 q& q  D) R
location ~ \.php$ {! S4 i+ a& V8 `3 G
            root           html;
( T$ t0 r: V: o- @$ ?4 x/ k9 y7 ?            fastcgi_pass   127.0.0.1:9000;
  c4 X7 Z. N% n" k            fastcgi_index  index.php;% g" M+ |& d# b* u/ G
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;! C4 p# C: {* [) U8 z
            include        fastcgi_params;3 z* P4 s. e) L& Q6 p6 L* ~
        }
1 |0 I( e8 _$ e0 o" o* J修改为:
% W# T- n/ ^& Z' s9 b+ {% M2 R1 |3 U: M8 @& n/ ]- R

        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

解压缩并进到源码目录,执行:

5 O) E( i' c: Y% l( Y' \) J

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

之后make make install安装。安装完毕后需要做以下几个事:

1,检查/etc/下是否存在my.conf, 如果有的话通过mv命令改名为

my.cnf.backup

ps:此步骤非常重要!!!

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql2 J% T2 _1 S% B! e) X' Q8 m0 L

6 h( G% y  ]$ R$ ^: g) Q4 E) C#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。
3 [$ \* X! v# {

执行

cat /etc/passwd 查看用户列表
( ]1 B4 z( S+ Ucat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。, ]4 `! k- M: f

3,进到/www/mysql,创建系统自带的数据库。

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql2 j7 s2 a0 W% v) a. w9 e

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql
* C' `9 r- X  [% _7 W: Q" L& e, dchkconfig mysql on2 ]# M4 k. k+ j6 h" Y1 V
service mysql start  --启动MySQL- S1 L8 g- J3 ^8 d0 m6 M

5,设置root密码

为了让任何地方都能用mysql/bin下的命令,vim /etc/prifile

添加:

PATH=/www/mysql/bin: $PATH
8 }3 y* E& j- iexport PATH
' P3 [# R7 t4 X4 [

保存后source /etc/profile

执行:

, L5 Q) T3 x# F4 u1 j4 \
mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');7 \) s2 \) i1 T
$ E+ Y. Q+ W. \- g" \! @

设置root用户的密码为root。

6,为了支持远程访问数据库,执行;

6 _+ j. C5 |9 b( s

mysql> grant all on *.* to xroot@"%" identified by "xroot”;

mysql> flush privileges; //更新权限

这样就创建了一个用户名为xroot,密码为xroot的用户,可以远程访问数据库。

四,安装php(php-fpm)

解压并进入源码:


2 J3 J' d0 S5 v& @% 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里:

( n7 r4 I! r5 x; e( 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.rpm0 |/ T: D  k  C. X6 s
, w8 c; L, U# E
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm  W5 o7 D7 K! K
Create /etc/yum.repos.d/php-fpm.repo file and add:
6 D6 `+ A* J% R; P2 q
  1. [nginx]$ F1 x0 D5 w; d! `9 w4 ^2 I
  2. name=nginx repo( b# [+ I) \$ G0 I3 w; w2 m7 V! y1 ]
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/, A* a. Z1 ?- u9 x
  4. gpgcheck=0% y- T7 _+ c8 M* w- j, I
  5. enabled=1
複製代碼
( S8 Q0 V( H( v9 X8 U
yum --enablerepo=remi,remi-test install php-fpm
, g* l4 D4 a' x" P8 T. ]* a- f( ]& j$ [& k: ]
* H1 N, `2 o1 l6 h6 S* J9 `, y
五,安装svn配置post-commit

此步作用是代替ftp,方便开发人员开发并同步代码。可以直接通过yum安装即可。

# rpm -qa subversion  //检查是否自带了低版本的svn
/ q0 }. f, [* F! B4 J1 L

#yum remove subversion //卸载低版本的svn; Y; l- _( J1 {! Q0 @2 M8 E

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn
5 V& M! y4 v# Q$ S7 ?

通过# 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]# Q. Z+ k- s* Q' y6 `4 N
# harry = harryssecret+ l5 ~% g  L4 C3 b5 _+ t
# sally = sallyssecret( q" Q# ^( u) K
wangning=wangning) J( b, }2 Z/ ?8 ^. y
yanzi=yanzi
* K+ p& Z1 s4 C- L& `, H

#vim authz  //设置权限


3 z: k. c, \8 p! `

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none6 h' F* ?1 i1 J  q
auth-access = write
* |! Q4 l/ r0 c  I1 v* t5 ^### The password-db option controls the location of the password( n( X" m/ {6 Q- ]) g7 ]
### database file.  Unless you specify a path starting with a /,1 _' n0 T) z& u- U: ?
### the file's location is relative to the directory containing, ?/ N! ^) f) z8 b1 u3 V2 H  ]: \
### this configuration file.2 D7 C3 {, O& A2 f0 s
### If SASL is enabled (see below), this file will NOT be used., {/ a7 i- m8 O4 _) |0 Q. `
### Uncomment the line below to use the default password file.
% r5 t! `: Q. J; `* A5 u* ~password-db = passwd
' K+ o( t7 D. E### The authz-db option controls the location of the authorization
; j; s  K7 R. v### rules for path-based access control.  Unless you specify a path, O( _5 l6 r. A: \) L; N. `
### starting with a /, the file's location is relative to the the) p6 d+ V9 z/ n" ]2 x
### directory containing this file.  If you don't specify an' p( ~5 W; Z0 _1 Q: P3 r  W
### authz-db, no path-based access control is done.; X& D# w/ L, D8 k
### Uncomment the line below to use the default authorization file.
, t/ m; Z  x8 {7 w( }( Lauthz-db = authz7 u/ C5 t2 J/ l; K. Y
### This option specifies the authentication realm of the repository.7 G4 O' X2 |4 g, ?/ U. }
### If two repositories have the same authentication realm, they should
$ }/ e# N7 i6 Z$ O( M3 @6 p### have the same password database, and vice versa.  The default realm
( x: D3 N4 r" |6 A5 O7 p& `### is repository's uuid.
% V, _* V) ~3 Prealm = My First Repository' T8 c2 a/ h* Y% [) p# N3 t

注意:上面这些有效行前面不能有空格。

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn
  E3 [7 z2 T( c4 \  u

#killall svnserve    //停止+ \! @# X2 Z& z; G/ [7 n

待启动svn后,可以在外面测试了。

svn checkout svn://192.1.15.222 --username xxx- E* H. P3 I, f) s  W, D! x! m# A  I

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
2 ^3 F+ G7 j" e

b, # cd /www/wwwsvn/hooks/,然后cp post-commit.tmpl post-commit  

vim post-commit,在里面输入:


/ l. W. m5 X/ ^7 e+ A

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" :
, D! m* c2 M% j5 ^

#system-config-securitylevel

選擇DISABLE後按OK,必須重開機

' l) B! ^- _/ I# v
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled

2 V1 k! K0 a! U3 _! f7 Z5 e
vi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
: C5 ~$ L7 Z& w+ g9 l( G




歡迎光臨 52AV手機A片王|52AV.ONE (https://www.52av.one/) Powered by Discuz! X3.2