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
% U  T4 i6 ^0 r6 \( W
! W: f- D- X: S- w6 J! c! G

目标:搭建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
: h% c. r# ?* A: y

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx( K/ K# J8 i* L! N

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

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

执行:

/usr/sbin/groupadd -f www
) b" }5 D3 M  K! W3 k+ ^- d: R5 j+ `/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx
& q; _+ y5 O2 Z3 I. c* M! J
/ @) c9 Z# T; ?- R8 ]chkconfig --add nginx
2 D) ]6 M* I2 x# {6 D( n! z, g+ U' Y5 j
chkconfig nginx on! x$ C/ T2 I' u- w7 i3 Q7 R

然后

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

#!/bin/bash% e/ K& X/ v$ E1 Y0 d5 l
# nginx Startup script for the Nginx HTTP Server( T: q$ u4 q, T2 _9 L' M9 u
# it is v.0.0.2 version.
0 O1 N: }9 q; n/ u: q7 i. ^- f+ B# chkconfig: - 85 154 Y* ]" O- G0 ~7 O4 W
# description: Nginx is a high-performance web and proxy server.
6 o( r2 u/ y; c# It has a lot of features, but it's not for everyone.7 \8 h0 Y; |7 `! l
# processname: nginx. P/ e- E/ b  S! W; @/ z" Q0 E9 N
# pidfile: /var/run/nginx.pid
1 q7 O" I/ Y# m" f: h3 \" H8 H# config: /usr/local/nginx/conf/nginx.conf  L& i6 [4 m7 ^: o
nginxd=/www/nginx/sbin/nginx1 `$ i0 |- R) `: M" O$ m+ D$ g
nginx_config=/www/nginx/conf/nginx.conf
3 Y7 t  R0 e9 T3 U8 t" Qnginx_pid=/www/nginx/logs/nginx.pid  f9 {( L" Y' [% q6 e
RETVAL=0
: e" |, J8 K; c: Fprog="nginx"# Z  w* _) q- I& {# [+ ^# f
# Source function library.
- {7 `8 n1 M6 t. /etc/rc.d/init.d/functions6 S& ?. p! M/ G" Z
# Source networking configuration.5 b0 n6 N  p; ^6 d9 Q. O# M1 x% s
. /etc/sysconfig/network
3 w# l  F) F( n8 O# Check that networking is up.# t! V3 m" j- P7 |; G
[ ${NETWORKING} = "no" ] && exit 02 a% k4 _, G& d0 q* }* Q8 O" g: m
[ -x $nginxd ] || exit 01 U( {3 K4 C) w6 H0 T
# Start nginx daemons functions.
; `1 `/ |6 K/ k: dstart() {6 H# T% R2 P) X* q
if [ -e $nginx_pid ];then
4 e1 @. \. K: _" s6 jecho "nginx already running...."2 s5 P0 \; M$ `( Z/ G
exit 1: W1 x( a) u4 t/ `% n4 `3 j4 g
fi
) C' N2 z  H6 c  |& ~; Secho -n $"Starting $prog: "
1 n* h* [' c2 {1 h5 m% Ydaemon $nginxd -c ${nginx_config}
# i! [8 \7 W, \6 wRETVAL=$?
! A* P8 P; {6 s# recho
+ W" ]" f! h5 X[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
' O; U8 K" {5 ]# _) U. Greturn $RETVAL+ _; m& V$ I2 H% K! {
}
3 ^: e6 u4 O7 H# Stop nginx daemons functions.
4 q- d) m0 G6 qstop() {
$ b1 U. o0 B# m# |0 g2 Y* mecho -n $"Stopping $prog: "
, ?) n5 R6 Z: s, e3 E6 D, L7 p- X7 bkillproc $nginxd
. T* _4 L! b3 ?/ v1 A7 N; p, ERETVAL=$?
- p- C9 H: m; Lecho
# l0 d& K+ ]( w0 ~' m  N( `[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid2 m1 k' b6 A" A+ d- S- |
}
2 R/ \( d* W6 j& ~- mreload() {
2 V3 y7 b0 o% I: z% Secho -n $"Reloading $prog: "
9 h5 y7 C& V% ?) ~# t2 Y; l  Q#kill -HUP `cat ${nginx_pid}`
5 R1 _2 v* N! q+ x4 mkillproc $nginxd -HUP  ?8 U5 b; \8 v; A) T
RETVAL=$?$ r( N5 T( F0 {" i: I8 h8 J. w
echo3 T$ q% a4 ]- L0 G
}
6 C7 v. T: \1 r. `" u( E4 j+ O# See how we were called.
4 @  @7 |  Y' L7 w& e5 y2 a4 zcase "$1" in5 }  b) ^5 r4 r- L8 U' s
start)9 v( @& N" w9 g% `& g( \
start! A" e+ M( ^0 o; g
;;) h6 R$ z  k, E- p9 l$ m
stop)
1 u: v; |; t2 ]+ C+ V6 A, Ystop
2 D5 ]0 d4 w, h4 o( z3 k;;) m" `1 `& A& v& b1 H, W5 L
reload)
; u; R$ I' u, D7 sreload
) ~$ M- B1 S" P: u/ i! E;;
' u! [# j9 x7 g' R  ]restart)
% u3 P; K9 `# [0 Fstop
) V7 j' n6 C7 j  B5 M; \0 `start0 q) Q0 \4 `5 R- K, m+ P- a8 d& j/ I
;;
% q- h  \+ _1 X2 O" pstatus)
! H1 k* ]" B1 T0 x! r7 c0 N! o6 ?status $prog/ ?% K( j! k" B% N
RETVAL=$?; P& D! N2 p' x: {
;;: |8 j0 d1 W7 _: `
*)
9 `, l% w1 R/ b$ _+ W! a- ~echo $"Usage: $prog {start|stop|restart|reload|status|help}"
+ n" _3 h* W) S" Texit 1
  }! ^3 b8 B& p! Q  d/ Z: Tesac" ^8 l3 c/ P' [5 R
exit $RETVAL
( J  D# w6 C) K* S. L- Z+ j
# S! R( z) l+ F& o  |

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

3 |0 \! L: H6 J; J
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:: c4 p3 n% t! [; S" a

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

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

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

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

% W3 h& T4 e0 g/ K1 V
       location / {( r& ?  t7 u8 s% S$ O. P- {
            root   html;4 y; a) Z- w, B0 N
            index  index.php index.html index.htm;
. w  C6 N; u/ p- e4 L  f0 @' A# J        }% h1 S1 y' w9 f+ T0 A
修改为:; V- x3 H! s* W% T# x  B
4 J; T7 W2 L6 |1 f8 }- E) ~; ]

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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

4 S; t; q/ Z8 ?, c9 [5 a
location ~ \.php$ {! T6 w2 |  i/ S1 P0 |( w: s1 Q
            root           html;
- @% l( _1 q- `! |( W$ z            fastcgi_pass   127.0.0.1:9000;
6 t0 o& G2 s- _9 l" r9 i' P. p( L            fastcgi_index  index.php;# U0 L1 M0 ]) q! M  T
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;" s. c- B4 p- U8 m
            include        fastcgi_params;6 V" A; ~$ P9 ~9 x
        }% K. \: z9 s( M; d" A
修改为:; _* \0 Q# Y5 R) A& F$ \$ L& \

4 N9 G" W* ^8 l9 R' o" u6 I

        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

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


* y, N0 a( C2 _  I9 L" E

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
" V6 n( _; [$ V! @7 |5 o* y' s0 b* s+ \0 R/ ?- }: Y6 G/ P. f' @/ u
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。
3 f7 E/ A1 M" D- V2 b/ q8 V

执行

cat /etc/passwd 查看用户列表
7 ?- @) {' F$ \  N; r: ^cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。2 y9 N' o6 c  p& r! f, _

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql0 n7 N. N: ]5 ?& D4 u

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql
$ v9 T5 E8 f3 Y, wchkconfig mysql on
. r( I8 x" J; \! l. D6 P" Zservice mysql start  --启动MySQL5 G% ~$ O  ]% a

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH2 ^4 O( Y& C, {; S" e
export PATH+ K+ P% H' _; [) w$ s" E

保存后source /etc/profile

执行:


  x7 k$ g9 P3 Q" D3 ~mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');  B$ S, p* \3 Z" E( C
6 b+ a' N# }( v+ Y2 \

设置root用户的密码为root。

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


1 b  D& K, b- ?( i" E1 N0 }" v

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:


& F+ N& P$ {$ ^) u5 c1 `

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


1 q. h: q, {8 w* ~

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
9 Z( p" J6 |1 {2 [
7 x$ T, T7 X- ]( c' @rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
7 K2 j0 ~4 X( O( D# X: I, u* CCreate /etc/yum.repos.d/php-fpm.repo file and add:! t3 Y8 _" k9 {% D
  1. [nginx]
    , U/ Q( O7 A" \6 Y6 _0 n
  2. name=nginx repo  O( R; D$ ]$ p, |
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    ! i6 J. m. H" D+ N" v
  4. gpgcheck=0
    7 \8 _! l2 B- g: d0 u
  5. enabled=1
複製代碼

7 Q* v8 r- w2 `' }8 zyum --enablerepo=remi,remi-test install php-fpm; b: g' g3 m1 ?+ C: I4 G

) X/ O4 h( x( `5 D5 S5 `! I

! ~5 d' b3 ?( w  b五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn& O0 |) u- L9 }: F8 q

#yum remove subversion //卸载低版本的svn" x3 y2 [+ v, R9 J( Y4 c! S

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn, k$ J$ C! w2 r: p9 O( K

通过# 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]  c4 E0 o' w- n) m1 f4 l
# harry = harryssecret
: K0 p0 d& N) t7 H8 {# sally = sallyssecret
& A, R9 H* e' w8 kwangning=wangning' `7 s! Q$ D8 y8 l
yanzi=yanzi
. p' h8 L% Q- h9 O7 W: \& ^# h( b

#vim authz  //设置权限


) A1 J: T3 B9 f

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none& Q, v7 W6 R) q1 b
auth-access = write8 q! d2 [- R% E7 w2 y. y+ e
### The password-db option controls the location of the password, \* f, b. c9 r: j  @& [! C8 z3 i
### database file.  Unless you specify a path starting with a /,
7 z$ N, I* L1 @* |0 g3 H### the file's location is relative to the directory containing- u9 ?( R- t2 o
### this configuration file.
6 N- Q# D2 p# f5 _% v" A6 n### If SASL is enabled (see below), this file will NOT be used.) o' R* j1 D' U  z; P
### Uncomment the line below to use the default password file.
# }3 h' |; O" m' S. \  b4 `password-db = passwd. Z  C  P. F6 }* u$ `4 @
### The authz-db option controls the location of the authorization& ~& B6 n, Y4 v5 y% F- Q
### rules for path-based access control.  Unless you specify a path
* e# I3 A) k9 j### starting with a /, the file's location is relative to the the6 M+ V, C" h& Z% |
### directory containing this file.  If you don't specify an. d- c6 A+ s( Z" @# P
### authz-db, no path-based access control is done.+ y- b9 M  F1 M! W# N% A
### Uncomment the line below to use the default authorization file.2 z  x% V7 t: O/ ?) b
authz-db = authz
0 g6 D' E+ n! S5 m/ P### This option specifies the authentication realm of the repository.
! |5 z  F) X( ]& S% d2 S( ?### If two repositories have the same authentication realm, they should
7 p$ b1 I, P3 V' [$ J" R* ?### have the same password database, and vice versa.  The default realm2 p2 r* ?! Y! X  v" L4 x
### is repository's uuid.) E- ?% d0 j& m& ]% H/ w) F
realm = My First Repository/ s3 Z" e, r7 N4 d) E9 T4 D

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn& a8 Q4 O  \- z) _" k

#killall svnserve    //停止
0 O& R$ y! g# L$ l/ U8 s6 e8 s1 `

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

svn checkout svn://192.1.15.222 --username xxx" u5 B+ M) f3 R7 J4 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/web
& ]3 q+ n5 |! _# B' }

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

vim post-commit,在里面输入:


/ s: {# ]4 s1 o

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" :' [! _* j0 ]9 p: F1 D/ g3 G7 [

#system-config-securitylevel

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


0 h' H4 A' p4 j7 w2 m執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled


2 l& n, E  |: F$ G, P4 mvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
- R$ F5 g" c# F  J" S




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