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

 找回密碼
 立即註冊
查看: 1604|回復: 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/491236592 J$ {5 B3 ~( {" g

% F  H0 _8 z, o

目标:搭建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
9 Y& ^7 G* q$ H" z; S& _

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx
# `* y/ Q0 G5 v3 ?0 I1 P. o" k6 B+ t

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

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

执行:

/usr/sbin/groupadd -f www; J7 U% u1 m: s* e% 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- X4 s- V; l: Q2 Z

! r8 O: Z* v5 T, T' rchkconfig --add nginx8 w$ ^4 }+ B6 p& q" W% C6 j

$ X7 f0 T1 p6 |5 m, K: X4 qchkconfig nginx on
' A. x/ q; j- Y% r7 h

然后

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

#!/bin/bash  q2 H4 o8 d: p& z" J+ S! g, {
# nginx Startup script for the Nginx HTTP Server: d5 o' P. I3 }5 |, _
# it is v.0.0.2 version.
% k+ o. S2 i/ y/ q# chkconfig: - 85 15: p$ p+ M5 b$ H3 C& j
# description: Nginx is a high-performance web and proxy server.: c; x) c/ h. Z& k7 Q6 i+ h& u' M
# It has a lot of features, but it's not for everyone.
- n7 S7 {! C, \/ {5 K- \# processname: nginx
: z& E6 x& m3 [0 j$ y# pidfile: /var/run/nginx.pid
5 i- c2 g# q  N' x' ^# config: /usr/local/nginx/conf/nginx.conf) L( |+ I1 \" _% n7 v
nginxd=/www/nginx/sbin/nginx
9 l: i. p: ^- C6 Enginx_config=/www/nginx/conf/nginx.conf: T+ V$ M+ w- W. [4 \% G
nginx_pid=/www/nginx/logs/nginx.pid: j  |- h; _: J7 h2 ?4 J
RETVAL=0
4 u! _2 y) k0 E4 s" qprog="nginx"* \" z) s" P) x6 H- T
# Source function library., u' q5 j) V9 x$ \- f3 u
. /etc/rc.d/init.d/functions
! g, g  y8 H3 V& Y7 x) \; P# Source networking configuration.& @. H" m( }' J1 A
. /etc/sysconfig/network7 x" {; z( t- C* ~1 O  v% \) p1 U
# Check that networking is up.4 ?* V( p7 M& ^- p! i2 k
[ ${NETWORKING} = "no" ] && exit 0
4 U; c' B% }' z3 J4 ^[ -x $nginxd ] || exit 0& B; e; l( K; \% \, m" ]0 T) Y
# Start nginx daemons functions.6 p" m0 _) v3 h; |: B1 K
start() {
; P7 k* c; c7 k+ w1 @. J+ g4 qif [ -e $nginx_pid ];then6 {: }/ e9 L) }& j5 a  L( A8 y
echo "nginx already running...."
1 u) a% B# \* ]) I' `& Aexit 1! r0 b- E, M8 V, `/ C9 k
fi8 E- j: C3 y" E8 Q
echo -n $"Starting $prog: "! d/ L: H$ @+ m7 j; j! t
daemon $nginxd -c ${nginx_config}
* @3 s* G$ s% J& hRETVAL=$?
; c6 `1 `) u. Gecho
+ ]3 O6 T" n. p. K+ @" E' L- v[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
0 i: X" t2 X2 a$ m. ]return $RETVAL
. b& A' Z/ F8 x# {* ~+ ~}
6 d6 d9 `- B& s5 J% i# Stop nginx daemons functions.
/ X+ p' c6 P2 p5 _' @' t" L1 [0 pstop() {
! O/ p" R, [$ F1 Xecho -n $"Stopping $prog: "8 I% U6 x  W* w* O5 D4 ^
killproc $nginxd
. ^) U4 Z7 p" T: S* BRETVAL=$?
7 I9 x1 Q) T$ N4 e5 [5 B6 _echo
$ c# w& U! R( _[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid' ^0 s2 o" L  p" h- d: a
}
) q' u+ b' q4 s" I7 \  X+ b5 lreload() {
* @. x( s' ~) Y' K) Necho -n $"Reloading $prog: "9 e. R7 E) `3 M4 f
#kill -HUP `cat ${nginx_pid}`
. M5 {" ?( Q9 {, a7 Nkillproc $nginxd -HUP
* H$ I& J: L" j4 vRETVAL=$?
& P/ a9 P+ J5 ~7 q/ iecho
4 y( r% U2 ]6 S/ }0 j}5 n9 Q- ~7 ~& h9 x- z
# See how we were called.; M$ h% d- Q$ ?' p) S. \
case "$1" in
' q, u  B3 L2 fstart)) h1 E7 F2 @6 {% I, K  p
start4 E/ v$ F0 {9 m1 D4 C% h" _+ }
;;0 K* G/ j0 |6 ~, ~) Q7 H
stop)
7 c$ o: T9 G7 c  F# ?3 I: f! kstop
, K7 u4 E6 b  F; ?/ Q! \) q4 B- a) M3 @;;  f3 _( X7 [5 {. Q8 A. r; S5 d
reload)
, t! I" x& a; N1 G" |/ Greload* s; ~) K/ `3 g
;;1 W  g; b" r$ K' C
restart), ^$ c' R6 S4 L; p" l9 F" z$ \
stop
) \4 Q7 z. F4 y  {8 O+ [. [# U% X. k# Ystart7 U, f5 H8 [8 R9 m7 t
;;
6 n6 b" N$ G$ l2 Q1 v' \3 astatus)
" C- p6 t) Q- i7 e4 qstatus $prog; F# w/ d; {* j  }5 b
RETVAL=$?% i' S9 Q' e; D! D0 ^3 f" l/ w8 I
;;
8 k5 S* Q) L4 M6 c8 D! ?*)
: \5 w! x+ ~, w; u! vecho $"Usage: $prog {start|stop|restart|reload|status|help}"
" x/ k/ Z, Q+ j, a. v4 zexit 1
( Z# h: |" I) Aesac
: g9 r; @0 b1 Q# d' bexit $RETVAL% b. _2 o" r# m& _2 u# L
( ]: z/ x( A+ f$ n

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

% p9 D( i1 M+ S$ W8 a2 l( C
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:) E2 o* n/ I( r! D: }

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

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

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

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


3 D, x+ `, E# B1 e8 Y0 C/ a       location / {
/ c" @, m( }7 o            root   html;
# ^9 o( G7 W1 R4 R6 X" |+ h6 A            index  index.php index.html index.htm;
- ]) ?. _, t) E: U% ~4 @: a% Q        }
8 x" A  ~3 E% S, R8 u修改为:3 S: V* ]) `8 z; a( L# R3 h
3 V' G5 |; e9 U8 i2 \

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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

