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
* e! \6 r" @0 j
; \1 x: t0 d/ O0 H6 p0 J+ U" t7 o* 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 cmake3 a! [/ g$ ]7 m  ^( 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/ {1 A/ p  i( q; @

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

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

执行:

/usr/sbin/groupadd -f www
8 D% D  A* X" l' q% V' P% f/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx
2 i! `5 T: Q. ^+ l/ A  F
) Y( Z- W/ J1 f3 J5 K8 S" s; m5 ichkconfig --add nginx
7 N6 D8 X" a: P8 l
" [0 ^' f) A4 f+ J7 Cchkconfig nginx on
: O, K- h7 j2 P$ G* E! A# L

然后

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

#!/bin/bash
4 B4 s* R( V' d& a# nginx Startup script for the Nginx HTTP Server8 I$ n$ _/ b/ _+ [  L
# it is v.0.0.2 version.4 ~/ {2 g8 c; i
# chkconfig: - 85 15
1 |5 _9 V. g5 E. |" q# description: Nginx is a high-performance web and proxy server.3 F4 N( u& y( e+ T. t3 a1 P, M
# It has a lot of features, but it's not for everyone.8 C; M5 ^* E: `  ?7 Z  ]* j! b
# processname: nginx
- y, B2 ?* Y8 |7 n3 W0 a8 C# pidfile: /var/run/nginx.pid4 c8 k' G( Q1 {
# config: /usr/local/nginx/conf/nginx.conf1 `8 f& \8 `% ?% I$ R& w
nginxd=/www/nginx/sbin/nginx& Y2 N$ m1 r0 @5 B2 ~
nginx_config=/www/nginx/conf/nginx.conf1 c0 O* m% U# U  ~
nginx_pid=/www/nginx/logs/nginx.pid/ e) b; n4 v0 W/ w2 Q7 v. @7 X, e% Z
RETVAL=0
3 X6 F+ t9 F. w; g; _2 `* O$ |prog="nginx"( f9 H) k& o) ~$ ]! h- m+ t
# Source function library.
& h% M' D8 o3 o. /etc/rc.d/init.d/functions
3 |% n9 o# Z' v# Source networking configuration.
! l& H! l+ j3 k1 }) f0 E( n4 k' ]. /etc/sysconfig/network: u5 V, Y) B  [& f
# Check that networking is up.$ i) e8 b$ P2 R) W) `# q/ m, K
[ ${NETWORKING} = "no" ] && exit 0
7 l! Q1 |" N" w3 |& n[ -x $nginxd ] || exit 0
; F1 j' k9 v/ @& M. w" X2 Y# Start nginx daemons functions.
' o' N/ s7 S' _" ?. \start() {
0 d5 S$ k5 ~5 @1 a6 oif [ -e $nginx_pid ];then) W+ P! T1 [/ ]% q
echo "nginx already running....". r! F  r- S  y
exit 1/ a; p3 J2 U8 R( o' J; ~$ I
fi
# K& t0 o3 \2 X& P7 L9 O/ m3 Y; Techo -n $"Starting $prog: "& K( h+ E$ [  b1 b6 [" \" J. k
daemon $nginxd -c ${nginx_config}2 g5 ]2 C! S. q
RETVAL=$?/ X2 Z6 G) I. q5 e1 P* A
echo
6 C* d# q3 n7 V+ T* E2 W[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
4 r; h5 J- ?7 v  f2 y' Freturn $RETVAL
+ l, \. s9 x" ]8 v3 Z}
2 n' B2 f/ N/ e' L4 B/ X# Stop nginx daemons functions.
5 ~- S/ t; l; x# Z+ `( I* Wstop() {' j/ g, p( z8 C$ `+ b
echo -n $"Stopping $prog: "" Z: ]0 X/ n8 F' B- U5 p+ g
killproc $nginxd; H" p0 j% i2 I, r: G
RETVAL=$?5 G4 S+ V# \/ @- q: ~) k4 @' o
echo7 r0 @) q" S0 |# Q5 c
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid: ?. [# k3 P  ?
}
9 c( |- [) o3 k9 T5 Qreload() {' z% a4 g3 L3 X* M& z3 m6 V! O2 k) _, c
echo -n $"Reloading $prog: "
& z) V% ]5 N0 x3 `7 O& W% T. F#kill -HUP `cat ${nginx_pid}`5 n( z& C5 q1 Z- v
killproc $nginxd -HUP
$ M0 V% s' ?% U) r# ^" lRETVAL=$?( a. x7 w+ j* |) B
echo/ ?8 L, X# c% h
}: ?! o; C& H: y2 p/ w' t. {8 W/ e
# See how we were called.- P- K3 f* y2 S: b5 S5 A! c
case "$1" in
* q- A8 j1 c3 R' Mstart)
, v. c  ~: |8 u" @7 Nstart3 J, U. D% ]+ v2 P) b
;;
; r5 J/ P1 \  k5 C- Sstop)! n3 C+ h+ a( ]7 W$ ]
stop
+ K% U$ M5 i& v) J3 H+ m; D+ ?;;
. N$ K" g+ g( ^" y7 j* Creload)
) n7 o3 N6 U1 a( X# Y6 C4 Freload* V+ |- f. ~1 P
;;! H* X& s  O$ s) m- |, d- M
restart)9 R: j1 B$ k' {, p( L
stop
+ n! w% y" p' I7 e& \( w# \start8 ]& D$ w) o+ a- {0 n0 x- Z
;;$ K6 y* m( o& G6 x/ l. a# g* B
status)- }3 ?2 {, C! g' E
status $prog
# ]/ R' {8 }( N0 l) i! ?RETVAL=$?
: M" J6 h" \7 g/ g6 S' h% A0 x;;/ h$ O) C4 z! P
*)2 T; O9 }. s: h) |  a5 J- v' A
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
+ M* F6 }' V0 Y- hexit 17 l( c  ]) }1 Q( u) `
esac; R" r8 e+ B7 L7 x" W; Q# C& B. Y+ z
exit $RETVAL9 h' N7 a- r: ?) y
# D1 A1 P6 S" v; e/ k/ G

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


