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' @  Z$ Z5 Y0 M

9 m7 s+ r7 w! E- a# z9 m3 q8 g8 j

目标:搭建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 cmake2 ^9 Y4 K7 P+ _0 m6 @

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

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

可用 google search engine 搜尋最新版本

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

二,安装nginx

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

./configure --user=www --group=www --prefix=/www/nginx5 E# O2 v* t5 L" ~5 }0 w1 H. X" |0 C1 z

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

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

执行:

/usr/sbin/groupadd -f www
' P( A0 C8 f' I& S; k4 [; q1 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
6 l; B* a2 J$ r# a0 n; z# ~( e
1 b7 Z4 V& p$ m7 o7 pchkconfig --add nginx% b- O/ d$ G  U* ?
  ~3 W5 Z( X; ?
chkconfig nginx on
" i$ C: |9 l6 N6 i( M9 w" D

然后

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

#!/bin/bash* Q2 C, T. n  z/ x# `: {
# nginx Startup script for the Nginx HTTP Server
7 y& |& e) l( J; @5 G) _5 b# it is v.0.0.2 version.
* |' q! ~6 z+ n. k# chkconfig: - 85 15+ K/ x8 G* P& x; J
# description: Nginx is a high-performance web and proxy server.8 C: L, h5 ^0 }7 X8 e
# It has a lot of features, but it's not for everyone.
; W, a+ h4 f: m* [# processname: nginx
$ X6 K9 ~$ X8 {, V4 ]! ]3 e4 `# pidfile: /var/run/nginx.pid
. l/ H: e. E" s# config: /usr/local/nginx/conf/nginx.conf
4 V$ T* G# V. Z2 z9 ^5 wnginxd=/www/nginx/sbin/nginx
3 d' J6 K8 q4 A9 lnginx_config=/www/nginx/conf/nginx.conf- _9 k: b+ o1 X9 X
nginx_pid=/www/nginx/logs/nginx.pid1 u% Y! e8 m6 |/ G1 o! }
RETVAL=0
1 U" O# |" c; h5 x$ J+ E2 K' Mprog="nginx"1 U0 m" d2 _6 D% U0 B
# Source function library.
4 M- _& _* Z. I0 c- x' T. /etc/rc.d/init.d/functions' t6 X. G8 m' x' q
# Source networking configuration.
2 P+ y3 l# I/ E+ Z9 ^$ M* C. /etc/sysconfig/network
3 O2 L3 f2 _  N  |' v" H& ~# Check that networking is up.
5 f$ s( p. Q  {6 M& {: W[ ${NETWORKING} = "no" ] && exit 0
; r- ]0 C7 f! t' o. }[ -x $nginxd ] || exit 02 C$ L! T* g7 s9 R
# Start nginx daemons functions.
9 o2 M/ ]  U: s1 Ustart() {& w* c+ q$ K4 d' O, l. U2 O
if [ -e $nginx_pid ];then
/ Z+ ?. h+ }4 @# N* Cecho "nginx already running...."5 x# d* I7 a4 l" a' k' e+ e/ Y) ^) L
exit 1) a" T5 E( q  O' ?1 [8 j
fi
: G/ W) O1 j1 D' D1 R3 necho -n $"Starting $prog: "; I0 o2 e5 R2 O  {- T3 ?6 v- y# a
daemon $nginxd -c ${nginx_config}
$ N/ F+ C- Q2 FRETVAL=$?; j% `2 ]6 ^7 t7 C
echo, S6 D# N; w3 \% j  J+ [) u% P
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
5 u- W. Q$ y$ T0 C3 o7 `return $RETVAL3 i+ l9 |1 M; ?% w& i
}
* i8 M9 Z* d9 M: I+ }3 x# Stop nginx daemons functions.# `4 U0 Z* o; n1 z
stop() {
4 m, _' c( V$ f3 j) o+ Becho -n $"Stopping $prog: "
1 ~+ C% d' w1 y' \2 [, Ikillproc $nginxd
1 _7 O- c$ f7 n  G+ I# H# L; cRETVAL=$?4 {- `; h+ v) V% t4 Y
echo
0 ]' m5 m' L' c/ e" {[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /www/nginx/logs/nginx.pid+ f, q7 e( l1 c/ j
}/ F' c9 J* s7 I% P' A/ b
reload() {7 J- P! T( q! C
echo -n $"Reloading $prog: "
* h, b% C' D# w* ^# ?#kill -HUP `cat ${nginx_pid}`! j+ H& g) \4 T2 [
killproc $nginxd -HUP. q- ^& m7 c* v+ P
RETVAL=$?
  }2 n/ |) A4 K- t: K) Lecho
