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

 找回密碼
 立即註冊
查看: 1574|回復: 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
: ^. z) g" W- ?$ |# Y$ A4 V. F+ G5 x7 T! G2 r1 X; O8 _$ C9 o1 Y) 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
% n5 Z* j" R) }- K

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx
0 @# p# x/ R3 r2 R' C( O: u, ]

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

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

执行:

/usr/sbin/groupadd -f www
. M- A8 x& N- b6 m/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx
1 w3 p& P, L5 e8 \+ g; _- ]" w+ p/ [3 T6 W
chkconfig --add nginx
* @# W3 a/ z2 L( i; R1 k5 W& L7 w
chkconfig nginx on3 i' j% k5 y5 S6 o/ n

然后

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

#!/bin/bash
' J' [7 Y9 }+ j  f4 x& o# nginx Startup script for the Nginx HTTP Server
9 Y9 N, c7 A4 X6 t- d0 ]# it is v.0.0.2 version.
9 \0 r1 X7 h" l# chkconfig: - 85 15
* O( }" D4 t/ b% B2 _# description: Nginx is a high-performance web and proxy server.
; r5 M  X& w  E9 `# It has a lot of features, but it's not for everyone.7 w5 R* ]/ H9 N+ @; C
# processname: nginx5 {  Y- O2 T/ P( h9 |/ U) P5 d* I
# pidfile: /var/run/nginx.pid
  O# u) U' T* A5 C# config: /usr/local/nginx/conf/nginx.conf
9 O8 F- p0 V" u- e3 O3 X) I. A' E: mnginxd=/www/nginx/sbin/nginx& Z1 m( B% W& H/ I
nginx_config=/www/nginx/conf/nginx.conf9 ^7 ?0 H' a1 c9 f. w4 o9 f+ @
nginx_pid=/www/nginx/logs/nginx.pid3 P' M8 r% v  g+ @
RETVAL=0
. X. M7 {5 u# Nprog="nginx"
# {! Q! M- ]  c- `, ~8 a; b# Source function library.4 q+ ]# X2 C( j  a# @, A: v6 P
. /etc/rc.d/init.d/functions6 z! a4 r. T$ v
# Source networking configuration.
* n; ~+ Y3 b: u( E+ H. /etc/sysconfig/network
* C: R. P, j" W" E* Q" F# Check that networking is up.
0 `8 @* y& T3 n: q# }[ ${NETWORKING} = "no" ] && exit 0
! ?% c5 A+ n% C/ Q[ -x $nginxd ] || exit 0
2 D* J- h  ?, @( z; g. q" {- p# Start nginx daemons functions.2 h3 M$ N7 D4 j; Y2 }$ S+ a  N
start() {
' ?, h/ k" K/ N7 x0 m% I* nif [ -e $nginx_pid ];then: \% z& Z6 V, v! O
echo "nginx already running...."/ W& q' A, `; x
exit 1
- ~& Z4 |0 j& Y4 B( X( \" Lfi, o1 J2 a8 w6 s- d: u: F
echo -n $"Starting $prog: "" ~/ |5 Q, T. [. A+ `
daemon $nginxd -c ${nginx_config}5 k" x" l4 j& J: j9 T0 `
RETVAL=$?4 S/ t( r, T/ u# ~! d
echo
" j! k# b$ O9 P: ~[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
* i3 U# t* P5 A7 m2 w% Xreturn $RETVAL
& g( Q, b1 Q6 t2 L}
+ h1 ^8 j1 Q' S* J1 U' @# Stop nginx daemons functions.2 X! u; Z3 A$ y" o$ G  K! K
stop() {
7 L) I5 x+ b  h9 V# o/ {echo -n $"Stopping $prog: "/ S9 |' A5 R' Y$ x  X
killproc $nginxd! e0 Y+ s. ]! H( n% O$ M/ {
RETVAL=$?! c7 I& |( \) `6 b' f% Y+ d! m
echo' }% h7 |/ Y1 Y2 J
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid3 O+ O7 z  s9 r7 m: Y( _
}
- n- g+ R2 B& ?* w9 n3 ~5 U- n& `: breload() {2 _2 l4 v% \0 ^: r: }
echo -n $"Reloading $prog: "
, b/ A6 D, Y1 q) d- O#kill -HUP `cat ${nginx_pid}`
+ ^' p& e# q0 X/ r! r  @5 I, \killproc $nginxd -HUP
7 l6 o1 b0 P9 U) |7 H. Q1 P" oRETVAL=$?
) w6 H3 q. H- u8 f0 G% e7 u' t3 mecho
; l; \( S, L7 k' K1 D( P}
  p" o2 }' u5 @: {, {- a# See how we were called.
: v. }$ j, H" |8 r% xcase "$1" in
* w+ g% Y- r1 n# k4 x, ]7 ^start)
# h; P$ `2 \0 c4 C& j. ?start
2 y- F+ `2 \) Y9 ]  i% {6 q;;
  |& L5 I! L- \$ m- K' Gstop), y  i# I% w) k! g  |( d. {
stop3 r; }* P# ^7 X/ [
;;* ?8 N* A) h" Y% I8 o/ Z( F
reload)7 E- }( T* V9 `  h8 c
reload3 V- e" J. l8 T8 e# _# l
;;
8 i4 ~/ y( y* m8 |: {restart)
+ t- S+ y. k6 C3 d+ b# g( Jstop: O, L# C" W1 n+ @
start
; t/ ]( t; Z6 i( p;;+ P0 h6 \" V# E3 v) d
status)) a' M! y, _5 i
status $prog
9 r3 ~# U8 m. ?RETVAL=$?
( y( N. ?' J. k1 d2 ~;;, u0 d6 q1 v: H3 S9 C: u9 Q9 J/ ^
*)
+ ]- `) R0 @3 B( M3 o% o+ eecho $"Usage: $prog {start|stop|restart|reload|status|help}"9 W4 n, h) \2 s) \* @& x
exit 15 R) [/ P( a( u( x/ q- G. M
esac; B6 U9 w6 [" I1 l& {
exit $RETVAL
2 y5 c; S% t7 u2 x4 d) V6 g/ D  x7 Q

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

5 c& E1 ]- k" L6 H
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:
, g; u; _' ?- E5 W) M% M

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

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

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

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

. |9 E" c0 S/ W5 M. F5 I( [' F/ Y
       location / {% L- b. m  p: O9 o% y  B2 z
            root   html;
' _7 H! F! f8 D( [            index  index.php index.html index.htm;1 q4 c( A9 X2 Q2 J
        }
9 O# Y# Q3 }# n2 s% F修改为:
# x8 v' n& N' w7 i/ l- N' |
, U" q" Z5 r1 a5 a

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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


, E, x& c/ C+ _! ]* |; \location ~ \.php$ {- k$ J7 g- D8 ~, e+ c4 @. b
            root           html;3 K% O3 f$ r$ m9 l6 z
            fastcgi_pass   127.0.0.1:9000;% w) b. o( q% p8 u  C" u+ _
            fastcgi_index  index.php;
+ B# d" H  O2 v            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
3 O4 V8 L9 x: V* u) f, i) d( d            include        fastcgi_params;3 j- ~( ^# w0 Z$ w. B3 f% O+ P$ q5 C
        }
+ p/ S# W) v; F; \# P- j修改为:
; p# X4 y  P6 Q: U: \
  b% h1 A' j: ?) J! H% J3 A

        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

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


4 K4 F! A5 u. {

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
0 z0 l: X9 u8 I, k, L4 H* ?2 H: W7 |: y$ O* I
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。. G% Y9 p2 B! h; W, B* ]" h

执行

cat /etc/passwd 查看用户列表  L4 w9 A7 R3 h4 C& F! D
cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。! T+ w- w7 g: n( i

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
7 v1 W  H1 r' e8 l

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql# g& E5 M9 w1 P3 V1 n$ e# |5 n; G
chkconfig mysql on. B" R  i; A0 ], y
service mysql start  --启动MySQL) _8 D- p5 E8 R

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
6 |3 t8 _2 v# Eexport PATH! B% w+ P  i, @5 }

保存后source /etc/profile

执行:

1 r8 ?7 F" W0 e% C
mysql -uroot  mysql> SET PASSWORD = PASSWORD('root');3 i/ G( s9 ]- H5 I; {7 x$ h; j2 F8 C

1 M2 `/ g8 j  E% h/ e. m

设置root用户的密码为root。

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

% q$ ]! H  r. E! y& ~0 v4 d" h

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:

" Y0 c) C/ F0 d, l- i# 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里:

" i3 z( h- d1 n# S# h) ]$ F

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
% {" o/ ]5 @* A* O0 l, ?/ k: z. Y/ Y% \8 `+ C" m
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
2 g5 ]' W& Z3 I7 K" e) N3 f( }Create /etc/yum.repos.d/php-fpm.repo file and add:% _6 Z$ U# {: R" R- R
  1. [nginx]
    1 j7 g9 A! w4 O' u
  2. name=nginx repo2 O, D$ p6 e& ?( S1 [4 U+ a
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    ; K/ S" d6 t# N2 S0 o* N
  4. gpgcheck=0
    8 A8 m' c; }; {
  5. enabled=1
複製代碼
5 ~3 B5 e  o: M2 |! `! `7 k& b% i
yum --enablerepo=remi,remi-test install php-fpm
8 Q2 P! B" w) t& Z: I/ o
  K, S0 w/ V4 ^
! \& `2 x8 O- h: \
五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn8 c: Z9 B( _8 n% B) E

#yum remove subversion //卸载低版本的svn
& y- Q! J( _- C3 B4 `1 i9 M5 j, A

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn5 u" I+ e% a! ~; F; `" O4 R

通过# 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]
# f" A5 M5 `0 [, I: m2 U" O! Y9 C# harry = harryssecret- `5 b& W" q; l: j
# sally = sallyssecret8 `) R  T5 Y, T0 K+ |6 u
wangning=wangning
# P* z8 f0 u' L4 z' zyanzi=yanzi
3 Z3 J; |6 ^# ]4 @$ U

#vim authz  //设置权限

/ e+ ?- u/ o3 S& V8 T0 K$ r: {

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none
6 V- B, l( l: O: o2 {+ U8 c+ Uauth-access = write: o5 Z4 m. V# s( Y8 k7 N1 w
### The password-db option controls the location of the password
" ^4 e* k4 U* }9 I$ S# b6 }6 r' e### database file.  Unless you specify a path starting with a /,
( M1 |: S8 l2 L2 R+ M1 C7 e### the file's location is relative to the directory containing) s( f8 \( Y$ f7 K4 ~
### this configuration file.
* ?% T2 }" V; w/ w' n& O### If SASL is enabled (see below), this file will NOT be used.
( f% ~/ R$ l5 F4 H4 \4 y9 `### Uncomment the line below to use the default password file.. P8 |4 @( h1 |7 S2 W  \
password-db = passwd+ i4 V5 \- O& I$ Q6 N
### The authz-db option controls the location of the authorization, R  F  l) @7 q# O! ?" {8 i% S
### rules for path-based access control.  Unless you specify a path
0 i0 B+ T4 U& x5 X, _. K### starting with a /, the file's location is relative to the the
- h, ^) a$ c4 c! d  G### directory containing this file.  If you don't specify an
  z4 [+ n/ C0 C+ C# b6 i: e* f1 _. Y### authz-db, no path-based access control is done.( c0 R3 s: v  Q
### Uncomment the line below to use the default authorization file.; U( t# @% H0 _
authz-db = authz) Z% i' w& j1 Y$ \* t$ t
### This option specifies the authentication realm of the repository.
! O* q, h! H1 Q5 [! }### If two repositories have the same authentication realm, they should
/ [9 ^& z* t& j### have the same password database, and vice versa.  The default realm# o# u4 J3 h6 U! X+ c1 c
### is repository's uuid.
" G3 c+ X, _# I5 Y1 d4 w- Hrealm = My First Repository
  L$ j$ `9 ~  W2 z) F/ f3 w

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn
1 J, O7 U- m4 v1 ^7 ~6 C) Q" p

#killall svnserve    //停止
$ d" C3 q# S( Z" }

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

svn checkout svn://192.1.15.222 --username xxx) G9 G- _: m# H  B' o

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/web1 a, S* G1 z- Y: _9 b

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

vim post-commit,在里面输入:

' A; n& B6 |. ^4 @

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" :& h, ^/ @0 ?' i0 [9 w/ l1 `; a

#system-config-securitylevel

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

# y6 r& S- v' g/ ]2 h
執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled


3 p& @; v. Q+ C$ Lvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題: m% \* A/ x% f! Q- ]( Q
回復

使用道具 舉報

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

本版積分規則



中文酷站排行榜

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

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

GMT+8, 2026-2-4 09:34 , Processed in 0.084351 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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