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
3 F* P( \4 i0 V7 u' c/ o/ A
9 u3 q, L# X6 f

目标:搭建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 cmake0 `' M7 ~# A( E( d

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx
  d7 F; c( V) o+ [. F. I3 `! n; S

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

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

执行:

/usr/sbin/groupadd -f www
3 q/ r( v% @  @3 e  [/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx- m/ I/ l) Z, }: ~* Z
8 l4 U- ~4 e3 a+ a
chkconfig --add nginx
& j! {" G$ Q  [' D
8 x6 M% P9 v( Gchkconfig nginx on
' O8 f! o: x7 M# W3 t

然后

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

#!/bin/bash
% H3 Z! m7 ?: ?8 e( s# nginx Startup script for the Nginx HTTP Server
8 \- r/ j9 s. y! n5 O# it is v.0.0.2 version.! w. [( I( i: y3 \
# chkconfig: - 85 15
# r& _6 a. P! w  d# description: Nginx is a high-performance web and proxy server.; s  n6 j$ j; n; X9 J
# It has a lot of features, but it's not for everyone.3 ^5 O+ r! E8 n4 M- j) ]
# processname: nginx
3 d+ c; t1 v1 C1 w! W& x) M# pidfile: /var/run/nginx.pid; N/ k8 P1 t3 v2 t+ @+ s
# config: /usr/local/nginx/conf/nginx.conf
9 {; Z) A& S: p! h# ^0 R7 p5 anginxd=/www/nginx/sbin/nginx. n. I1 @6 ?- J8 `
nginx_config=/www/nginx/conf/nginx.conf
+ H% ~, @6 \- tnginx_pid=/www/nginx/logs/nginx.pid
2 U, ~6 k% S6 ]. a  |' [- @RETVAL=0
. t1 Z( z; H" v" M1 O5 r* ?( K* Pprog="nginx"
7 k8 Z" A1 c, O2 i" F* {# Source function library.
1 c% I: N) `4 }' h- J2 S. /etc/rc.d/init.d/functions
+ s! o, G1 v: W2 g& v  d4 {7 w# Source networking configuration.
& j) y# i- @2 g6 I3 G9 ]( O; N. /etc/sysconfig/network
9 q: s' J; ~, j0 g4 s% {1 o# Check that networking is up.( X2 ^7 D' |0 o' |
[ ${NETWORKING} = "no" ] && exit 0
1 T* G9 {& K: \5 y( |) [* H% f[ -x $nginxd ] || exit 0$ a8 H# H; [5 \- u7 [' l/ k7 j) W" p
# Start nginx daemons functions.3 k: O: }  K) Q
start() {
" v% B) {8 @% lif [ -e $nginx_pid ];then/ m$ j( @  c4 v: A4 o: T7 I* `
echo "nginx already running...."4 G3 [1 O" t9 G" f5 M8 i; Q
exit 1
" v, Q# ~$ x& `3 O' M2 f6 a+ e7 ~fi# y5 A( t, T9 ]  n  g2 g
echo -n $"Starting $prog: ", q5 _: P+ p# |9 d) u: u! c3 b7 B
daemon $nginxd -c ${nginx_config}
' u+ f( f3 A3 w7 q) GRETVAL=$?/ @) E8 D) i9 H5 ^* l
echo
3 x+ H$ D( W( w- y. @5 J: R# O[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx) w# d# i5 F& C
return $RETVAL6 s( c% a: e9 ~) z, k% _8 o
}( Z; [' {" R/ f" c
# Stop nginx daemons functions.
: D, P4 t# j2 h3 Astop() {
; b' q* t: J% \' i) Recho -n $"Stopping $prog: "
9 H9 j0 p' K) h2 C: ~2 ~; `killproc $nginxd
  y; T9 L# ^' K% o7 x6 s# tRETVAL=$?& n& K! ?4 Y4 W) e: Y) b
echo6 y6 B4 c, l2 I6 P6 p( |  u! j
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
1 d8 [9 G7 l, m* D}
+ |( ~: r# r9 e9 U2 ~' R2 ?reload() {
( v; o  a- h9 N# R7 E( |echo -n $"Reloading $prog: "
! t- [  z1 z( @6 b' d/ o! X) J#kill -HUP `cat ${nginx_pid}`
+ U4 K  h  ?7 E- a' i+ pkillproc $nginxd -HUP
1 s( r3 \% x- a( z$ u1 k8 H5 h( sRETVAL=$?" E5 h: {. A, q0 a
echo9 K! t$ _3 H! y: m" C
}
( w  D9 I; P% F, c1 L2 }3 {4 Y# See how we were called.
* @# p+ o) h! ?  y5 c) ycase "$1" in( r1 O. l7 Q: b( u- D2 H9 T# R2 U
start)2 z8 h! R4 {! a* b
start
- r" E; ~% r1 y* i$ B% r7 ~;;' ?& Y3 y8 X) u
stop), I& W$ _( a' A8 C9 F" E
stop
6 t+ u, y7 H% H* G$ i;;
5 x$ @/ q4 B1 ?reload)
5 Q6 f' z5 t, @reload
/ I. G+ a2 }! A5 X& \# X' o;;$ ~9 m, h  s1 k5 F8 |( Z2 H
restart)% L6 }( U  p3 f# p
stop
( H4 j; \% {$ b$ r( i9 lstart0 R  ]- j& K  T9 Q
;;2 n/ @6 M" t0 x. @
status)$ Y; b5 d. f( B4 R9 g
status $prog
1 f" R+ N& i7 @+ H. ~RETVAL=$?/ p( P; D( u4 ]- Y5 L1 K
;;
$ p+ j1 T- p( }2 o9 U' T, Z4 S*)
, |! C1 @  F/ y& ~echo $"Usage: $prog {start|stop|restart|reload|status|help}"# S- V5 W2 j1 w7 ?' J
exit 1
0 a5 w+ N1 j1 j3 q8 desac2 T: h; k+ J1 S1 n
exit $RETVAL  [% v; F, w) K& R! k2 u# p9 h
9 L! q7 s7 S: z8 z) J5 Z

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

- Y: i8 J/ |" Y9 G" t8 ]" \
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:4 J) r& a: b. @6 z+ X

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

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

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

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

4 ], [+ I* i1 P0 s! g
       location / {
+ J- [! s- V" w! x            root   html;  A2 ~& D8 N. U
            index  index.php index.html index.htm;0 H" z% L5 e4 o8 U/ w& R
        }
0 Z$ a, f! w( r# a% e- e% K修改为:4 N$ I4 w( c: m4 Q; A! x8 ~, o

, W4 |! }$ t, n) L' Z7 [% |

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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

' e- O+ l* b7 n" A" j2 f
location ~ \.php$ {
, F+ y/ g7 w1 m3 f: K            root           html;
2 {% O: r3 Z3 G: _            fastcgi_pass   127.0.0.1:9000;7 B3 O! s! J1 h% B2 c! d& ^0 k0 |
            fastcgi_index  index.php;
4 g: F3 P* a3 E: U- W! p            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
5 J' f1 N7 j: P; K- w+ |1 m3 u            include        fastcgi_params;
$ d; R7 ?# ^/ P% ?- O) n        }" Z. r; q' j* L* c6 q" M  Q. n4 v+ x
修改为:; t6 e0 `* B4 h
% H& R6 u- R, w- c) I- y0 q

        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

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


( m& Q4 _8 y1 W! e8 s2 I8 q$ g

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
4 P4 b: `( G6 J' K$ k% `8 j/ ?" u) V8 J
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。0 I  M8 x+ a/ f9 }; F, ~! m# o

执行

cat /etc/passwd 查看用户列表; p$ J- G+ _. g) R, d# l, t" N
cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。
9 W3 R9 y6 i( _* l; a: ~1 {

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
2 G& E  V  l( D3 ?/ D) T

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql* }- f2 S% J9 B/ _* @. l
chkconfig mysql on
( X; \( o) j4 }0 s; ^) Fservice mysql start  --启动MySQL
6 \3 f6 \' q9 ]" E% f

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
9 b: R: c/ W7 {& R/ l, N+ ?export PATH  ?- r4 p4 o; z2 i! @, i, ~

保存后source /etc/profile

执行:

2 F' I. f. H1 E2 \$ Z# S' T1 c
mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');9 Z0 o' u& `  b. c* }, A

0 r  `; z5 s! X) s

设置root用户的密码为root。

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

- m, n1 i* c- T8 z! N7 O

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:


4 B+ B% {) G7 V7 H

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

/ c2 S- _) G; u9 R$ w0 g- c' 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.rpm
4 J5 ~: S9 n9 M
) ?. I1 b6 d) Q6 w+ U% a8 Srpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm# S" z; w+ P5 C& P. t
Create /etc/yum.repos.d/php-fpm.repo file and add:
3 ^; `/ k; H1 d+ b0 q; o: m
  1. [nginx]
    9 J4 B  I- F- [& f* u& U2 g
  2. name=nginx repo
    / C! t% T; s9 V# P
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    4 N# n3 r  [  V' ?
  4. gpgcheck=0
    + N( @3 S( K) a6 I6 H
  5. enabled=1
複製代碼

; R; d1 ~5 Z$ ryum --enablerepo=remi,remi-test install php-fpm3 }2 q- B, Z, U
, c# H& w1 [, Z' |0 C3 ?7 i2 Q" Y! u
2 M" n! W2 S* H$ Y! j" {0 @
五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn
9 `+ m7 [; D4 P% p5 |

#yum remove subversion //卸载低版本的svn
, j/ s9 }& o1 K

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn
5 R& Y4 R8 q, Q! C! Q  O8 G0 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]
/ d( ~# Q7 _6 w' K4 |- J! d* C# harry = harryssecret# Q5 l( W8 I: ~/ n" H" ]
# sally = sallyssecret$ e3 a9 x$ y9 j# Z
wangning=wangning' F( Y0 ~6 V7 B% o
yanzi=yanzi
3 c; }/ J# U8 X1 f

#vim authz  //设置权限

+ U6 Z' h1 z1 ]5 E

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none
) {! a! X; a. _7 S- b8 yauth-access = write
" c- @& l0 q! p+ A6 f" w7 B! z### The password-db option controls the location of the password
2 ]! V- \3 K2 }/ x9 Q' O0 R### database file.  Unless you specify a path starting with a /,6 c$ ~: W% [/ l( P8 g+ g  a
### the file's location is relative to the directory containing: e; W& o' Y( w. @$ E  o$ I7 C
### this configuration file.
( R6 O2 `+ j- J+ \& }### If SASL is enabled (see below), this file will NOT be used.
+ y0 l/ {$ r# A& S4 J" V2 ]' }" Y### Uncomment the line below to use the default password file.# `: f. A% t7 G
password-db = passwd
  Y" r2 v+ Y( ?8 O### The authz-db option controls the location of the authorization
. c- ~4 Z' o0 o. Q$ m2 n! A( a### rules for path-based access control.  Unless you specify a path: @; B1 x, |% h, X  Q
### starting with a /, the file's location is relative to the the, m6 e/ m8 |6 m3 |9 j) X
### directory containing this file.  If you don't specify an
0 H' h* q9 a8 M2 L7 p### authz-db, no path-based access control is done.3 w4 }7 i1 I' C3 V: ?5 Y4 d) _7 j
### Uncomment the line below to use the default authorization file.
/ }! r' Q1 G7 Bauthz-db = authz
5 X, M2 k$ {# q3 ~3 \### This option specifies the authentication realm of the repository.
8 ]. d5 a  |/ \9 R# S1 O### If two repositories have the same authentication realm, they should4 l# v9 e$ [$ ]; V4 u* G9 g
### have the same password database, and vice versa.  The default realm4 K# V! T" Y; I0 _; H7 |3 L( g) W
### is repository's uuid.4 y1 S/ P0 n2 b9 ?
realm = My First Repository
5 Z7 F& Z7 M3 a; ?. n7 w

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn
" m' ^: o+ u/ Q4 l1 R7 p7 h

#killall svnserve    //停止$ W, o  t* g: `$ Q/ q: f6 H

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

svn checkout svn://192.1.15.222 --username xxx! Z6 f4 c6 \: y' g, @2 i& h

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/web7 a$ N6 u- @# f0 D$ P3 k

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

vim post-commit,在里面输入:

5 ~# ?, @; T7 b5 l

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" :
9 _- A9 T& n4 J. ~: X

#system-config-securitylevel

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

8 x9 j  ^' f4 e$ K. I
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled


6 i" n8 T8 X4 _3 l4 D/ _' A; {% cvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
% `7 W3 T. {* U6 A9 C: e




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