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* O- w& M) Y% B" |# {0 z
! k8 B/ e) O( h/ ?  z0 \

目标:搭建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 B. [6 x8 ~, L% R' K: Z2 s

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx
* `( ~& L9 r5 U+ g

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

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

执行:

/usr/sbin/groupadd -f www5 |0 w1 z# u. D, C, ]
/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx
4 d4 c' j- @9 f/ U- f. z7 O) n# ^( s# W. s2 s0 D) w
chkconfig --add nginx# u3 h' W! U# \- S+ M
4 z! ^9 o% C3 N) w1 @; U
chkconfig nginx on# L% z6 j7 x1 |' H4 p! ^# t" y9 j. s! j

然后

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

#!/bin/bash
2 v) f2 T2 w: d) G* M# nginx Startup script for the Nginx HTTP Server; g6 o4 D$ w1 _6 T* V/ v
# it is v.0.0.2 version.
" v7 W- S- d& v: Q+ n* d# chkconfig: - 85 15) D5 w" y( M. _/ z
# description: Nginx is a high-performance web and proxy server.
: \7 W7 Y8 S- q4 Y: B. x# It has a lot of features, but it's not for everyone.
$ \# e0 h( E% W1 s& O# processname: nginx
' c) L, [+ ^" w4 ]2 j6 x! H- X% p# pidfile: /var/run/nginx.pid
9 P9 g. o, h/ M& k# config: /usr/local/nginx/conf/nginx.conf
  d" B* a7 S6 ^( s+ Pnginxd=/www/nginx/sbin/nginx
: c9 k, K. u' q+ B9 c+ unginx_config=/www/nginx/conf/nginx.conf
/ N/ f: g$ s5 M7 mnginx_pid=/www/nginx/logs/nginx.pid
3 E: H- w, V4 L+ j  _$ R& mRETVAL=0
3 [( `3 _  g+ K/ Zprog="nginx"
! K" L8 {! Z/ ^0 m; l# Source function library.
0 J8 f# i# O1 a( U4 `% ^. /etc/rc.d/init.d/functions
( I9 M, a, m0 ^3 i+ w# Source networking configuration.
! M5 G" ]. V2 U9 y. /etc/sysconfig/network; \8 w, P; \5 e/ N+ H; ]
# Check that networking is up., ^9 Y& r$ I! x
[ ${NETWORKING} = "no" ] && exit 0
& R  |& |. X1 D. n8 W( }[ -x $nginxd ] || exit 0
/ f3 h% S" T" h8 D5 a& @# Start nginx daemons functions.! Y8 i  O* C1 d8 N: y. {1 B
start() {
! r% I/ r+ ]7 O& Q: @2 z6 D% E' hif [ -e $nginx_pid ];then
5 l. V& Q9 L* j' ^# q" `2 c! C! _echo "nginx already running...."6 Q( S1 k7 t/ I4 w/ ?& \4 N' ?0 C
exit 1: `# g7 t! d; Y2 z& f: a% |. Y$ @
fi4 p( a9 a$ C6 [9 l. H
echo -n $"Starting $prog: "
* Q# U  W) W2 _7 i3 H. U! Udaemon $nginxd -c ${nginx_config}
: }2 J' ]4 z. y) C! c4 `RETVAL=$?& X) p" d6 E/ o' c/ T0 }
echo' a, V, `, z: ?9 y; o- E' u
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
0 U% D' i3 L" `* l# N! W7 D6 l7 j4 g# _return $RETVAL
# z  ]; E; F- G# T3 j}
; G; N/ n3 x# @3 r" W1 g# Stop nginx daemons functions.
; h  `% z$ _4 Z  ~( k' {  Zstop() {' s( P1 G% T1 q3 M2 u
echo -n $"Stopping $prog: "* R! D+ y- a7 j; n4 M
killproc $nginxd9 Y" E4 S% I% Q
RETVAL=$?
6 Q! W" S; R: necho
) X* W: }% w* i[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
5 P! H( [; i8 Y3 M3 m! }0 j: Z+ x' m}
. y& |7 g  a& X: u! H* {$ Dreload() {
  W: l8 |' O9 B6 Z( P( ?, mecho -n $"Reloading $prog: "
5 ~8 {+ |$ \7 K" ~2 H3 W% D#kill -HUP `cat ${nginx_pid}`
+ P+ Q7 v$ X% c8 q1 u7 D6 p3 q! pkillproc $nginxd -HUP
* D$ ^. G! x1 r' i# eRETVAL=$?
, o1 ^, Q6 D8 A! gecho7 l1 ^: N( ^. S
}5 a, z/ y9 ~" N9 @% W
# See how we were called." f, `& ^* M& ~
case "$1" in
3 p6 _+ m0 @! C1 s* _. Z' |( Lstart)% z+ G- P# ?  z
start
( t; l& c4 B! ^" x' N* f+ T;;& R' ~4 G, H2 x; E* ]* |
stop)
4 j  B3 r, f' Ostop2 ]0 y( ^, w: N1 n
;;
1 F* j  J, @7 K( L5 freload)
. Z+ L. g) y; a# ureload0 v. R* i8 r4 @7 f; ^; J! J
;;( T+ T5 e( j0 Q# a# z
restart). ^4 @5 B2 n* G$ W9 d+ v8 |6 v
stop
! @5 K7 C& H' g) m' Sstart' h; B5 b/ E2 o
;;
$ i0 T7 G# l) q3 h$ c* ^3 istatus)
  t6 L& ]" J* }5 L% ystatus $prog
; b9 j0 n5 f! {* e4 FRETVAL=$?  ]  M: ]( r" @+ y
;;
, L3 b: A2 |- }3 ?6 d*)
$ }+ }4 J- {9 p! f& y" S  Y3 v$ uecho $"Usage: $prog {start|stop|restart|reload|status|help}"
2 q& o& @; s# t! w1 F9 k+ p$ [& B- pexit 1
+ N# Y0 [1 J" [6 D- ?$ Lesac
8 k: v; k; S4 T3 x, Dexit $RETVAL
; u/ g; h8 g+ u+ T% h& U
0 t/ ?. c/ H4 f: K7 G2 A

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


2 Z7 b! t$ Z1 t" J( m3 ]0 w3 S4 fchmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:
$ z5 \- _" h( V3 h6 @* [' d4 U

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

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

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

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

* ?% i/ x# A' V: G  }; ~0 F
       location / {0 ]3 a0 K$ P9 D/ M8 M
            root   html;! J9 f3 Z, `6 [1 a1 Y
            index  index.php index.html index.htm;
3 r6 S" h2 k! b: Q" U        }
9 M2 G  c: f7 c! r/ |5 X6 a. [+ c# U修改为:1 a0 L9 Q, q& S" r# |

( H( `- _; c, ^6 s$ V4 L% e1 C6 }

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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


