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

標題: CentOS原始碼安裝搭建LNMP(包括nginx,mysql,php,svn) [打印本頁]

作者: IT_man    時間: 2015-10-31 15:13
標題: CentOS原始碼安裝搭建LNMP(包括nginx,mysql,php,svn)
參考自 : http://blog.csdn.net/yanzi1225627/article/details/49123659
- H4 o$ S+ o: X4 e, M7 K2 t7 W0 @) Q5 K3 p/ N' m& ~% b/ V

目标:搭建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( G) Z5 c0 A5 E6 s, Y0 X

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx$ F( m% ~# Z+ C  C" q; U/ r( d! [

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

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

执行:

/usr/sbin/groupadd -f www
, ]6 v7 B. q) W8 S0 a/usr/sbin/useradd M
-s /sbin/nologin -g www www   //-M 不建立使用者目錄

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

chmod 755 /etc/init.d/nginx
6 F+ P* k# X# S% E4 z& y; f( ^$ a* L+ o( T6 `( S: p4 p* K
chkconfig --add nginx; l& I- {& s; y( ?  T' J# \
2 o2 I' B! ]0 S5 m( k" ]) K
chkconfig nginx on/ j: c6 r: Z- q

然后

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

#!/bin/bash6 g, X+ k& p8 r% s5 z2 m
# nginx Startup script for the Nginx HTTP Server
- G# ^( P! w1 K' W0 K6 O# it is v.0.0.2 version.
8 f% T+ ?& o$ \* k, a+ r- u& j# chkconfig: - 85 15, u8 ]4 l/ E. f& e% N$ _
# description: Nginx is a high-performance web and proxy server.
0 z3 [$ `  d/ |- |# It has a lot of features, but it's not for everyone.
4 }. Z  A. H' Q# processname: nginx
' E: {/ T) I4 z  l* P# pidfile: /var/run/nginx.pid
) P) b& h& r9 Q+ l+ S# config: /usr/local/nginx/conf/nginx.conf
' a; E6 t% R( a, P+ `3 a. mnginxd=/www/nginx/sbin/nginx
) C) R5 X7 l) k" vnginx_config=/www/nginx/conf/nginx.conf
; |" z) c9 p1 [. M6 V  c6 Rnginx_pid=/www/nginx/logs/nginx.pid" p, j* b5 ~+ }' r$ Q3 n
RETVAL=0# L- X. F4 u1 R
prog="nginx"1 e6 \1 M& {7 E$ ]5 [% C0 f
# Source function library.
/ |5 a+ n9 |- v1 s1 u. /etc/rc.d/init.d/functions
6 _6 B4 H% j4 T2 m3 B# Source networking configuration.
1 O2 Z$ S) g+ t: M% Z( T- B. /etc/sysconfig/network
9 A! c4 J$ _" A# Check that networking is up.
# @* @  ]9 X! A! I[ ${NETWORKING} = "no" ] && exit 0
9 M- Q- x* m) {* c0 i3 K8 A[ -x $nginxd ] || exit 0& ]. O& P1 I4 m
# Start nginx daemons functions.$ F8 K! ?, o' ^& F- i0 t
start() {
* D, b0 c* ~1 N, o* `if [ -e $nginx_pid ];then, x) k' l" ]- c" ?4 ^, j
echo "nginx already running...."% J1 h  h5 Q4 D! O2 ^
exit 1; y% K7 F) l: M0 T
fi/ k& S) C+ O. V
echo -n $"Starting $prog: "/ @( g( Z& T: K
daemon $nginxd -c ${nginx_config}, }0 ~. t8 q8 e  u1 A" V
RETVAL=$?
5 C0 g4 A. V. I, t# Q& Z+ kecho
1 ?" o# l( @4 S. b[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx8 w" o: h2 }. l2 i- y
return $RETVAL+ x# H5 _/ |% j7 y1 Q* H& l# o3 |  A
}
+ T7 T1 }  K2 ^% F/ Q# Stop nginx daemons functions.3 ]; I9 T. n/ ?: v9 U1 u
stop() {
9 B8 \1 V; L2 u& w; Recho -n $"Stopping $prog: "
6 ]0 Z& L& _5 [3 v% y7 Ekillproc $nginxd7 Q. W2 F4 F; }, S% T+ W
RETVAL=$?
1 V( U6 o( M' h1 h4 t: t5 |; Jecho
: h. ^$ R4 E, D# V  ?[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid5 S7 Z! j8 i( e! ]7 w, _
}
- p' s% ]6 i9 i" m+ o2 zreload() {
9 y0 F/ G( O" {$ becho -n $"Reloading $prog: "
& M) b7 O5 x" v/ h#kill -HUP `cat ${nginx_pid}`
0 N% j4 b/ m4 s/ g5 P, Ykillproc $nginxd -HUP
2 c2 F' b* \6 T# V* URETVAL=$?
6 O7 P" L) f" T/ H, D* cecho8 H. m$ T% l5 [: I4 i
}
( V3 ]# ], U3 G5 b# See how we were called.
6 R5 ?1 [/ `* u7 E9 _& T; ccase "$1" in
' F2 |" e/ h8 G5 _8 Tstart)
, Z; U; B/ L' B* H- i( \- z; cstart4 x8 G( k# t, N/ o
;;
- M# r/ B. v& x7 Cstop)8 B! l1 U. q/ B) h" L. m, j5 t
stop
2 b' L( V" f- V) N;;) H& c0 {0 N5 S( D+ U. X* C/ L9 [
reload)
2 Z8 p% Y: ]+ j# qreload
! Q3 x7 j: ]$ |9 U;;
5 s% J8 E6 b+ c1 Vrestart)
0 s3 Q- |5 ]; L; R& Lstop
+ X' l+ H: Y7 b9 ^4 Jstart1 z: ~8 q; s/ J  g
;;
( R9 V0 M( f* Xstatus)
8 @# Q) u+ o4 wstatus $prog0 A3 G0 T; Q7 c7 T
RETVAL=$?5 o) h$ W& t" a" r& [) u( E) C2 }* k
;;
  }/ e# T4 \4 I/ |+ V8 L*)% W! |+ X% V/ Z- L" {/ g. ^, A; y
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
  V$ A; m; S6 ^' l# p: @4 O" sexit 1$ B; F+ A- Z# c+ C! J- o