% H' V+ z8 [4 b, B& j6 e0 Pchmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:
, G1 K: Z! O+ F8 l  ?2 N$ P% R

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

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

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

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

! z3 z9 `; k0 ^+ T& {. d" G
       location / {
5 x  u; n$ W1 G% A5 `5 b6 A: `/ f+ T" X            root   html;2 S% V* ^9 L3 t
            index  index.php index.html index.htm;' P/ P# I6 V+ L4 D/ }) K
        }! c# e" k  b" k# G
修改为:1 a4 H" V% B. M8 c% p

& a; U5 F/ a' k, E

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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


' s: X7 p! q% \location ~ \.php$ {: @2 o$ w5 [' G6 C, p
            root           html;2 Z5 P7 k# {& x! d
            fastcgi_pass   127.0.0.1:9000;7 J- [5 z- t7 g% B6 X5 r
            fastcgi_index  index.php;) r9 X9 t4 K% F
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
! C4 b! N" @( q            include        fastcgi_params;
8 S$ ^, s1 g7 q( J5 I7 t  {3 h        }6 ?2 t) {6 L8 q
修改为:
, X) N+ k3 }+ B
& O7 |3 C" ^% M

        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' {' n- _2 C! o# o' A

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql' z$ G$ V) |% t9 K4 k
- _0 M* p' i" c* X+ k/ y
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。. M# a9 I" `3 k

执行

