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

 找回密碼
 立即註冊
查看: 1523|回復: 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
. I" S0 i3 q2 I8 M: `( ~1 o1 L0 I) Z$ B  G6 {- q2 q

目标:搭建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+ I' ^6 h% b4 ^( ^1 ]+ L+ m4 @

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx
' p2 A; d5 D7 h8 D" o

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

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

执行:

/usr/sbin/groupadd -f www! X6 s, L& w: L7 A. v' D, m4 ~6 j
/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx
+ H& w* u5 \2 {6 g. K" s
$ g5 m: n- g0 y& J# U; tchkconfig --add nginx
! u0 B+ l- y# B" M& e1 q- x
0 y, w# C! c* a9 Schkconfig nginx on
. {3 o/ N7 ~  K; Q2 g: |

然后

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

#!/bin/bash
. l3 @( S6 f1 x* D+ t  J' W/ J# nginx Startup script for the Nginx HTTP Server2 N0 _1 W6 N) t$ p7 m8 S
# it is v.0.0.2 version.
5 j  f  g6 w2 ]3 N6 i3 j& |, b# chkconfig: - 85 15
( ?. _8 G4 e( F8 }' n3 m6 @8 o: ]# description: Nginx is a high-performance web and proxy server.; d2 d5 f! D' N! T# d1 O
# It has a lot of features, but it's not for everyone.( C4 w( I+ Y6 J
# processname: nginx3 z; z& j. f1 J: E
# pidfile: /var/run/nginx.pid7 [# }& x8 `& k/ s2 J
# config: /usr/local/nginx/conf/nginx.conf
( \! S; K$ Y! W$ u/ q4 v, x) Xnginxd=/www/nginx/sbin/nginx
5 O8 W6 e, ^# @  K" snginx_config=/www/nginx/conf/nginx.conf0 ^& H1 y$ ]& L: |
nginx_pid=/www/nginx/logs/nginx.pid0 P! z% d! E, M( X8 y3 j7 c
RETVAL=0
0 r& r5 q& y; j! w$ |0 W7 p/ `prog="nginx"3 r8 o% I4 W* i! b' Z1 N0 u4 M  T
# Source function library.
# x( b. |7 v0 A5 `8 X8 N+ W: u: z3 R6 R. /etc/rc.d/init.d/functions, l- D6 f+ G. L, @
# Source networking configuration.
0 R& \- _: M+ Q4 v: G  y& U. /etc/sysconfig/network
3 x  @- P- D0 p+ r/ I* l# Check that networking is up.- M; c: l. Y0 {* @% z/ {; W0 e
[ ${NETWORKING} = "no" ] && exit 00 k/ B  |# |/ ?0 ]
[ -x $nginxd ] || exit 0
# _8 o3 I1 b5 z! o1 C/ |, o! Z* H# Start nginx daemons functions.- ], h$ _% ?0 i
start() {- w9 d' P& U2 x2 ?9 h
if [ -e $nginx_pid ];then
+ {) T7 S4 A* K7 Wecho "nginx already running...."3 [& N$ U/ s. C7 q* j' j
exit 1* j( n0 ?8 L( [$ v6 H
fi& |% O2 P) W( Q+ V) a$ Q# X# v1 B
echo -n $"Starting $prog: "
* ]2 y, @" D/ p4 D- a- R2 _, c3 ?daemon $nginxd -c ${nginx_config}8 |/ n" e8 L4 T4 P; `$ O8 V) W4 X& O
RETVAL=$?
! E9 s! a! z5 l- e) _5 _echo; A, \; K& l' V& C3 d' M3 w
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
( \4 J9 b! P( rreturn $RETVAL" U: E, u! u4 ]
}
& G  k# V8 O# f% z# Stop nginx daemons functions.
4 t1 H" r$ a8 P# C- Bstop() {
  y" `; l; z- I8 Aecho -n $"Stopping $prog: ": c1 C4 A7 e) b; `( J
killproc $nginxd
6 x4 s0 f+ k) i5 zRETVAL=$?( h$ ~  t3 v! _4 f: ~+ x
echo3 w6 f; D7 n7 R
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid) r# {& R+ _& Z7 g" |/ Y! p  w- J  x; s
}; Q3 s. P- ^3 e9 e
reload() {
& T& i2 r0 ~8 V9 aecho -n $"Reloading $prog: "
2 @8 z: I2 g8 C. `  P#kill -HUP `cat ${nginx_pid}`
9 x& V* P/ b& Hkillproc $nginxd -HUP
" R/ ^! K& u: d! `RETVAL=$?
. k' t5 ~7 W8 @& \/ O# Becho4 h0 j& r% ^2 u  R' r7 I! E
}5 V% @( p; _: I* m" b8 G
# See how we were called.0 m3 J- B2 K& n4 W$ u9 O
case "$1" in
9 u- F! ]- r% I8 }start)
$ d, p! [) T+ c) ]" s+ K5 Mstart: E$ O$ h' i6 I2 a; z  y9 i: k( b
;;
, O: i0 f+ J( ^) f8 A6 estop)6 I4 p/ p: A. W. M
stop
# K: I5 Y. e; R;;7 x9 c/ W2 [; \& N! U
reload)
' \' C" s4 @5 y( _- Ireload1 l; b5 F2 m* h
;;
# m" E! ]" j! ]4 @% }+ H* Urestart)
: ~5 I6 y' N& z! {& e5 s! bstop# M' y  W. N  Z' D5 a- y) L! u; b0 Y
start. a0 h% D: f  ?9 ~
;;
) e* f0 ?7 f3 k/ Lstatus)" b" m7 o# F+ b5 s; D. f) S
status $prog) F$ V, c/ i7 t, c/ J) M
RETVAL=$?
/ E! ?8 t4 V$ z1 d- S) R- k;;6 |* ^. d( y+ T3 u, s" B
*)
9 j. T& `: f4 O2 [# X) [8 A2 becho $"Usage: $prog {start|stop|restart|reload|status|help}"
: V! X2 y8 e: P, L4 kexit 1
0 r2 {. O+ G( g, J3 C4 ^1 Lesac
6 _/ W# E0 \. ~/ y/ L1 F5 P: Rexit $RETVAL
$ }# V. A; {) k5 f8 ]6 t) }6 k3 T" ?- V7 w' h1 Q" @7 \6 z9 Q

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

) {# f; p; |3 \4 L" w# K. q
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:7 [; _+ N5 i( ^  m  ]2 N" H

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

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

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

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


$ A3 u; U$ L$ S' c$ s6 K; `       location / {0 D' B/ p8 o# v  P
            root   html;
  j( R* O; L( `& P& r9 w            index  index.php index.html index.htm;* y- {  d" L  U$ Y7 b7 x4 y+ h
        }
+ n( e9 F$ [9 S% E& D0 {修改为:" |) h0 }7 o; N
: r! M0 I, D4 B

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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


1 M5 n. i+ a. D. I& plocation ~ \.php$ {
. Z% ?& }# z+ V( ^9 D! H            root           html;9 ]8 ^- g- e7 _. J+ w
            fastcgi_pass   127.0.0.1:9000;
, k- s7 Z; e0 y- |            fastcgi_index  index.php;  X8 _8 v1 H. ~: F2 {$ q/ y
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;9 t. d7 ]2 W7 u: g
            include        fastcgi_params;
" f4 K6 n% o; b& t        }4 r7 x8 D9 j% `% T
修改为:
0 c1 R, @  V) ^8 K1 y% J: J7 ~" c, @# Y* a/ j: J; t

        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

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


. D# c1 F! P' _4 h* i( T) y

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
: `- _+ l6 o  E5 v) b- H1 A& i3 m, _' i! u8 x; V* G) G
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。0 ^) z' e; i- N" J3 s) |  h) L; y

执行

cat /etc/passwd 查看用户列表3 W* K  G% U/ T7 w# n0 l; G" g
cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。8 d0 D! p4 L* ~$ v6 J

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
8 X2 ]2 N! r. ^# T; ?: _- N0 Q- Y

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql0 j1 ^( i- v4 E8 h3 K
chkconfig mysql on% e  P; o2 ?5 r* s
service mysql start  --启动MySQL
) I% h8 l4 X' {8 \# `

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH8 n- j) G9 @; p$ Y
export PATH
! C. @; k0 H. p# j( h8 K; _6 X

保存后source /etc/profile

执行:


1 T, f9 k$ `8 |mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');
; o4 e2 L$ ^) s/ V0 w3 y# n* M

5 L% U- X! v  N

设置root用户的密码为root。

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


' u9 J8 I+ j. v

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:


5 H* X" V" f5 v1 u/ U1 P0 f8 [( P

#./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 O9 T! a: {" Z0 [6 |) o

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
1 J9 ~* ~% ~) W/ g, i7 p
$ H2 Q8 H, b- T9 Irpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm; c9 @( U0 E- U* b
Create /etc/yum.repos.d/php-fpm.repo file and add:  I8 s  }! U, r7 R5 f" C
  1. [nginx]
    * |' P+ u& e+ m4 X0 A, H0 I
  2. name=nginx repo' }3 A9 w! O, t2 [
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    ! D/ T. r: x) ]+ R. h5 V
  4. gpgcheck=00 {0 A5 ~/ @6 e) M: v
  5. enabled=1
複製代碼

! H+ w  l4 u& q- S3 U7 Vyum --enablerepo=remi,remi-test install php-fpm
- W! Z5 I; L( f
1 c0 {4 p/ c; I

% \& b! ~5 p7 v- f/ K五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn
# r( a" Q0 R2 h; X+ `! G  ^8 X

#yum remove subversion //卸载低版本的svn
2 ^$ F7 k! }, _

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn
: J# I0 [; P- ~% r* f$ ]" o( u

