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

 找回密碼
 立即註冊
查看: 1573|回復: 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/491236591 k" l5 o" u6 m7 C
' J( q/ ^3 w5 q, e* L) Q( a; 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
" m0 W  n! r# @4 W0 W/ w( P8 G' C

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx6 d* G7 m  ]# g2 C1 f1 J

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

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

执行:

/usr/sbin/groupadd -f www
) Z$ l6 M1 D2 J6 g* 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
8 b& J! W7 F& V& O. [7 t% y3 X6 T
: `0 A( V3 Q; X9 ~7 n' r) l9 Wchkconfig --add nginx
$ u0 V3 |6 j1 W
+ n/ k* p! ~$ {& \2 U9 ]; c$ ]chkconfig nginx on
! @" }, G; Z6 M" \0 }$ `6 H- D

然后

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

#!/bin/bash4 y0 C& u5 p% b; a/ k) v
# nginx Startup script for the Nginx HTTP Server
* x+ ]8 O$ Q  _- b; q' C# it is v.0.0.2 version.
: @/ M0 Q, V/ H" Y; w' Q# chkconfig: - 85 150 t$ q- f( e( w  l5 M
# description: Nginx is a high-performance web and proxy server.
* h# `( C) m* w  e# It has a lot of features, but it's not for everyone.
3 L% z% P8 H/ v; o0 B/ q# processname: nginx
% \  ]! M, L4 j$ G% y8 t# pidfile: /var/run/nginx.pid  a! N; A: {7 j% R
# config: /usr/local/nginx/conf/nginx.conf
, ^0 o. @4 Q; v4 C% |- {8 Tnginxd=/www/nginx/sbin/nginx) c6 ^8 E9 v5 A- _
nginx_config=/www/nginx/conf/nginx.conf
5 \' K8 u' A+ r  E+ a/ u2 Tnginx_pid=/www/nginx/logs/nginx.pid
7 u/ q. O) d" r6 ?RETVAL=0
5 G) [1 ]3 N% d( A$ R+ Y3 ]prog="nginx"
! I7 ?- g/ ]! k$ y- F: p& I# Source function library.
9 F/ d7 S' v" X2 M0 x& ?' d. /etc/rc.d/init.d/functions( L8 l% R) _2 E6 ]" D
# Source networking configuration.$ d% w; u) b  B1 w
. /etc/sysconfig/network
* n& _* t8 y) z2 Y: U% B3 ]9 B$ c- ?* `' f# Check that networking is up." |( h' `! k% W, I: ~5 Y
[ ${NETWORKING} = "no" ] && exit 05 }. |; C- T( N3 B/ c5 Y
[ -x $nginxd ] || exit 0
$ f. U2 t7 C+ C9 w" N- E$ y# Start nginx daemons functions.
9 O, `0 v1 a9 Y7 d# J9 ]$ V( lstart() {
+ O. A3 `$ N. B  Cif [ -e $nginx_pid ];then( o: A9 M  C( v2 y5 P4 H2 _
echo "nginx already running...."7 w1 T- B) y2 P1 n6 f
exit 1
( _6 o! `+ O) f& b7 ?fi
3 a; \+ j' l- F8 yecho -n $"Starting $prog: "
8 @$ f0 C7 G6 u8 p/ ?( M: O4 ndaemon $nginxd -c ${nginx_config}
& W5 V' U6 H0 l- N1 e4 URETVAL=$?& X8 b  Z- M$ H& g* ]; b& E3 S7 |
echo! d2 h( M7 I( |' p' K
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
; x% Z* F' Q: U( M4 S5 treturn $RETVAL
# c$ o, W# H. ~}
6 i/ A; }: i- @# Stop nginx daemons functions.4 L0 y& E7 _( a4 i) N- h0 R2 b7 J: O
stop() {: C0 W8 i! q# V/ t' x' u6 @
echo -n $"Stopping $prog: "
; F: \$ `* H4 ^- t& rkillproc $nginxd& @' K  s; y( b+ x% o6 V6 [+ F
RETVAL=$?/ s; ^. R+ _8 M5 k
echo  J) l3 g" Y, d. c( v
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid
6 f  P7 i1 @- v4 C3 o$ f}) k! q( {' x2 Z  H, J7 P7 M
reload() {
6 f/ l& o) }! @" \echo -n $"Reloading $prog: "- D$ J6 K. M: B% A" I' o. P: x- t. G
#kill -HUP `cat ${nginx_pid}`
1 ]1 v7 Y2 T: S4 Ekillproc $nginxd -HUP
4 O* [) ?. C) z" k  K& [RETVAL=$?- }: ]6 }: t( `$ j0 j
echo
% h$ g  R  g( H4 H. W}* ^" S. ?; q: h7 l
# See how we were called.
& ?5 B! T! N7 Z9 Y) R1 @( fcase "$1" in8 G( n0 |8 ^4 l0 F& k7 y
start)
2 F6 d3 U( D! h+ w8 X. Pstart
6 d( l& l+ Q' P. r+ @* P;;' ~4 x  T* J+ i$ p; l) `( J: m
stop)
+ R7 U9 Z" q3 wstop) i7 S: D( P+ C, ^- _, i8 R
;;
; o8 s4 u& k) J9 Jreload)
. u5 K; x* X* V4 P" Mreload. I6 k) |- m( c( @# p- d
;;
$ T) n. Y, f7 n( ?5 Zrestart)* e; x4 {7 a3 w
stop
3 L2 [; B0 N0 \0 r4 f6 R+ nstart& n+ [5 o( d9 P! Z3 F$ v) d
;;
+ F3 x& c4 l% {  \5 zstatus), S+ u# b) ]; F" a5 y" X: P
status $prog+ q! N1 Z+ c5 R0 b$ s
RETVAL=$?
& c) U' h' v' e2 H" Y6 M% ?: k;;7 I9 a9 a0 @" o9 m& t) y  P5 J
*)( Y9 \. H$ Q4 p- t
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
5 k+ `/ g1 V+ N4 A3 ~2 Vexit 1
8 Y5 U5 H+ E2 \( P2 v) H1 {esac4 v; A# M8 p! z( F/ p
exit $RETVAL: U( u! I  C4 G5 u) ?7 H8 e
( t, B( x& ]5 Q4 B# a

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

! N/ N2 A! K* d  {7 |5 A
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:$ }; g7 t# D, M+ _1 N; S

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

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

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

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


$ d- o# n( F( \% c+ p- v# B5 v       location / {
+ u- ?. m! d9 b' _6 u9 U6 T            root   html;6 _. O( j; Q+ M  W% N: U
            index  index.php index.html index.htm;
3 x  A' X1 k! _( z' g; n+ C( o        }
7 d! O9 g' Q3 `. A6 P修改为:) B. q( b; N. D$ z; ~6 L% u' w$ k  f
' _  q# {/ q, g* P" ?$ U3 ]# A

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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

& v, K, _# v1 u: l7 f
location ~ \.php$ {5 V3 ~2 S  F( t0 j3 g3 M4 X& i6 c
            root           html;
% E' p" [/ C5 J7 v" `6 q            fastcgi_pass   127.0.0.1:9000;
3 I3 Q+ I' z: Y            fastcgi_index  index.php;
. f1 C7 b! r: ]5 g* k1 l; a+ Y            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
3 T7 B% Z* |1 y# `5 l; Q; V            include        fastcgi_params;  G0 v6 A1 b8 n  m- ?
        }# N# W3 a* ~4 M' ~
修改为:! W2 t. @( A9 @2 a

9 S0 M4 h& q4 H4 S0 Z! c5 H) i$ K

        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

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

6 z% g: J, p7 d' e) d! Y) F: g6 P

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql+ s! P0 {( Q$ B' |0 ^

4 R/ `0 \* w6 M0 Y) v5 O5 O! ]#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。
. V2 `% q7 C9 o$ z" k1 p8 j

执行

cat /etc/passwd 查看用户列表' z6 R# w1 n. f
cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。
4 C) Y8 `, {# ~+ N' i5 E

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql; S1 K! z! f. b3 w7 c; U% i

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql
) X5 T$ j# {7 B* [chkconfig mysql on
/ c! R6 W4 m4 J5 c1 g" gservice mysql start  --启动MySQL
- y1 l4 @# ]4 {

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH8 r7 z8 G9 k" _- h' @# H
export PATH
( T, S. d: a' ~, `

保存后source /etc/profile

执行:

0 A' Z- u2 d/ s
mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');
3 p8 b+ U% I" T9 d; e# k! Q2 N- e

% Q7 \: i( f& ?1 v1 i3 e9 S- O2 e7 W

设置root用户的密码为root。

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

8 N& F" g5 I: P( b

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:

& m& h2 i8 P5 Z9 s- u; A3 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里:

! B, {' O! @$ k5 ]- T

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.rpm4 T# [7 ~+ x1 Q, l
9 b$ }- G  f: t2 ?/ c5 z
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm! u9 u5 r4 t# ]. Q( p9 T& v
Create /etc/yum.repos.d/php-fpm.repo file and add:
6 {8 R7 J( F' z% P# |
  1. [nginx]' D0 ^6 ~4 }; `" H$ Z: y
  2. name=nginx repo
    , O/ S/ A) c6 E
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/) D$ Z) y9 @  M- d8 i
  4. gpgcheck=05 g- E9 q2 Y7 }4 F1 r! g. p
  5. enabled=1
複製代碼
5 j: t' H5 N% ]1 R) m0 C
yum --enablerepo=remi,remi-test install php-fpm' X! e6 q: ~; B; [" ^' Y0 U

8 N4 o3 P+ H# S+ B( {5 E

: ^  q7 r. o- j) v8 Z6 N五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn" E2 [3 v/ ^& X

#yum remove subversion //卸载低版本的svn
+ V% u9 c0 f3 d2 ]8 ]6 F8 z, t1 i

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn" @+ I* ~: u( Y! v+ M  K6 W* s

通过# 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]
6 _: ~3 s! O' ^7 k5 s1 o& ^8 N# harry = harryssecret1 W8 {, m7 }: e; x( c
# sally = sallyssecret
" v2 p. T  S+ f9 T/ `$ R/ [- twangning=wangning
3 T4 Z/ [, e+ [4 P, }3 n) Myanzi=yanzi
$ X% N8 G5 ~; M. P9 `

#vim authz  //设置权限

4 a2 J6 O6 k9 H, F" n1 A: @& c5 [' F

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none
8 q( {- @0 h+ ]' z/ `, K8 Uauth-access = write
* g5 n: J  H' p% F### The password-db option controls the location of the password* C7 k! w2 z# l6 j" T2 D
### database file.  Unless you specify a path starting with a /,3 r. R! w: j1 w( Y
### the file's location is relative to the directory containing
) x! M% ^# ~% m. I& a+ r# a* Q6 ?### this configuration file.( |) ^5 n1 {) T3 Z0 H
### If SASL is enabled (see below), this file will NOT be used.. w% `- q8 S  Y4 d  o, _; J) A
### Uncomment the line below to use the default password file.% u: ^" H, R3 Y$ a7 {
password-db = passwd9 p6 R! c, F$ P) m/ `  O- w" p3 U
### The authz-db option controls the location of the authorization2 O* V7 h8 ]' c" F" E
### rules for path-based access control.  Unless you specify a path
% _+ B8 i$ N6 }+ r### starting with a /, the file's location is relative to the the
( ^3 V- _! J# }. d### directory containing this file.  If you don't specify an' K" y9 J4 Y* \7 y
### authz-db, no path-based access control is done.
+ i2 k8 F. |5 e2 v9 x, S### Uncomment the line below to use the default authorization file.
2 M. `  k* r! A- Sauthz-db = authz
: F" M* {$ O' K; `* {; d### This option specifies the authentication realm of the repository.
. `3 K4 A4 G  Y+ b2 k### If two repositories have the same authentication realm, they should
+ z4 f: ~* k. ]; ~6 T### have the same password database, and vice versa.  The default realm
; \- w0 R% d3 U" p& [7 o/ J, A### is repository's uuid.% O0 ~/ C. q: x/ l; Z( O
realm = My First Repository
* z) ~( X6 e* I! @7 F  V; H

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn! v, a% C& c" J& Q- N5 @. V& s

#killall svnserve    //停止: y. ~9 m7 G" F: ?

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

svn checkout svn://192.1.15.222 --username xxx
- `8 L. Y8 {0 D3 `' d2 r6 j

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& @7 H9 P" O4 J. U8 a; W

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

vim post-commit,在里面输入:

0 v' E: t7 V( D  }" n% I: p1 M- T0 r* B

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" :
$ m$ i/ U8 Z5 K6 G

#system-config-securitylevel

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

5 p2 i* V* ]5 z/ @/ |
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled

. ~# {3 s* K' ]  Y& @' i
vi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
; A& E/ E* b' ^. ~& a
回復

使用道具 舉報

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

本版積分規則



中文酷站排行榜

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

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

GMT+8, 2026-2-4 05:14 , Processed in 0.081920 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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