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+ A/ b! |! V. I
/ ]: I4 A7 |4 v. B* o0 K: P$ u: X' W' h

目标:搭建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 cmake1 J( t5 j0 |4 j- X: L# ^

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx
6 [% C5 ~% |' d6 b  T

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

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

执行:

/usr/sbin/groupadd -f www
7 D5 Y: F' F. q9 O( r/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx
7 E7 E# l5 u  i2 J
4 e+ y) m1 U9 ichkconfig --add nginx; E" f3 {7 R3 X" T, |: s, H

( X; E$ O! G% I( x  ^, ~' D& rchkconfig nginx on
; @& s& P; Z% a/ E4 s- ~9 f) m

然后

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

#!/bin/bash
* e3 V/ E; c7 N, S  @7 y6 x: X# nginx Startup script for the Nginx HTTP Server
! S5 q) h. r6 k, M! R6 L& Y# it is v.0.0.2 version.
$ m0 I/ k! S7 p7 }1 B6 T* V# chkconfig: - 85 15
& u3 ~) Y! l) |* M) V( h) R# description: Nginx is a high-performance web and proxy server.
# b8 q# u: w6 {1 ]# It has a lot of features, but it's not for everyone.1 |' t% D/ E3 H% H1 l* ?
# processname: nginx
% m, S0 e; o, p  R) K# pidfile: /var/run/nginx.pid( o/ Q+ L% z0 J
# config: /usr/local/nginx/conf/nginx.conf+ K8 m" u* [+ d; R' R
nginxd=/www/nginx/sbin/nginx) R3 \6 R) o$ o! O: ^1 z4 P
nginx_config=/www/nginx/conf/nginx.conf. y, h' P( `# ?  K
nginx_pid=/www/nginx/logs/nginx.pid+ Z* m! I9 Q& P5 `. a) X5 C
RETVAL=0% c- O: \2 k. }: c' X' o
prog="nginx"6 J* f6 r) t  O) {' e! c
# Source function library.
/ W' T3 y$ G6 e- U( y1 B' v, U. /etc/rc.d/init.d/functions
4 p9 g2 c  w1 U# ~' ^3 @. R# Source networking configuration.
. p  s+ s0 f+ Y3 e+ i% Y4 j2 p9 O. /etc/sysconfig/network2 Q& e9 I. K1 U; k4 w1 K
# Check that networking is up.& Q# W& R2 P4 ~1 _6 e3 z
[ ${NETWORKING} = "no" ] && exit 04 J+ v+ r2 D$ \4 {# w/ W  `
[ -x $nginxd ] || exit 0
% [* U: `, C' l9 T. V# Start nginx daemons functions.
: `! Z/ K) v6 S# Y; g. K  x  Fstart() {
4 w$ w( L" D+ f) N8 hif [ -e $nginx_pid ];then7 o: C" h0 |4 ~" x9 S
echo "nginx already running...."4 h0 ]2 a9 H8 b8 h
exit 1
1 Z& w" x; |: a. v. ofi5 B& c% {% I/ h8 @- p1 p: p
echo -n $"Starting $prog: "
0 n4 ^  v- ~; kdaemon $nginxd -c ${nginx_config}
( ^" R/ L! U; C; XRETVAL=$?
, B  o- _( H) T; J1 D$ Uecho+ N! h- o7 h. f9 ?* q, q
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx, Y- y2 \- f1 T/ f2 a/ i& n
return $RETVAL
' @3 O% P( o$ x7 y}
8 w3 ?& E+ s7 o+ n8 Y% j+ m' v# Stop nginx daemons functions.7 r1 `7 ^, p: j) G4 K" @: U% Z
stop() {7 r  N; J5 M! K, S
echo -n $"Stopping $prog: "
- }$ E, H/ s& C( Mkillproc $nginxd
' s0 k  ?  D; CRETVAL=$?$ \/ c" b( f- P+ [- l1 J* U3 m
echo
- \) N( m( C& ]6 I5 Y( W$ t[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
8 w! ^& ~! U& H% j. j! @1 I: N}
; k) u9 h0 P' i  @& B3 Creload() {
% h& C; r0 L6 I6 Fecho -n $"Reloading $prog: "& \, Y: b) y8 q/ l1 @
#kill -HUP `cat ${nginx_pid}`; a! j: o1 B3 m, ]- V. m
killproc $nginxd -HUP
. h! A6 j4 R% K6 A1 tRETVAL=$?" }4 ?% }. J) E, t4 Q
echo8 R$ S0 S2 a) H8 d5 Q7 U7 a, `
}/ P; F# Q9 i( W1 p$ z& ~. s
# See how we were called.
. m2 n# V. h" ~case "$1" in
# c" m$ N# L( M: R; \$ @$ H% Xstart)
! [4 p: N. X6 _start
! z& a5 I% e. O$ K" \) `;;
3 Z5 o: [3 b5 _/ r$ dstop)
( }( C( Z8 r: j, p. ?* dstop+ z- l9 Q0 C& \' k" Z6 N
;;
, U$ I, {$ X1 y6 n9 Creload)5 F0 z; z. }# F! S6 k5 `
reload
( L7 t" T! J8 X. X$ F;;6 U/ N6 Z% L( d- Y  w! e
restart)
$ T# ~1 x# T( o" o3 t# Jstop# P; V' x- j' r0 T3 c7 S  u: z
start& l# C/ c# D% m- B0 q1 V2 X
;;4 Z6 D" `& e8 W% N0 f: `& v: q
status)
8 f' j/ A) K! d' C6 l9 Cstatus $prog
8 z  |( `! f9 [' u- @) u! Y& kRETVAL=$?
8 m$ I& {' ~" K;;. ?( A2 n3 ^6 x
*)7 V2 t& u' T# m6 |8 b
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
3 A+ Q1 K/ s8 q# G* I, y& w+ texit 18 }2 B( [3 K9 V$ m
esac
: L/ n" N3 m! A6 D2 W6 {6 ^exit $RETVAL+ h* A# P% c2 k  u

# U' I2 t# G) B2 ^; C$ _6 I

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

# S9 E" C& R1 @" w. b! ^
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:: C4 A) W8 W, m$ s  \: ~9 u8 S5 u

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

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

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

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


0 u' [( U; m) h% B3 I, q       location / {* R) w+ @2 {4 a$ L- j
            root   html;, V: f8 V" ~' y. x, ]
            index  index.php index.html index.htm;& \; B! j. u* m% l  C* a2 v; e
        }% \! I% @4 k! P- u! A& b5 U
修改为:
$ z% ]0 ~' e" i7 U5 q: f5 q1 }
; i" Y# H& A0 I6 g

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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


; \5 M) D2 U5 P# ~location ~ \.php$ {
$ g* r6 b1 ^+ n% l% C            root           html;
7 L% V& }0 ?4 O: q- l            fastcgi_pass   127.0.0.1:9000;* f$ n) e$ F  F
            fastcgi_index  index.php;
  z$ P2 j/ T8 e7 P3 s! y8 H            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
5 C0 [; g. P6 ?5 x            include        fastcgi_params;. h9 ]. n1 E, Z' ?' d
        }- v- b, F% i' B& F* e
修改为:
' n& ^% T0 o4 u5 F
* D1 z  Q7 Z8 d& t8 I! B

        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

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


+ J5 E7 {8 n& _1 U0 b, S

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
' |! \0 V8 ^1 V6 j# u, [" h
4 H# h# D7 L( P( S7 G1 i#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。+ j. W- M- `# c3 I

执行

cat /etc/passwd 查看用户列表
2 p3 M. E; M9 V# U$ o5 p* Zcat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。6 W* D6 W  }. r  T& f4 k

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
4 q# e# \$ R& S8 S* Y

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql. @  ^* c2 H$ G) c6 C
chkconfig mysql on
6 j# W- C2 `  E) Mservice mysql start  --启动MySQL
- h- L; e3 y/ D1 d, b( ^

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH, e7 [4 Y% t; N) [0 Z9 c1 S! X
export PATH  T% T2 H6 A, C7 p

保存后source /etc/profile

执行:

6 Q8 j- h4 |8 n7 M
mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');
8 a1 |+ @4 a. X

$ b6 w# @( x" G+ |1 x& T: Z

设置root用户的密码为root。

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


2 {; \2 i9 w/ I/ r" a# c

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:

6 I$ E7 A9 E# _$ A# v  t7 c& z7 g4 E& q! ]

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


6 O) ?3 ?+ e; p, X

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.rpm8 @& ?5 T; w/ S  G3 Q

1 k! t6 s3 q( o: drpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
9 `" d4 |. T8 o( `- TCreate /etc/yum.repos.d/php-fpm.repo file and add:
1 P! U! Q' Q% O8 V
  1. [nginx]
    % N5 D. w+ K2 `3 j
  2. name=nginx repo
    5 j9 [- s+ B$ c! n3 w' J; y$ L
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    * [, U+ h+ k, M% S' T
  4. gpgcheck=0. s4 |5 Q3 I- A& E
  5. enabled=1
複製代碼
! T: @1 m4 I$ T4 X
yum --enablerepo=remi,remi-test install php-fpm- x+ X( [0 Z8 E+ X2 ]
+ L0 _; B$ q$ p5 o3 |
' C, C$ F0 s" f- ]' p
五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn
0 x! o) f8 O  i! f  A

#yum remove subversion //卸载低版本的svn
* U/ z/ ^9 C3 s2 X

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn4 }! `2 S8 h+ K! L$ O! i

通过# 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]1 I0 X  w+ W  F$ y
# harry = harryssecret
0 G& o- W5 K, T+ d) ^# |3 P5 y& B# sally = sallyssecret
2 R# _6 s; v2 W% L4 \% t6 Zwangning=wangning1 ]- F0 }+ {2 r7 r. k: U
yanzi=yanzi
- C3 J5 g0 R: \- J5 X# r

#vim authz  //设置权限


1 O3 ?+ \! a3 k

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none# a5 G: e" `5 }2 e' d( d
auth-access = write3 V$ D, a" A) t* }5 F: l
### The password-db option controls the location of the password' u: q- }; b5 w2 }
### database file.  Unless you specify a path starting with a /,
$ I$ P0 T! A5 a( {. @: S$ k### the file's location is relative to the directory containing
4 {4 Z2 E0 R& f9 K3 Z### this configuration file.
% r0 j: |7 B& O" D# Y, C, Y### If SASL is enabled (see below), this file will NOT be used.
. J- s! q" j; I/ C4 [, g4 x* u$ J2 x### Uncomment the line below to use the default password file.7 T3 @' k6 J' R
password-db = passwd
' F. f  m/ l5 |3 E! `) y### The authz-db option controls the location of the authorization( Y! ^( l% w) V; |9 U
### rules for path-based access control.  Unless you specify a path
6 X. U* _' s  i( @) h+ [' @### starting with a /, the file's location is relative to the the
  u: R3 c& D; A### directory containing this file.  If you don't specify an' ^* U9 x" s) T6 T4 Y5 [5 `$ }
### authz-db, no path-based access control is done.5 T' m1 K+ R) d) L
### Uncomment the line below to use the default authorization file.2 B8 K+ j4 @$ H8 w, ^* f  n
authz-db = authz
' t: O  Y7 K- h. I) o### This option specifies the authentication realm of the repository.
" w; P/ v. Y2 m5 P+ |: P/ a### If two repositories have the same authentication realm, they should
' i  L$ U$ T8 q! V7 M### have the same password database, and vice versa.  The default realm: v9 g7 D2 \9 ^' C, J& ~
### is repository's uuid.
# x! d0 g% q! K7 m: p( z  U4 Yrealm = My First Repository
; W/ d8 L1 ?* N7 S, h0 b

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn" @0 d) r; x. B  I% d8 @

#killall svnserve    //停止. L# {) ~3 u- ~

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

svn checkout svn://192.1.15.222 --username xxx
  o$ r: Q) v' Y

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& Z7 f9 c' ?; T. l5 }/ Z

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

vim post-commit,在里面输入:


" U: j  J' R3 a, b2 D& [7 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" :. p9 ]+ }2 q; o, m* G

#system-config-securitylevel

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

4 u( x$ M3 a; C6 K9 |6 M
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled

" _1 u" _1 p, g) u3 A7 q5 D$ H
vi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
& H5 A( E7 l* R1 ]9 M




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