通过# 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]
* p. D  s1 ^; V* M# harry = harryssecret
) Q* E# m$ M( A' |# sally = sallyssecret
, w0 R$ w+ X( r- gwangning=wangning3 _3 C+ F5 \% K& M
yanzi=yanzi. I, I( T6 t. L0 i2 Q

#vim authz  //设置权限

# R) @' x1 U8 L+ P* J, N

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none3 c* ]. t* _% }: K2 x$ m2 b, \. l' _
auth-access = write7 M) Y% a$ k9 F; I
### The password-db option controls the location of the password
  J( G+ d$ ^; }: B7 w" ?### database file.  Unless you specify a path starting with a /,
* H7 K$ P& ^" ?$ u2 h+ z) i/ J. |### the file's location is relative to the directory containing
+ Y' o0 E( k) K! L### this configuration file.
4 ~) F& Z0 H. A7 @! K8 ~: Z, F### If SASL is enabled (see below), this file will NOT be used.
1 Y# p# G" {7 b### Uncomment the line below to use the default password file.! L2 e$ s' G  L3 U/ _# P+ J) d
password-db = passwd
+ l) P0 c: y. T9 {& O1 u& N* ]  M### The authz-db option controls the location of the authorization$ }3 X8 n  y2 q* {5 H
### rules for path-based access control.  Unless you specify a path
/ I9 }+ z. H: V' Z. ?, J### starting with a /, the file's location is relative to the the
0 k. B# ~8 Y% m( V### directory containing this file.  If you don't specify an
: f- ?: M+ F. U% H+ T; s/ q### authz-db, no path-based access control is done./ w5 A8 U5 N9 P0 g6 |
### Uncomment the line below to use the default authorization file.; G0 [5 R4 i! {  P$ T
authz-db = authz
3 E$ r7 b0 }% |8 h### This option specifies the authentication realm of the repository.
9 G9 X( x# j1 v/ X- Y# q### If two repositories have the same authentication realm, they should
# p  P/ R! V; G/ a7 A/ W5 u7 u### have the same password database, and vice versa.  The default realm" B! D+ w7 E, @
### is repository's uuid.: K. U% g2 `- r
realm = My First Repository5 m/ @- A: v* R$ j

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn
4 T1 h' V  Z6 [  X1 G

#killall svnserve    //停止
1 e, l1 T6 ?- [( Z& E; E+ o

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

svn checkout svn://192.1.15.222 --username xxx$ d* n) _3 L; A' T

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
1 p' }  d9 M+ S$ F% p, R

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

vim post-commit,在里面输入:

- K9 t" ^+ z  J! D

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 {6 ?' _- I. i. s* H

#system-config-securitylevel

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

2 U1 h) B  \) q7 z0 n7 K
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled

, E9 j6 u2 k" g# @8 J  @) S7 A
vi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
" o, B2 s5 q% C. @3 X/ W
回復

使用道具 舉報

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

本版積分規則



中文酷站排行榜

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

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

GMT+8, 2025-12-6 18:27 , Processed in 0.077588 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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