esac
" U9 @8 p' c4 f2 y# Eexit $RETVAL  b8 t/ K- C0 U/ {9 P7 m7 t# m  @
. c' |" K) @7 ~

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


  q: b6 v. i$ Q" pchmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:# \7 H2 [$ d& j! Y& Z. F0 N) _

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

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

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

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

  d" b& a% E- w* ?+ R) s
       location / {
: ^% Z, H2 W& f& W' n( H8 T* T            root   html;
) ?3 l; W% Z1 B' C4 u# l            index  index.php index.html index.htm;
# [6 |/ o& q& M* H' Q+ X        }
+ d1 Y" G5 V% s3 B$ P修改为:
/ j7 P4 }1 j6 g$ \6 j  z$ A2 R2 K, x4 x1 g4 ^5 u

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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

$ h" G! E, F$ H" B. R/ [
location ~ \.php$ {
7 }% F: Y7 j7 A7 z8 ]0 Q# m            root           html;. ]7 W9 y0 W: U
            fastcgi_pass   127.0.0.1:9000;
3 k& P0 y/ [. W3 W. Z) v6 D; r            fastcgi_index  index.php;
& N0 B( u& o5 y: v; L            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
4 r9 D; c# Q% j3 N' H            include        fastcgi_params;/ c) ?! g" F3 h8 x# `
        }7 u3 o7 p; l. X5 z% q& C) x
修改为:
0 }  i# H. V' |' c0 v: q+ Q. E, D+ r& P  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

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


& @9 n" N/ ]0 o# s

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql# R/ y% s# r! o8 G
. s7 Q: k, d( x/ t4 A
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。
* [& }5 N: V8 v4 ^0 @6 \4 L

执行

cat /etc/passwd 查看用户列表' [0 U- \& C: I
cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。2 K" f, ]& X$ g7 S

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql2 U) g* K& Q1 w( c/ Y8 T  J) o9 s) w

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql
4 V' t$ i- j1 R( R( @  J9 B" z, Ochkconfig mysql on
; e' W- W5 b7 }+ Qservice mysql start  --启动MySQL$ e! l  I: y+ {

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
7 s; ^1 i& p# B" h  }4 Aexport PATH
' g* y) \4 T: a0 O) b  ~: B

保存后source /etc/profile

执行:


