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) B5 z0 Z. m: ^2 E% W! {
  X1 L5 b0 E! J) [* ~* S

目标:搭建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' N3 p" P$ Y. P2 Z. |8 W

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx( e  J9 \: x+ T' n. v' d2 {8 x2 r

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

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

执行:

/usr/sbin/groupadd -f www* O7 ^# K4 j# e) M; E8 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
- W3 y4 \! t5 b$ M0 H7 n, `/ o# J1 R5 L# r/ Q  K
chkconfig --add nginx8 j7 f$ o) Q3 \! F0 [
9 |; G" {& `: n' ?2 g  c
chkconfig nginx on% e& c6 e& k' q

然后

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

#!/bin/bash/ E" I' ?4 x5 l* ^4 `  E7 Q5 H
# nginx Startup script for the Nginx HTTP Server
  L1 f! n4 @! H6 @# it is v.0.0.2 version., q. c6 Z5 q# e  A8 l4 K3 \) W
# chkconfig: - 85 15
* c2 w0 A0 c# [# description: Nginx is a high-performance web and proxy server.
- ^0 z5 B" z, b1 X8 C# It has a lot of features, but it's not for everyone.6 s4 j* O% a, z, {& o
# processname: nginx
3 m" w* H* M: y# pidfile: /var/run/nginx.pid2 \+ {) Y6 o( C9 |
# config: /usr/local/nginx/conf/nginx.conf
- O2 K- I; R* a- m: H4 d! enginxd=/www/nginx/sbin/nginx% h8 N9 |% V! r% n
nginx_config=/www/nginx/conf/nginx.conf+ i7 a5 P" z) i  _8 V( @
nginx_pid=/www/nginx/logs/nginx.pid+ x7 a  [0 Z' r* H. P' [8 R
RETVAL=0" V  q% C7 U) C1 r" c
prog="nginx"
, [6 v& g; J+ ]/ e# Source function library.8 s3 ]3 Y/ b$ Q: _: a
. /etc/rc.d/init.d/functions; j8 g5 e3 M3 l+ N, T# Z
# Source networking configuration.& D8 U" Y& F- l/ e+ R
. /etc/sysconfig/network
) U# x9 a+ X' `# Check that networking is up.1 l" G! t7 X8 O% W' `& f
[ ${NETWORKING} = "no" ] && exit 0  _: i$ N' f: q% i6 Z+ M
[ -x $nginxd ] || exit 06 M( r! w: [! j6 X3 K: D$ |+ g
# Start nginx daemons functions.& P7 Q; p0 B% J
start() {0 L: [- t3 t  F5 h5 J
if [ -e $nginx_pid ];then+ c2 g, H9 y+ m0 s. T/ O5 T% D# v. h
echo "nginx already running...."5 X2 {" B8 s- {. r2 \" J  e! K6 S
exit 1
! T6 K% I9 ~, M# i# vfi
& I- G% p+ Q6 n  i+ Q% X( Eecho -n $"Starting $prog: "' g" Q: M0 C: H9 Q9 _; s- [  B. Z
daemon $nginxd -c ${nginx_config}" i0 Y+ T, I8 j1 n9 @
RETVAL=$?
6 \! L& N8 e: W3 L. v! x, F7 S" gecho
9 C% N+ \4 v- A- K[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
! w( ~# z" s& `' T+ }, |return $RETVAL( }1 u; [$ p1 J( ]7 ^
}
- [0 H8 c9 E& }) H8 f# Stop nginx daemons functions.: z8 s! z2 e; R6 b( B5 i$ U
stop() {  c; f4 e( o; [$ F% m
echo -n $"Stopping $prog: "
6 n& o/ e9 u7 G( \& skillproc $nginxd
* }" \9 ]  c! YRETVAL=$?. n  M  A: S" E- d) Q
echo2 e0 Q+ z' b8 m
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
5 k* u7 X" g- ?/ p+ S# G1 n; |' F}, V5 o* f- Y/ C3 @0 k: L
reload() {1 i/ ^8 K6 w4 I! Z) |" g
echo -n $"Reloading $prog: "
1 \% e1 F, W; E#kill -HUP `cat ${nginx_pid}`
/ O* p) F7 F6 J$ `killproc $nginxd -HUP
; M3 l% f' D0 u, N4 d. `" r% ORETVAL=$?
; N# D% |, N7 f3 }: C0 W% R1 Kecho/ l+ y: I! ^) s
}( r( R: q( j! S5 ]$ r+ o. `
# See how we were called.
, I/ a8 ?+ z) mcase "$1" in+ ], K1 H# \3 c& c$ E
start)
- {; t( T- L/ L+ _start9 ^9 f# g: B6 ]! e% ?  a, P
;;
- M0 |' {7 U$ {5 b" R8 M: Xstop): S" {; K% h# \' I' F
stop6 U  q7 f5 y# m. R$ H9 w' d+ u
;;
/ l! I2 L) X) }' n) m8 A, o$ C" Sreload)* T, _: J1 C* z8 X" W) n
reload0 n* ]+ S+ l& W6 @; _/ o
;;7 ]' `+ z8 h! x/ s6 u
restart)( \+ D& N$ W3 V, ~# u0 W7 l+ Q
stop! h, V$ I. f/ u4 X/ K
start2 p9 q% B# ^. {
;;
9 ?; ^& l4 y# e$ K3 wstatus)
; y9 P5 N  i5 _" Hstatus $prog
6 x) c5 G5 l" ]" t, ?2 ARETVAL=$?6 _9 q1 x  t$ I" F& N& t- b( h
;;2 D% l6 F& r# y- q& _3 Z4 A0 ?& H
*)
) H" j% h2 {% K6 |) ]8 gecho $"Usage: $prog {start|stop|restart|reload|status|help}"
) N' H# Q1 Y: X) ?$ d% N5 D& O  Vexit 16 u! ~2 e- l7 Q$ I1 ?
esac  _% ~& ^  m* ?7 g1 I* B6 M
exit $RETVAL/ x' p! z$ [# H. L) |$ ?* w
# t) k' b6 m% U: w

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


/ G5 ~" X; |" I7 S) K) }5 N7 C' i1 q. ochmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:
6 |! z% S' M/ w# p% q

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

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

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

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


  F/ g- P( Z/ t+ J' E       location / {% i; G- Z1 O: l: B& E) G# V" m
            root   html;, m2 i/ O( g1 \. G: T1 w
            index  index.php index.html index.htm;7 T) n8 [7 `! ]( w) ^
        }) L" @8 {. D! |# {$ B
修改为:
6 G7 Y/ u1 G* x# q0 [4 F
- J/ j4 @3 X0 M9 K% w$ r9 m

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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


. K3 m6 h! ~2 d  {2 q  C+ n% elocation ~ \.php$ {7 ?; @) ?; d2 k' l4 ?5 ~/ I& r+ ~$ i
            root           html;
3 U/ y# c3 c8 r            fastcgi_pass   127.0.0.1:9000;
2 Y/ m# E! w, e5 x# x            fastcgi_index  index.php;
, z) j! T8 l8 }) K" j4 V' h            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;& i( J: o8 e& N: B
            include        fastcgi_params;! B' v; F+ v2 H* d! e  [4 Q: J/ ?
        }
9 i) `! E$ s/ H4 i( t修改为:  O. I; ~4 ?% h# A

3 j5 H! @4 d+ W. u6 X+ L

        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

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


, x* l1 z" m8 \$ O

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
7 Y9 w* X' E) ?3 }+ ?( p2 G
4 c7 g3 q( L% f( w- R#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。5 F) W- P( A* ?( |/ m0 A

执行

cat /etc/passwd 查看用户列表
& @) D1 M9 e: {( u. s" kcat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。( X+ \  a2 |5 a: g

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
* Y" R, }0 m' M5 g

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql
1 N$ Y5 N, ~$ k4 Jchkconfig mysql on. u+ C2 m" E6 w
service mysql start  --启动MySQL2 d. s1 ~3 t9 v4 @7 n4 d% M5 L

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
- w5 M9 T6 K) D2 B0 }export PATH
4 [! ~# ^& ~3 Y3 N# @

保存后source /etc/profile

执行:

$ @7 P$ S5 |/ O' J% q: D# ^* y
mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');2 o) N: O& e; f1 w& N
' x: V5 v' g9 F7 ^

设置root用户的密码为root。

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

" |1 s/ Y# s$ w) ^4 N! p4 A4 F8 F

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:

) C6 d. w% ~- X: L% d. y

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


8 R+ K! G+ O; R7 x6 Z. I8 z# B' q- l

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- H2 Q" I% A) O" Y0 U1 X; _& V
2 k/ `, R# R2 S5 S
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm% }; [! i% p3 m7 p) f/ y/ `( P& A
Create /etc/yum.repos.d/php-fpm.repo file and add:
; E9 F* A$ v3 I3 ^: A
  1. [nginx], N0 ^1 r+ u% ~) U: f1 ^
  2. name=nginx repo
    ) ]4 C' J. L/ ~9 {' n' b
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/' s/ ~2 {9 l3 j/ U' c1 h1 o! A3 _
  4. gpgcheck=0& X1 F. m6 D3 c5 b# t2 s
  5. enabled=1
複製代碼

0 g+ }4 G# _! U2 V' @4 oyum --enablerepo=remi,remi-test install php-fpm
6 l1 u2 q" q7 Z- T( \9 `$ X% w* J& L9 M

# G4 B' F) \# g! i; C5 K& S五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn/ D! u. n2 p& t

#yum remove subversion //卸载低版本的svn
" G8 f- d( M5 |9 U' r! E

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn' t. `/ l, t. f4 k# i+ D

通过# 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]
! v9 }1 h3 X" g' m# harry = harryssecret4 p) H6 R" [2 n+ j4 C8 O3 L
# sally = sallyssecret, P/ D$ H+ g: g. B2 }$ z+ ~" W
wangning=wangning
1 z/ B5 V9 U8 j! v0 k4 V4 W: Hyanzi=yanzi
/ O7 ~- q9 y6 w5 V( I3 h3 T

#vim authz  //设置权限

6 Z2 D& M" y: B$ n, p1 u, e9 K% H

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none7 \3 f5 R$ |- i3 @4 y
auth-access = write! j6 Q7 p' a% V7 e
### The password-db option controls the location of the password, A+ j) q( V$ R6 X2 V
### database file.  Unless you specify a path starting with a /,
* t, l5 [4 n* P! e8 r### the file's location is relative to the directory containing
; u: K' i$ f! v! G: ]### this configuration file.
  L6 G% ^( C4 `' b9 S  e# V& }) U$ |% ^### If SASL is enabled (see below), this file will NOT be used.. S+ m6 ~1 j- q- H; R5 c
### Uncomment the line below to use the default password file.! d. g) ]* G3 K+ F
password-db = passwd
; v9 J2 q4 w5 n& U; Y### The authz-db option controls the location of the authorization! |' J2 z! f$ L; g
### rules for path-based access control.  Unless you specify a path
& q/ D- {7 G( D" I( M6 V### starting with a /, the file's location is relative to the the
) r- ?  C# Y5 ]% `$ i% J### directory containing this file.  If you don't specify an) s' H( N1 h+ g' Q+ t
### authz-db, no path-based access control is done.
# R0 M1 I$ D. X1 t1 ?### Uncomment the line below to use the default authorization file.
7 B, q3 ~0 g; `: Y( yauthz-db = authz
3 k4 W) T! b, S% d### This option specifies the authentication realm of the repository.) b  |" b; A$ [" n! f# C3 e5 N: h# {
### If two repositories have the same authentication realm, they should0 _& e% r7 _' q  |; h& {4 C9 p8 E) O
### have the same password database, and vice versa.  The default realm
; P" I5 T/ M2 }' e& t! E### is repository's uuid., X4 z5 n/ _* h: ]; {5 w
realm = My First Repository
, e2 A; }1 D; ]* B' L

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn: N/ T2 m" ^8 F

#killall svnserve    //停止
- ^; v3 n* W& S7 P6 R% m& m

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

svn checkout svn://192.1.15.222 --username xxx  [) P& i3 J! f) }/ [, _

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
/ a2 ]8 g: T: \( [7 L0 Y# r

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

vim post-commit,在里面输入:


+ ?2 I  p) }5 J& r% Q

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" :2 X9 X! P  {( z

#system-config-securitylevel

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


# C2 Q' J, j/ L& A# I$ U! F0 M執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled


: ]; c+ t4 E$ J: Y/ Y8 Fvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題2 v* x8 ~) H3 ?





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