0 r/ r- v3 y8 N. m3 x, r1 \) flocation ~ \.php$ {& q& L0 E6 l* v; w' R- g. X
            root           html;; K5 ^' t8 V- U3 a' j9 t$ T
            fastcgi_pass   127.0.0.1:9000;
8 J9 I/ E0 P. z- ?. R- n5 i            fastcgi_index  index.php;7 m9 ?% Z! ?/ ]# W% }% H
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
, h) e5 Q& {. p) a. h            include        fastcgi_params;
. b1 R( Q6 M: E# I- I/ |- a, v( H        }
0 J5 o' B/ ]9 y/ o修改为:
* n+ a$ }2 ^% p
& c( X7 d2 @( Y; B/ b) C

        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

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

+ [% P+ R8 J% w9 N3 q+ K- D

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
& D6 \* d( h# q1 R6 o& o; q3 @$ y) h3 F( F0 C# V& t
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。. N" j5 g" N4 d6 Z: R

执行

cat /etc/passwd 查看用户列表( n6 j& R% U: w) W2 o5 f
cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。( C" C4 m) Z* f6 x( {$ ]# a

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
2 q: ^3 |( V% N" E

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql0 J% B2 D. \0 @
chkconfig mysql on
( R% Q% x5 V" K$ d/ qservice mysql start  --启动MySQL
; G, ~4 p2 s: D; Y6 S1 P1 d

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
, y" n- c5 I2 `export PATH) S: n- ~! D- z& W6 D

保存后source /etc/profile

执行:


5 e& y3 k7 ^- T* O  _3 r8 y) _mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');
; b: `" {; g* E+ V
# [2 U; V% f" b' l" `$ v

设置root用户的密码为root。

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


( ^8 n. B' X" t6 m: ]8 l

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:


+ [( W* [9 o. Q3 G

#./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里:

- n4 a# u1 M, I7 b8 S

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
* y- m9 `+ a7 e+ E- X, g' @
" K' G+ t! A% O% prpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm& y1 M* `9 r9 l1 f% d# t
Create /etc/yum.repos.d/php-fpm.repo file and add:! e% |# u/ A3 a$ X5 Y0 A0 _
  1. [nginx]7 A4 C7 n# e2 @- s( |8 r0 o+ _
  2. name=nginx repo1 A, R$ e/ ]* T+ y" B
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/1 H# s( m% g  |/ C! l3 O8 z1 f: V, a5 j
  4. gpgcheck=09 ^% k% N: {, X+ I8 o6 |
  5. enabled=1
複製代碼
7 e" v, l& ]* v/ _7 t
yum --enablerepo=remi,remi-test install php-fpm
! i; e8 \% ~+ m& O3 ^  _7 ]4 ?
0 G! ~" }+ w* h( H7 H6 f) e
7 L0 n9 k. D- p! x
五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn" e$ h" S9 t/ f* V& v

#yum remove subversion //卸载低版本的svn: k5 K0 L0 R# c" r1 `

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn
. O9 {" p! r5 n* W( [# X

通过# 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]
% z, `: H0 d" P# `) n$ u# harry = harryssecret; U$ @3 s1 p: }& w+ r" n+ m
# sally = sallyssecret
# j6 i  G! D& ]8 Twangning=wangning
# ]2 S5 {6 X1 ~% ~yanzi=yanzi
7 X2 p" }$ M& N- u# i7 X" q1 c/ ]* p

#vim authz  //设置权限


& M0 ]) p2 c8 }6 @4 }  i# y

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none
4 N0 l. k/ M& [- H% P' s6 @# B: \auth-access = write
$ C$ h# l5 |8 H  r& b7 ~8 G### The password-db option controls the location of the password
) d+ u- [  v4 I3 M4 J7 ]( x### database file.  Unless you specify a path starting with a /,# p7 B6 S2 {, o; j" N
### the file's location is relative to the directory containing8 Y! K9 Z7 }: x/ r, {
### this configuration file.8 _" ~" f' M4 N+ W
### If SASL is enabled (see below), this file will NOT be used." u5 b0 T* R3 }! a# e) n6 Y5 x  L
### Uncomment the line below to use the default password file.
, m* {1 l0 f! [- P  Epassword-db = passwd. [' z" P8 ^8 |( O/ W, z
### The authz-db option controls the location of the authorization
* Q  ^- d  O) `) G5 z3 f: x### rules for path-based access control.  Unless you specify a path
5 j& I" F" o( _' u### starting with a /, the file's location is relative to the the
6 [8 D+ T+ i# U6 [8 \2 A### directory containing this file.  If you don't specify an! ^& }. X/ l; }; f' T& {. c
### authz-db, no path-based access control is done.
' `+ `4 p5 u8 Z* Y. z. w) Y### Uncomment the line below to use the default authorization file.$ v3 ^7 r  D! \' @' Y& ]$ k' C
authz-db = authz9 ?4 C3 K+ o' f+ S
### This option specifies the authentication realm of the repository.
7 ]. M' n2 o4 p' C* E5 w### If two repositories have the same authentication realm, they should
5 Q0 H. n; r( Z* V: S# y2 M### have the same password database, and vice versa.  The default realm
, q: B7 V/ l3 O$ `4 l5 R/ I5 I### is repository's uuid.8 A8 N6 U* g# L1 y
realm = My First Repository0 a" M9 R6 S2 ]

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn+ k2 ]0 D7 E' y$ A

#killall svnserve    //停止8 j6 H1 J( t- b# Z+ b

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

svn checkout svn://192.1.15.222 --username xxx: r% q  r+ Z5 t! F2 Y- q

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
6 F% T7 d- W& ^2 \- U& c

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

vim post-commit,在里面输入:

9 t# S& ~9 J  X/ I

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" :" b. J; `( k% M0 e8 M' X$ c

#system-config-securitylevel

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


  J: Q, P, `2 v; M執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled

0 Y9 A1 d6 i& L, }' m& \, a3 g3 R
vi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題* G, Z) B6 k1 D$ Z, G' I





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