* O# L1 _1 Y( B; @  T" W& K3 C0 X
location ~ \.php$ {" y( E4 i- t4 S8 M! C% n- _. Q
            root           html;8 j2 X! k' O9 _# h# a% F
            fastcgi_pass   127.0.0.1:9000;4 C5 L% Q2 F3 Z# P0 T- s
            fastcgi_index  index.php;
8 R) ^, ]8 ^, D' q! q8 Q            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;4 p, z# A6 O4 z5 S% x
            include        fastcgi_params;
2 q9 j2 v' y" v' I! `        }) ?/ o6 J7 A0 ]1 x! D3 }' ], P" c
修改为:
( j/ f/ F8 a3 J, T" r! k" ~" R
9 p$ z( h( s% L- |1 r6 O5 \

        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

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


- r; @  S$ k- ^$ H, t/ _, m) \

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
2 }: z# n* {/ S7 m9 H3 s" l5 E+ [& |9 M
& R2 d( F% q& ?) l% O#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。
6 f' |4 ~- q' K8 l

执行

cat /etc/passwd 查看用户列表
( N1 ?' y+ [: U4 U, Dcat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。. ^% R$ f3 l; j- \

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
' r, ]! ]0 \4 T1 z0 q

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql/ r; R8 @$ r3 c2 @, g/ W
chkconfig mysql on# o9 j* u) U. p/ i; H
service mysql start  --启动MySQL# X2 n. w# U# e- ~- ]. q& s% m

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
2 g1 ^, X2 j  B/ t% d3 A! b7 yexport PATH
6 {5 x0 I1 {; O; e5 o, [

保存后source /etc/profile

执行:


( C+ ^* R/ o+ N% W4 Y* ~! Cmysql -uroot  mysql> SET PASSWORD = PASSWORD('root');& S* k+ {3 ?9 B# F: c. I

; f7 G$ \6 m/ {. u( t3 D

设置root用户的密码为root。

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

1 M; k+ n% \- a7 r( ?& Q  M

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:

$ E9 h4 a! [, C$ {. T2 R( E

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


2 \: H5 m/ M9 m  d8 {# a

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% V& ~* c% `; }+ P! V- b
( j; F$ M8 {: s3 ~: k9 e- a
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm* N8 q5 a+ j9 q8 n
Create /etc/yum.repos.d/php-fpm.repo file and add:! J& B4 h9 ]. S9 G
  1. [nginx]
    ) ~; U% `: Y7 \' g- J1 Z
  2. name=nginx repo
    ! h, K9 ?( p7 F$ ]- I- f& o, h
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/  _  ~" E8 s5 Y5 a3 m7 D1 @9 _
  4. gpgcheck=0
    . y8 P7 C; y% K2 Y# T, s8 y( R0 I7 c
  5. enabled=1
複製代碼
; X5 K, [# F, B" w8 L) P
yum --enablerepo=remi,remi-test install php-fpm* b: m0 \1 \6 x9 O- \6 K* L

7 O  J6 ^% O6 g. D# H

; Y: a  ~* V* s/ y( a" o5 `五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn
( r, L. q- _7 M$ n( q1 U; u; m1 |

#yum remove subversion //卸载低版本的svn" m; N( O2 {) n7 a

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn
6 G- ^9 i1 s1 u' x; b0 B

通过# 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]( S, ]. ]9 Z; h
# harry = harryssecret
& H5 R( r% L, w+ V) I( N& C# sally = sallyssecret# z$ @/ v* b) E3 B, T
wangning=wangning, p) y/ a( i+ n: l% U
yanzi=yanzi
+ W9 b" M# F9 [

#vim authz  //设置权限

  d% _1 u0 m5 T! Q2 T

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none7 a* t6 M; G  b5 e0 N  r
auth-access = write
7 t9 n& Z8 j. T) T* B5 ?2 d### The password-db option controls the location of the password3 L% M0 f9 i* U
### database file.  Unless you specify a path starting with a /,
; o# n$ `: h) k2 Q2 H# V### the file's location is relative to the directory containing9 J$ @2 |" e2 d: y/ D5 |
### this configuration file.
4 t( H9 C8 R* ]6 ?7 v* P) m### If SASL is enabled (see below), this file will NOT be used.
' T+ ~; K- B' A! C### Uncomment the line below to use the default password file.$ l6 J8 H" u( e3 f% W- I% H
password-db = passwd
1 ^$ s2 H5 m3 }0 e5 _### The authz-db option controls the location of the authorization* b4 T) O2 Q" C8 d* w& K" a8 G6 l
### rules for path-based access control.  Unless you specify a path7 S6 l0 z( ?: K9 }3 P
### starting with a /, the file's location is relative to the the( ]* X& Y- k, }6 C
### directory containing this file.  If you don't specify an( R( @& y% V% Q+ P$ M( w- o
### authz-db, no path-based access control is done.
: f! ~' V  o8 g- v! g  ^0 |### Uncomment the line below to use the default authorization file.2 I+ e' K+ p4 Y! q9 y+ X  d# Q
authz-db = authz2 q) s& ~8 d3 e
### This option specifies the authentication realm of the repository.
8 S& U6 i% q# p1 u2 L2 T- o### If two repositories have the same authentication realm, they should
& c# M4 k) _0 c### have the same password database, and vice versa.  The default realm
* r* u( h' H" ^3 E+ u7 G" G: |$ f. S### is repository's uuid.
1 z+ \/ Q/ D* A1 Jrealm = My First Repository' ]6 g4 f9 l9 j" p

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn
! M+ |2 J9 L+ S3 E7 o, u  Z

#killall svnserve    //停止
$ z" U( B7 ~4 G1 c8 I

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

svn checkout svn://192.1.15.222 --username xxx
3 M% d# i, u& S" \

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
. g. D& D; K: q( c

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

vim post-commit,在里面输入:


- ?  l; {+ 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" :
8 P# e5 b, X6 P1 o

#system-config-securitylevel

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


/ Q2 I. J  l  S6 t執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled


- i8 ~$ i" b: `0 [vi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
; a0 l8 Q, U, ]: Y6 r' c) V& M
回復

使用道具 舉報

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

本版積分規則



中文酷站排行榜

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

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

GMT+8, 2026-5-7 21:15 , Processed in 0.090138 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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