52AV手機A片王|52AV.one

 找回密碼
 立即註冊
查看: 1438|回復: 0

[nginx|php-fpm] CentOS原始碼安裝搭建LNMP(包括nginx,mysql,php,svn)

[複製鏈接]
發表於 2015-10-31 15:13:09 | 顯示全部樓層 |閱讀模式
tid=21242&參考自 : http://blog.csdn.net/yanzi1225627/article/details/49123659
* N# D" S2 u, E! l
; l) z* i/ z' G; T- i; f- 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 cmake
4 l& T4 K. p, y/ P* U

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx
# X4 G, R2 \  _

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

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

执行:

/usr/sbin/groupadd -f www/ f( @, X+ x$ c: u  H9 ?
/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx
% P( i8 T" b  b2 @% m1 z2 I0 [/ x1 V$ x8 h% i2 U0 q2 I
chkconfig --add nginx& v" h/ k, s: G8 V% T

9 s& `7 k  o3 r4 k. d  Wchkconfig nginx on8 E( G$ ^4 R/ L2 o5 p7 u& k

然后

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

#!/bin/bash  m# s' l: F2 w! P. N
# nginx Startup script for the Nginx HTTP Server
' m$ ~- Z$ [- S# it is v.0.0.2 version.2 N0 o. i5 H" u7 ^. o8 R$ i% R
# chkconfig: - 85 156 n2 v/ `$ G5 W) j  R: F0 u9 F- A
# description: Nginx is a high-performance web and proxy server.
! {5 Q; k' ^+ b( p  [7 O) Z% B- A# It has a lot of features, but it's not for everyone.: e& h8 f# j5 S6 [
# processname: nginx6 T( w6 H9 G) W+ w8 F' ~
# pidfile: /var/run/nginx.pid
4 i; ]$ S% Z  [- N9 ^# t1 J: D, ~# config: /usr/local/nginx/conf/nginx.conf
0 j, W, N% F# E  k; Xnginxd=/www/nginx/sbin/nginx
: Q! d1 g7 X' Hnginx_config=/www/nginx/conf/nginx.conf
& S1 D2 b2 O0 `3 Gnginx_pid=/www/nginx/logs/nginx.pid, L; o% }/ j& l$ c( f
RETVAL=0
9 p8 Z) o) ]1 l- E# x8 |' ~prog="nginx", W* c# ^  h: Q. [0 j0 B& E4 t1 S
# Source function library.! S" h6 J% r7 _* d2 \; N* b
. /etc/rc.d/init.d/functions8 g/ u& b. y0 c* T
# Source networking configuration.
) |+ C/ W- @- {2 D1 O6 A. /etc/sysconfig/network
. ^! k5 E9 D/ ?5 u+ G& T7 p# S# Check that networking is up.
# {2 F- o4 W1 v( Y: b[ ${NETWORKING} = "no" ] && exit 0) e( M% g0 g- y6 _
[ -x $nginxd ] || exit 0$ s- c) f: f! l* Q2 S4 Y" ^" i, P2 q
# Start nginx daemons functions.; Y+ J1 e/ _8 F. @2 {: B& W
start() {; ]* n8 ]6 d$ }
if [ -e $nginx_pid ];then0 D& t2 G8 D# I1 V: i3 Z' f& R
echo "nginx already running...."5 V& x5 t. q0 Y
exit 1) ]* N- u5 z( r* v$ d; S
fi
* ]) T: U) c& e, S' N' y6 Necho -n $"Starting $prog: "6 j+ y7 s1 T$ k% V
daemon $nginxd -c ${nginx_config}
" u& c: J) F  O% h" J0 sRETVAL=$?
0 [9 m  x" u- H) A& E- }7 f6 Uecho
% ]  R( p6 I! s' C* U) O[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
6 d9 C& R# @' L2 Y9 jreturn $RETVAL
1 B/ {$ _" m3 q}
# S# P  `7 z7 K: R1 l/ o$ R) S# Stop nginx daemons functions.
* k( \7 m0 @8 E, j/ Qstop() {
. {: i7 J9 a  P* L: M) Zecho -n $"Stopping $prog: "
6 Y  m4 i, O1 \5 ~7 \( U8 G' Ykillproc $nginxd0 w' u7 A- U$ p2 ]9 ?
RETVAL=$?
* p0 N" D8 D9 N: ^3 iecho# A4 L  A: d. W0 T, w
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
* a! R0 q& [: W" }% S' P# ?0 H$ V}: b  |; K, O, T; O: A
reload() {
( K6 z7 s) D0 d, |0 v* mecho -n $"Reloading $prog: "2 c* h: L+ ]3 U
#kill -HUP `cat ${nginx_pid}`
) o2 _# z- n) }6 ikillproc $nginxd -HUP
3 [! ]6 w) ?7 x6 C4 c3 xRETVAL=$?
" t# y$ d5 {: `echo  G8 N) o- {' `% C% @
}
9 S. R# o$ E3 x1 q. {# @# See how we were called.
! Y# d6 i8 n& V/ Wcase "$1" in" y- e- b: Z, v: _, E8 T
start): h! z$ S' {( t. l3 S; U$ r
start
, q; f: m% y8 O( h% L. L, F;;5 j0 V+ k0 t0 {4 r, w* X' t
stop)
: h( ?3 e4 ~8 Sstop
  Y  @% }+ \% v5 j;;
/ k6 S7 _0 o% b' @: ^( |& zreload)
% q: }: T2 q3 t' S- jreload8 K! _; _/ J) E$ z' l, R
;;! Y5 }, D+ a* s  J( ^7 u
restart)
" q' b/ z$ g1 W3 X$ b  S$ }% cstop
9 |9 d) h) x1 p4 K( K3 A) p1 A5 U* Bstart- F* j6 Y! g2 z" Y
;;
) @- |7 Z  B/ X$ I/ L& C4 ustatus)
$ W% j* Y" H  f: R( I6 M# Qstatus $prog
1 Z, L4 I3 s# a5 YRETVAL=$?. G' O' c% F  U
;;
7 p' z- T; W- k/ y# A) A4 @*)
' \' K% d8 X4 H+ q2 lecho $"Usage: $prog {start|stop|restart|reload|status|help}"
, N# Q. D% E% p6 Cexit 1
0 |& v8 ]+ f9 h" fesac2 F) p' m. f  G; y- }' H( {
exit $RETVAL
+ _( ~/ D/ u& V8 G* D2 q' ~0 S. B7 c5 O6 D

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

1 q/ o3 {; R. {6 t" H' b( ^$ c4 b
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:; c6 R, X7 g+ H% b2 x. n& ]: I

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

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

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

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

! ~9 i& V/ U# ^) }9 \0 a
       location / {
" M6 H* N2 w4 W9 y- [* b) v            root   html;+ O. P4 O5 y5 M7 L
            index  index.php index.html index.htm;# F6 d2 a: d' {6 i  j0 v
        }, Y' R, V/ P$ b  C
修改为:; S7 B8 w3 w; s& M; i$ ^9 c

  L' S  K$ j$ q$ g5 c( q5 U% j0 m& h

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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

) L9 I+ c& I# g9 ~
location ~ \.php$ {
$ U: W# T$ A. E7 M3 [' J) |; b            root           html;
7 Y- C8 L' k1 o            fastcgi_pass   127.0.0.1:9000;
( t4 @% K& b$ ?/ c) n            fastcgi_index  index.php;
4 u4 Y: Z8 i2 n. R, Q            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
7 S# F4 b) B6 n) c6 H' R% _) |            include        fastcgi_params;! U6 b3 `6 ~/ ]1 D
        }. p& `) ]$ Q/ K; w: H5 `' C# S
修改为:
9 e5 r" L, B3 u  H1 I7 m: b9 I
# Y" k* `9 S2 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

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

1 a9 @% j, g5 B/ g- J) W; g) 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
9 ?$ e7 {8 F# ^  m' g% K
8 M( o1 L: \6 \. \. K7 m0 K+ z& ?8 ?#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。: C) r* L9 Y1 {

执行

cat /etc/passwd 查看用户列表* v: n1 k! p1 Z) B0 @+ J4 T
cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。; l% T* k9 }" F% h) _

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
2 Z! P$ }3 l& a! G

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql
. x/ O/ L+ `4 |2 Rchkconfig mysql on
" o+ k# @( ]6 M! Y! J/ T- \& Yservice mysql start  --启动MySQL2 [6 u8 s3 c5 f

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
2 z7 A, i; E* J4 t+ Z) e+ ]export PATH
) C: s4 E5 ~/ h! S

保存后source /etc/profile

执行:

! P- Z; z# K; r" q& z5 n
mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');3 K) M: k7 D) w) e$ ~: [  H
  l7 k6 G. j$ V7 t

设置root用户的密码为root。

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

( {, ^# I$ j- J1 ]2 s

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:


8 C% Q% A: ]) i" u$ J$ c

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


+ M9 ~9 D" h$ s

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
& \( |3 ~) F3 U: d7 d
# {+ Q5 O$ S+ y# y! [+ [  ]rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
/ }6 p3 I1 I) s( o# H: P1 E- @Create /etc/yum.repos.d/php-fpm.repo file and add:  E7 l8 _& X2 H! R! C& y
  1. [nginx]
    1 P) E0 O4 |. U; S6 S, S  k
  2. name=nginx repo9 ~3 p& S: B, b4 [5 Z
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    6 D) W/ e# @8 ?
  4. gpgcheck=0
    : N7 G+ x0 z" X+ J
  5. enabled=1
複製代碼

+ C4 v# @$ s. I& vyum --enablerepo=remi,remi-test install php-fpm: d/ L3 C0 S' E1 A

4 m: Q" w+ w5 M  j4 Z+ l0 j

0 V( t& w% E7 N, ]$ f五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn, C+ x# W+ x% N

#yum remove subversion //卸载低版本的svn8 `. W5 E. _# n

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn
: m( V/ g% s9 v

通过# 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]) o& e! X0 w2 ^% n. o
# harry = harryssecret6 q9 n- @3 g* n! \1 G) ]' o1 {
# sally = sallyssecret8 x( o8 A1 Q( g6 r- G5 i8 D+ `$ }4 @
wangning=wangning
- Q  i3 _' N7 l' X/ a6 qyanzi=yanzi2 w. Z- z% T1 k+ H1 f, z. _4 X$ g

#vim authz  //设置权限

* o) [) t* @+ F1 M2 W

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none
& f2 ?1 x2 d- x7 K0 Pauth-access = write
) y0 [: @' W! m* \" [. O### The password-db option controls the location of the password  [8 v' G0 j8 o, G
### database file.  Unless you specify a path starting with a /,# n9 _7 ~$ [: m# |1 B1 Z
### the file's location is relative to the directory containing
; H2 }: O/ A; ?3 O### this configuration file.2 Q5 J8 u% L' A6 m; M5 D
### If SASL is enabled (see below), this file will NOT be used.7 N: ]! |2 V3 m/ {
### Uncomment the line below to use the default password file." e, @. @) m' J' z
password-db = passwd- {: m" B* f5 j0 w, M
### The authz-db option controls the location of the authorization6 W5 H0 A( M* n5 `* H8 t5 @. W
### rules for path-based access control.  Unless you specify a path" d1 D3 \% `8 b" t0 a6 s2 ^
### starting with a /, the file's location is relative to the the5 Y) }! u0 t$ V6 y/ O- J
### directory containing this file.  If you don't specify an) m2 y. W6 A" n$ C5 J
### authz-db, no path-based access control is done.
) S; h7 b! h) Y+ N2 \### Uncomment the line below to use the default authorization file.
6 N; I6 M9 `  ^0 p0 o/ g3 c+ bauthz-db = authz; I: G4 z: v4 F* h4 T2 Z7 v
### This option specifies the authentication realm of the repository.
0 Q  }% d) O! J4 P7 \* `& L& q# R### If two repositories have the same authentication realm, they should
/ Q2 e/ _6 b7 H+ @+ ?8 p/ T4 S### have the same password database, and vice versa.  The default realm
% b" `: |, F& \### is repository's uuid.$ {) _  q3 u0 Y2 K) Z) c( R
realm = My First Repository
) h! T; y* |) I' O- ]" ?& [

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn
0 {+ [# R! R5 M7 y6 u

#killall svnserve    //停止, q: v* x- P0 C2 u# G/ C

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

svn checkout svn://192.1.15.222 --username xxx. s0 v: L& Z! I  v

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# y/ d8 x( z( A) \, \1 I4 W

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

vim post-commit,在里面输入:

- o0 |2 q  P5 _  N6 ^

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" :
3 O. m0 a) F. E3 m. \2 e

#system-config-securitylevel

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

% v; g- \$ j6 y  T( I- @
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled


! Y/ G  {, O) i; Wvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題1 q. }% D% E+ G- y% |7 g
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則



中文酷站排行榜

本區塊內容依據『電腦網路內容分級處理辦法』為限制級網站,限定年滿18歲以上或達當地國家法定年齡人士方可進入,且願接受本站各項條款,未滿18歲 謝絕進入瀏覽。為防範未滿18歲之未成年網友瀏覽網路上限制級內容的圖文資訊,建議您可進行網路內容分級組織ICRA分級服務的安裝與設定。 (為還給愛護 本站的網友一個純淨的論壇環境,本站設有管理員)

QQ|小黑屋|手機板| 52AV手機A片王

GMT+8, 2025-4-5 18:56 , Processed in 0.111855 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表