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/491236592 g* w+ k( u& I$ N% p1 Q  ~! S) d
6 y# z+ U. s" _2 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
; _. ]/ g% n( T! x

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx
8 C  l) T. R4 C9 Q

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

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

执行:

/usr/sbin/groupadd -f www( c* B2 n, y# F3 `# W
/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx
& H5 f+ I4 C2 s7 \4 W- M! Z- \9 m
+ G4 [! b% i) E+ l2 F0 O/ x. ichkconfig --add nginx! h' L. {7 G9 @$ B: M# |

  }& U/ H8 G/ ^5 p% m1 \, B+ uchkconfig nginx on; E: v$ Z- M" O0 V

然后

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

#!/bin/bash1 ]4 z% O" I' |, `
# nginx Startup script for the Nginx HTTP Server
4 F# e0 {, q6 a- o# it is v.0.0.2 version.
9 ^2 u, U4 }8 y: a2 Z# chkconfig: - 85 150 q" _0 L! x# m7 W$ E4 ]7 \9 F
# description: Nginx is a high-performance web and proxy server.# Q8 K" s( z" |0 f/ B( s" S4 Y
# It has a lot of features, but it's not for everyone.
& Z9 L* A! v$ Q0 k! q+ v# processname: nginx
2 X7 Q$ I; j- ^/ a# q2 B8 Q# pidfile: /var/run/nginx.pid
- H. R0 f: X" R# b6 }' D9 \' v6 u# config: /usr/local/nginx/conf/nginx.conf: Q9 \& f4 Q1 _; f( B
nginxd=/www/nginx/sbin/nginx
. \; a& E" `& j+ H6 enginx_config=/www/nginx/conf/nginx.conf- s' ]7 ?* x! i5 b# ]9 N5 M( Z
nginx_pid=/www/nginx/logs/nginx.pid
' B5 b7 h8 Y& p6 s, GRETVAL=0
4 p8 F+ ?+ s7 I% Qprog="nginx"$ \% e2 @5 i. `% ^2 }  e" ~
# Source function library.  U; G; T# E& S2 U( u* K
. /etc/rc.d/init.d/functions
9 l" `# ~  Z; u" z) V! L; \# Source networking configuration.
6 k- E# m2 c) s' b. /etc/sysconfig/network# A$ r0 N/ {8 j% x9 v2 t  k( ^. P% ~
# Check that networking is up." F/ z: B* J" a; l" s) P3 M" m
[ ${NETWORKING} = "no" ] && exit 0
; j  B0 R; u% Q) X0 q# y[ -x $nginxd ] || exit 0
9 _6 o7 T- ], l4 Q, M: c3 V2 s# Start nginx daemons functions.
  d* T( m2 y) a+ rstart() {
0 ?  |& z* d$ e% h$ _# ^. vif [ -e $nginx_pid ];then
+ A' |! Z* Q, _4 n9 F; N+ C; Techo "nginx already running...."
( v! c1 S( o, b' Pexit 1
- G1 E* e1 `7 A: ]6 o# a5 E* Hfi; p6 x9 G  \0 z, x1 b' Z
echo -n $"Starting $prog: "3 R( Q: v  U# O; m" _
daemon $nginxd -c ${nginx_config}
6 O7 j) o& O; o+ A; {RETVAL=$?
, k0 L9 f$ `2 Y6 K# A0 {6 recho, u/ N4 j- w2 W* {8 ]( S: L
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx, }+ g( A2 ^! }' t- a9 H0 ~6 C
return $RETVAL. k5 s0 Q, C9 o9 E  S
}
6 i7 ^; L- A. W% m9 g0 E# Stop nginx daemons functions.
6 g- w* E! `! P5 i) }+ estop() {
$ f' w, g4 S% O- l( Becho -n $"Stopping $prog: "1 F0 T2 |0 \$ l9 {9 |% a9 ~! D
killproc $nginxd
1 k( R1 S; T9 O8 t' a" L8 GRETVAL=$?
3 S9 Z: K- k" Z/ j2 a: C4 d  Mecho
( n: N; r: n' I[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
* l# b4 o: G( ~}; [! R) i7 I% H4 L" T
reload() {
' [( v* M# W* eecho -n $"Reloading $prog: ". }: G" S: D; }" F& ~
#kill -HUP `cat ${nginx_pid}`
, }; l# U. A7 o& F& A8 t* ikillproc $nginxd -HUP
; D  r7 {. @9 ?+ F0 Q  W- G" qRETVAL=$?
6 L( i: H+ p/ u5 T" @- T; \# v! f' Zecho2 x# y4 U0 d7 y) _
}
6 b) Z1 o7 N. p! C( `# See how we were called.8 l) i3 @4 ]8 o6 l! |2 p( @
case "$1" in
' G) B' _$ N% `start)2 W" r' A: T. C7 r, r- ^
start1 e& Q' x: V6 D9 g0 g3 i
;;! N. W$ V6 s/ p% S9 O) o. K' Q
stop)! U3 [: ?+ C; r: F- B* N- ~# L
stop
' _2 g( p4 q. l0 d( ~6 Z;;
$ [+ W" W& ^! q; Q8 |& m7 Ereload)
) H. ?5 c# I( kreload8 X9 q9 y: u6 G) P* m
;;
) c. M( n  t5 q+ c- j( zrestart)2 Z$ w0 Y$ g( R% ^  W2 I5 S' i
stop- }  d3 q5 Y# H4 v0 b
start; [% _5 B9 f3 n8 T! u; K
;;. M* U% l4 d% O+ M7 M
status)
$ u6 f9 |7 D$ k- c/ estatus $prog; ~2 R2 j( g$ U( f
RETVAL=$?
% C' R. T) e3 c/ ?; s;;9 X  v; K6 ]  q
*)
& U) F; x- Y8 I( wecho $"Usage: $prog {start|stop|restart|reload|status|help}"
: h0 j* S$ @+ x& vexit 1
$ k; q% p' S0 i/ i& |esac; W# U8 q5 ~* q/ D0 _; Y- t0 z
exit $RETVAL
7 @7 B* p1 v" M  s0 ~* E" ~
; r4 K. f( V; x! ]  L- M  i3 a

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


& B) u7 z, E: A0 d# }+ }chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:
. v4 u4 O' l4 h' Q; A# t# Z

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

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

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

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


4 K, \7 I4 n5 G' M. ~; X. H       location / {
: [, U# v8 l. [5 `! b% N8 A6 l, i            root   html;
8 z  _0 ?: K; p! a            index  index.php index.html index.htm;% `; Y4 K4 z, s$ @. p: ]/ D
        }
+ {- Y% y0 f9 j' h! `4 K  [( {修改为:3 C0 I+ N. j" q& v9 c
  i$ I: s' S6 i

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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


/ Z8 p1 E6 V7 L  q0 z& ilocation ~ \.php$ {6 p9 G: R  y" n/ e
            root           html;- v$ \: t, H% O
            fastcgi_pass   127.0.0.1:9000;
' W7 G. g9 U+ w8 @+ A& z4 G            fastcgi_index  index.php;) ]; c* p" W% @( l
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;/ |+ S7 y* U9 E8 Q+ G
            include        fastcgi_params;  @9 y9 @" D2 X0 }
        }
% P% g( {( K' }1 u$ p/ s  G修改为:
0 |0 k5 t( c1 O; \/ R7 L' v  w: }3 M% b1 `

        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

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

% t* k( W+ b5 c* q- [* V' ?

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
# s5 y! |; ?" Z/ i% T7 r+ F8 X/ Y- b: J' v1 c
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。
9 p) v* p" E1 i! d' V

执行

cat /etc/passwd 查看用户列表; ]. C, b, e' }' n$ B
cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。
; L6 Z' |8 r4 j

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
. {: n- x6 o, I& ]8 _

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql( ?! T* w8 _+ b  x" c2 h: G
chkconfig mysql on
# @4 E* E' o/ U; C9 Q# Bservice mysql start  --启动MySQL, w9 {' a  i- d

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
2 o- {( p  ^' Q9 U& P( Mexport PATH
% y# P5 m* z6 ]" u! @7 f4 g, v0 _

保存后source /etc/profile

执行:

  d+ g, Q2 k; I/ ~
mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');& i; I$ x  N8 ~+ x( }7 v9 \! z/ j2 U

4 {& n: y( b6 v* o1 @

设置root用户的密码为root。

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


3 i( W! `! F) \0 z! k

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:

/ @6 K+ ~, w* L1 z2 N. f- z

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


' K" [1 |2 u6 \; ]" L8 }1 y* Z

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
+ U% A1 n9 P. F% F. P& x
( O4 ^# p9 P4 \rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm& p  x/ A4 t: S- U0 W
Create /etc/yum.repos.d/php-fpm.repo file and add:
4 Z' h$ f7 q8 O; R$ i$ g
  1. [nginx]0 j& z  E0 Y& y. a( Y4 p
  2. name=nginx repo- c0 Z: }' J2 J& ^6 H  _
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    " {0 }% l( v/ P
  4. gpgcheck=0
    2 e$ H0 w/ h! }
  5. enabled=1
複製代碼
/ B+ ~. g$ D, U' U7 t& I+ o$ j
yum --enablerepo=remi,remi-test install php-fpm
( \" k* c/ M+ j# i( Q0 K3 q- W4 Y% \6 y7 W

  L* R: J6 c2 k五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn( ~( b9 w7 d8 j; M1 u1 X; x  a1 b

#yum remove subversion //卸载低版本的svn: u1 w( e( a3 v

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn
7 j* Y# g+ k2 n% O% u" f' |5 u

通过# 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]
/ K6 l1 ~* r) i, u- v1 M# harry = harryssecret
* Q& `  T% i8 D9 o' p* }3 ~# sally = sallyssecret
4 ^- g9 t) C7 ~0 P% F) xwangning=wangning8 m: m% y2 Z! k7 m4 j2 b% A
yanzi=yanzi4 p. N1 l' S& W( d( T/ s* q

#vim authz  //设置权限


# q2 n8 n( o& \7 m

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none
+ _$ Q4 O+ k/ [/ i: T7 Z, xauth-access = write, a7 P" L( g" }
### The password-db option controls the location of the password
0 }- x6 b/ z) q' d: @$ s### database file.  Unless you specify a path starting with a /,/ O, F: a5 R4 z1 Y) R4 Q
### the file's location is relative to the directory containing  o' Z* j/ x$ X, x1 I, p: C" A3 q
### this configuration file.
4 h% |! V2 N% }2 @' `& N4 F### If SASL is enabled (see below), this file will NOT be used.
( w" ~  ]5 k" G. @### Uncomment the line below to use the default password file.( Z  F  D0 Z3 q4 `
password-db = passwd
' \/ E5 Q- @+ d# U### The authz-db option controls the location of the authorization5 q9 n5 K$ T& v; g6 V9 t
### rules for path-based access control.  Unless you specify a path* Q( M. S5 b! ?6 N
### starting with a /, the file's location is relative to the the
' n7 L4 m1 U: N6 `$ Y4 ~& L( Z### directory containing this file.  If you don't specify an
$ ^6 }0 n! R& H! \### authz-db, no path-based access control is done.
$ b/ X  I; i* v; T3 w### Uncomment the line below to use the default authorization file.2 S; S8 D7 J) z: q+ M3 y
authz-db = authz6 K4 j" N3 |6 X: h
### This option specifies the authentication realm of the repository.' u+ j5 Q4 l. e- w5 P  X* w1 w
### If two repositories have the same authentication realm, they should/ `5 O# v7 S& c
### have the same password database, and vice versa.  The default realm
$ F& Z6 U5 A; ]. P+ k### is repository's uuid.
% s. @" o2 c+ z3 O9 {* m3 srealm = My First Repository
( L5 b4 o% u' M; [, c) j+ m

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn
4 b( Z' o3 I9 U: _: a' F0 j/ P

#killall svnserve    //停止
* r( m3 b& \" H/ F

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

svn checkout svn://192.1.15.222 --username xxx
7 M, ^" y' v3 Z# F9 [, t

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
9 }0 Q  O# D, W& H- E! i7 V

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

vim post-commit,在里面输入:


4 \: O' p$ Q4 G( n

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" :
8 L6 j& I' a9 f3 G9 _6 b

#system-config-securitylevel

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


+ H8 F( |) \& T' m執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled

. }1 |+ V6 e  |
vi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
# V5 f6 W0 L9 k" e7 `




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