5 U+ E& Q! s3 U. J6 X6 K}
; o3 e: n7 w/ E# See how we were called.
% b$ ^8 i5 `& B) Zcase "$1" in
: g0 M7 G/ w' ^start)
  ^: y; I4 i- ]' K* k, jstart
/ V$ q' `1 K: a& Q  G+ F: I1 u;;
  B6 P1 M) f/ t0 m3 vstop), I* z* O/ _' O* ?, l1 Z1 [/ a$ J
stop5 r) _8 K9 q, F5 y8 X% ~. ?( H
;;. a) I  ?, `( t1 ^, V7 w4 V5 g
reload)
, b, i3 ^$ F3 M- wreload" Y' U' u- M3 Y) O& {" H. _
;;
7 Z9 ]0 d9 `( B5 m" n* erestart)
, I7 k, d( F; m( O7 Z; j0 ~4 Lstop: i( ^( P, A0 A/ R2 V
start
5 a$ G9 u. @8 G/ \' E/ p;;
# G8 i" z1 c8 K8 \8 Tstatus)9 S  `. P) w" J" `1 _
status $prog
* _; c$ F8 @6 {" \9 nRETVAL=$?: f. l  Z8 n. x1 C2 g) n/ L
;;9 _5 g$ Q4 r! Q. i. o. u0 \
*)
3 j- s$ B2 p$ b4 j1 x& s0 v; H# \echo $"Usage: $prog {start|stop|restart|reload|status|help}"
# u# H8 D0 q, {" Kexit 17 m$ i- ?4 t4 n$ ?. t
esac
2 _7 i1 J" n/ W1 Sexit $RETVAL/ \8 b# p0 `* d9 W5 E( y3 d9 |

" w: r5 s: ^1 p' r

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

, r+ L" O& `. m8 L' b/ L! O- S7 r0 c
chmod 775 /etc/rc.d/init.d/nginx #赋予执行权限chkconfig nginx on #设置开机启动/etc/rc.d/init.d/nginx restart 或 service nginx restart至此nginx安装就ok了,但遗留两个问题:" d9 |, y0 r2 `4 B

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

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

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

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


" M% p- `+ Z) u. j  a2 o  Z       location / {
7 p& r1 s- q: |( k- n5 x            root   html;
. j! s% @1 q& E, u7 Q. h- F( \            index  index.php index.html index.htm;  t0 m! @+ e  J7 y
        }
" V+ t9 g3 N' N6 Q修改为:
) h! U4 h5 {$ c  I/ c1 Q. c4 n9 ~; O

        location / {

            root   /www/web;

            index  index.html index.php;

        }

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

+ J- w* T4 Z5 G2 s2 W+ W
location ~ \.php$ {
) N8 m6 P; Y0 g" k, @% [# m5 S& U% S. e            root           html;
& P; D. {* _% A# Q3 r. u            fastcgi_pass   127.0.0.1:9000;) T  ?" y1 s% d: }8 M
            fastcgi_index  index.php;, Z3 _* r8 i' `1 \0 k8 k: g
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  D' t1 L3 \5 k6 A            include        fastcgi_params;
+ h8 ~3 Z+ _5 |! g        }
8 U8 ~7 ], D/ Q  z修改为:/ k6 B' C" I+ D5 h, c  \
: V8 b& g. z+ b' U/ ]

        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

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


7 i- {- v" j5 r) a7 O) Z

#cmake -DCMAKE_INSTALL_PREFIX=/www/mysql

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

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

my.cnf.backup

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

2,创建mysql用户和分组

#/usr/sbin/groupadd mysql
5 l& \# X# N  N% o0 g3 A0 P2 f; f. T9 u. U! Y
#/usr/sbin/useradd -g mysql mysql 增加mysql用户和分组。( ~# ~: ?$ |! [

执行

cat /etc/passwd 查看用户列表/ d' i6 q& P* Q9 ]# M
cat /etc/group  查看用户组列表

chown -R mysql:mysql /www/mysql修改mysql安装目录的权限。& ?, ^. }( B. R  f$ C' z

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

scripts/mysql_install_db --basedir=/www/mysql --datadir=/www/mysql/data --user=mysql
. `  H. U; w4 |9 w

4,添加服务,启动MySQL

cp support-files/mysql.server /etc/init.d/mysql
  t4 h: X5 M' C) y+ Schkconfig mysql on
9 p& q4 O' _- c, \service mysql start  --启动MySQL9 Y% |9 d8 X% B% s# q

5,设置root密码

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

添加:

PATH=/www/mysql/bin: $PATH
+ @( M: c$ |) g0 P) g! N- fexport PATH+ g) y2 b: G# o& a$ h$ z4 {+ p

保存后source /etc/profile

执行:


& l& Y) h+ I) n4 r, {% o3 Ymysql -uroot  mysql> SET PASSWORD = PASSWORD('root');0 @5 i# u( i% p2 p4 E# y6 r

4 L' o! U* X9 i6 V% U

设置root用户的密码为root。

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

3 P* {% N) i! l$ C

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

mysql> flush privileges; //更新权限

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

四,安装php(php-fpm)

解压并进入源码:

! y6 [7 b$ |, n% G, X

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

! C  u% ?1 ^7 u: e% E

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
" L$ }: C3 p  R7 h* F& B$ y' B5 c8 J
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm" R# I$ E1 h3 I
Create /etc/yum.repos.d/php-fpm.repo file and add:% e% o" i" B- N
  1. [nginx]# X" |7 F) V- z
  2. name=nginx repo
    4 h, u' P3 d7 w' G- ^
  3. baseurl=http://nginx.org/packages/centos/$releasever/$basearch/! [9 f5 I8 P/ o  l4 p
  4. gpgcheck=0$ W* v3 S9 ^4 M0 p7 l# h
  5. enabled=1
複製代碼

0 ]% Z* Y  T8 X% C' y6 nyum --enablerepo=remi,remi-test install php-fpm
/ R# l! {& Z% P3 I% ^7 y* n1 T* Y2 B' d

2 S; q! z1 `1 {1 V五,安装svn配置post-commit

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

# rpm -qa subversion  //检查是否自带了低版本的svn4 j$ o+ f# ]* M- U: R9 G) v7 h

#yum remove subversion //卸载低版本的svn
& v7 X% L0 j  r0 I

# yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql   //安装svn, o) d8 v9 H7 J& L1 ]  p2 e  t

通过# 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]
, j; ^' |" ?% v) h5 r, G! |# harry = harryssecret
" M( e  I% Y2 I0 l! a# sally = sallyssecret6 T& l& ?& V; N: s$ w+ G
wangning=wangning
# ^  v$ V) L+ T) [* zyanzi=yanzi) G$ |# N3 _4 i9 ?$ u( ~+ }7 t

#vim authz  //设置权限

6 N9 e3 ]! Y3 a1 I

[/]

wangning = rw

yanzi = rw

# &joe = r

# * =

#vim svnserve.conf

anon-access = none  y* z" r& l% P+ t/ s. U( n& h7 L
auth-access = write7 S) i7 @* v) E2 \+ z7 l
### The password-db option controls the location of the password
/ r+ F* F/ J" Y/ L- g### database file.  Unless you specify a path starting with a /,. i/ v7 ?2 r! |; ~) Y
### the file's location is relative to the directory containing# p8 R& z( ~! H2 V* w1 i, J
### this configuration file.# t1 f/ b) r" t1 [' B
### If SASL is enabled (see below), this file will NOT be used.5 C$ M+ ]5 u% o: \
### Uncomment the line below to use the default password file.
% R8 j. C/ s. P, i1 ~- o) Spassword-db = passwd
5 l. V6 c3 d/ w! M& A) q### The authz-db option controls the location of the authorization& \1 u4 f$ m- ?, w+ O  @. ?# H& g6 H8 ~
### rules for path-based access control.  Unless you specify a path
' S- e, W- Z0 Z/ P, h- z### starting with a /, the file's location is relative to the the
! p. n1 m) u5 I7 {9 F( o### directory containing this file.  If you don't specify an
" b- t: m' o  p* `) D* D. P### authz-db, no path-based access control is done.0 n/ I1 O2 f# L$ h) C, H, H- I
### Uncomment the line below to use the default authorization file./ k* |8 V* j5 N( n( Y
authz-db = authz
  N* }3 T1 L5 p0 o### This option specifies the authentication realm of the repository.
; G  U# x/ N& j  [( g### If two repositories have the same authentication realm, they should
- g6 Z$ o) w( @' N- ~! y### have the same password database, and vice versa.  The default realm7 ?6 r6 W1 U% T" p5 `8 f+ o
### is repository's uuid.$ x$ d2 y" x0 x4 t
realm = My First Repository2 O' b; Y7 [+ b5 |  g) t7 N

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

3,启动及停止svn

#svnserve -d -r /www/wwwwvn   //启动svn
" e; n* D! Z5 r% v

#killall svnserve    //停止
3 u0 i5 `! Q6 j  A' |1 c

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

svn checkout svn://192.1.15.222 --username xxx/ w) h7 d) ^. [( J4 p4 {

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' T/ o& d9 j" X' W: M

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

vim post-commit,在里面输入:

9 \; D. f/ D0 z8 h2 o7 F

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 t1 R' ~2 z4 b6 Y/ O6 u

#system-config-securitylevel

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


4 f, X# o9 g; V/ @執行setenforce 0就可以暫時關閉功能,執行setenforce 1就可以暫時打開功能

vi /etc/sysconfig/selinux

SELINUX=enforcing 改成--> SELINUX=disabled


% k# ~- F6 ^9 X, |3 bvi /var/log/audit/audit.log (或 tail -f /var/log/audit/audit.log) 查看 SElinux 有何問題
. k% U8 i* m4 y& p




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