1 K, P( X8 f/ E7 c& k' l. z7 hmysql -uroot  mysql> SET PASSWORD = PASSWORD('root');
' X) @- p* H( r+ h: J6 b' i
  K& Y4 g6 k( L, @3 M. M

设置root用户的密码为root。

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

, x9 t6 e. @* Y7 `7 j

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:


& k4 k8 T/ r$ J8 |$ ^: ]4 f: s6 j

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

- d7 I. [- t; W, `

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' s0 r9 U0 e5 e" i0 }2 w' T
8 t5 s- V7 X, F0 \: m
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm8 E' H$ P# g& Y* @! M) k5 m
Create /etc/yum.repos.d/php-fpm.repo file and add:
' l, i8 Q! a. O% v7 J& A1 R
  1. [nginx]
    9 R' @( Z3 G0 M. j' ^6 _6 o" b
  2. name=nginx repo0 U9 \& ^  f  E& F& p% Q+ n
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    * `  u  t* j' |/ H4 A
  4. gpgcheck=0
    ' O# K) u% z' a7 H, f* ?
  5. enabled=1
複製代碼

# }( P4 q: v0 Fyum --enablerepo=remi,remi-test install php-fpm& B+ F) q; c3 W- c8 g7 ?6 s/ P5 S
! a5 z% V: s2 w- H- q# N+ f
0 H3 g* b  C/ {! `
五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn6 W' L. T+ Q, I6 U8 F/ ^

#yum remove subversion //卸载低版本的svn% x( v, k$ e. K

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn
0 |$ M$ R, h1 j- y

通过# 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]
1 Q+ t' h( M  s3 z0 H* T5 E; C# harry = harryssecret, g+ X/ ~* K1 i1 G% _; p
# sally = sallyssecret) `: q' |4 _: ~' H; t. P3 G0 u
wangning=wangning, F2 o1 I0 t' G
yanzi=yanzi. ^; ~. ~% `3 C7 k) E" s( C" I5 G

#vim authz  //设置权限

# G( n( ?, T/ i3 I; B

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none8 w4 n8 U, m) n8 e& A# W" }
auth-access = write0 f/ ?9 l  m! W8 a- X. C- I
### The password-db option controls the location of the password: @& G9 ]& Y1 c- Y1 H4 s( G
### database file.  Unless you specify a path starting with a /,: F. r- u. Q& x' c" }) k& \) F
### the file's location is relative to the directory containing
, q0 w# L1 P* _. N2 \% a% s### this configuration file.
& f) A. y5 x* b  Q% Y### If SASL is enabled (see below), this file will NOT be used.
# p2 r. D. Q6 {8 i/ K### Uncomment the line below to use the default password file.$ |5 M. _9 N: s/ q
password-db = passwd& P2 k9 O* ~0 P
### The authz-db option controls the location of the authorization& p2 h. Y4 x; x# z) L
### rules for path-based access control.  Unless you specify a path( t5 r; u) {0 B
### starting with a /, the file's location is relative to the the
0 q/ B0 u" q2 Y( v4 U- n$ O### directory containing this file.  If you don't specify an
# `6 F( @7 j# g9 t; q( ^( z5 ]### authz-db, no path-based access control is done.1 b7 S% \: v) S* T% V$ F
### Uncomment the line below to use the default authorization file.+ Q) x% \( @1 n. f& Z
authz-db = authz# r. [$ X! Z# I. g% v' C
### This option specifies the authentication realm of the repository.% R* \4 n  B+ R' y
### If two repositories have the same authentication realm, they should
9 Y& u! G* L7 v# s7 h* {### have the same password database, and vice versa.  The default realm' z' u  i7 _* E9 V
### is repository's uuid.
0 e  K6 t# v. m( Zrealm = My First Repository
9 H  f# R8 ?' K+ _; d* g- c

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn- z7 c, p! |& p9 o+ @# K% c: U

#killall svnserve    //停止
) f. \- r: m9 g2 f! M

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

svn checkout svn://192.1.15.222 --username xxx
# R' z4 Q% K2 S$ b

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
2 o( G  j) N+ v4 D8 J0 i* g6 h

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

vim post-commit,在里面输入:


& K/ x: n( P- C+ j! N

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" :
7 a, m' K" q- L# E& L* z

#system-config-securitylevel

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


* A( J2 E9 U( y7 L7 L) Q4 S+ V) W* Y: V  z# K執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled


- M  E( a' U$ b; |0 a5 O& V  fvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
- u5 B# f2 l5 w% |




歡迎光臨 52AV手機A片王|52AV.ONE (https://www.52av.one/) Powered by Discuz! X3.2