cat /etc/passwd 查看用户列表
% t6 S$ y# S% R9 \+ E( Scat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。
- H5 [' P6 {) V+ Q9 R# P

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
9 i0 L+ E0 U% T5 `) v# f

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql) Y7 E1 k0 N5 g# q" j$ R- _
chkconfig mysql on
1 M) N, p: r3 o& @% \service mysql start  --启动MySQL
3 e7 u% a3 |4 p9 J3 n

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
1 \& ?' h* S5 j6 xexport PATH1 A# H) X: k3 u' o% \

保存后source /etc/profile

执行:


& p2 G2 Z4 u# Z% S" n0 bmysql -uroot  mysql> SET PASSWORD = PASSWORD('root');
& b" N: j3 C6 b
) c! A! a5 c- e

设置root用户的密码为root。

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

' S6 k' q! C; Q  r! C$ T

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:


3 Q4 ^, {* t# N6 u9 M

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


  H" [, ~8 `/ Y9 C  H, R

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.rpm6 A5 I( d! R# I8 e0 v, N

% ]/ }- e& v1 [rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm9 l8 K  C* ~) C# |
Create /etc/yum.repos.d/php-fpm.repo file and add:
/ s3 q' t+ w2 ]' S$ t/ ^: D
  1. [nginx]; d, S& f3 k4 G
  2. name=nginx repo5 h; v& p  d8 _' l& z! ~' u/ E
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    9 g% T4 L' o4 i) D2 o
  4. gpgcheck=0
      `# ^9 l6 i3 h
  5. enabled=1
複製代碼

, I! N3 R; U  P3 G4 e; [' J: Jyum --enablerepo=remi,remi-test install php-fpm0 x* L, g/ s/ m; W/ j3 _8 `
/ ^; t0 S% `6 t5 E# y

0 A, z6 k) J) w. [* @五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn+ g+ W* m" d. y: c% R

#yum remove subversion //卸载低版本的svn
+ @. v6 y! h$ t% \+ `3 d% D

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn8 S9 R7 l/ d4 m9 R2 ^, z; V; X5 Y

通过# 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: c1 U1 J; g5 b
# harry = harryssecret3 R8 _( v' A8 n$ V* E9 B
# sally = sallyssecret) J8 L, I" U- T8 R
wangning=wangning
( z& c5 z( D0 ?$ S. b: vyanzi=yanzi/ A, C' e% p0 }0 \4 m1 L4 K& m

#vim authz  //设置权限


4 Q* ?1 P) ^" q! `1 f( Z

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none9 `$ s% S+ f$ L2 {$ U. s. p
auth-access = write
. b. Q9 o1 u( F### The password-db option controls the location of the password
8 R1 x' j" Y- u* ?* f### database file.  Unless you specify a path starting with a /,% Y- w/ `0 ^% Z/ \, U" W: V1 a
### the file's location is relative to the directory containing
9 r' z5 B- w; s, A# V3 A6 R### this configuration file.# d# n  ~4 U. V  ]
### If SASL is enabled (see below), this file will NOT be used.9 z; l2 p' E  r% z: l
### Uncomment the line below to use the default password file.1 l' E! N1 u* a8 R# V% _  t. D  v
password-db = passwd
/ B- y' B( d8 ]( y- ~3 K### The authz-db option controls the location of the authorization" z- a* n2 p1 O$ I
### rules for path-based access control.  Unless you specify a path
2 N+ ]) Z( i9 f" @, `### starting with a /, the file's location is relative to the the
) E) \! W7 U0 Z### directory containing this file.  If you don't specify an: |+ N: P2 M& h
### authz-db, no path-based access control is done.# b( U- D" ~; B* Y. J; m
### Uncomment the line below to use the default authorization file.1 X0 Q; \  K& R
authz-db = authz  K! V  a! d/ n$ P" d0 \
### This option specifies the authentication realm of the repository.
/ h% y2 f/ ^3 U0 X% x2 k/ g### If two repositories have the same authentication realm, they should1 h6 N. d5 K1 v% w
### have the same password database, and vice versa.  The default realm
( a; L4 ]( l1 ?1 n$ G### is repository's uuid.# t1 F" A/ Q1 z
realm = My First Repository
" z4 N0 s5 B8 T3 x  C/ N

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn
* `0 B9 I  m9 h: [) k2 `, D

#killall svnserve    //停止
. z* L/ A  O, U# q- ~+ C

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

svn checkout svn://192.1.15.222 --username xxx7 t8 E, u5 k" f$ N  v* O8 w

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 ^+ V6 y. R" i1 Q8 c1 y  E

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

vim post-commit,在里面输入:


, o' g3 ^' D3 y! |

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" :; e2 U, E/ x( V9 z# ?  E

#system-config-securitylevel

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


* S0 s: ^5 S7 {, }執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled

" a( P$ r% }3 }
vi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
$ _4 A* i- R5 {) u( h( r




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