Centos6部署LNMP平台
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos6部署LNMP平台相关的知识,希望对你有一定的参考价值。
【本文档所介绍的内容适用于公司测试/生产常见的php,html等web环境部署,即常见的LAMP/LNMP环境部署】
一:LAMP环境部署前准备:
1.1相关软件以及系统
系统要求:Centos 6.0以上 (64位)
相关中间件:nginx: 1.6.0 以上 php:版本5.5 mysql-5.5 .25
1.2相关系统依赖包安装检查准备
1.2.1 检查系统自带httpd,mysql是否安装
# rpm -qa | grep httpd # rpm -qa | grep mysql # rpm -qa | grep nginx
如有安装,请使用以下命令卸载相关程序
# yum remove httpd mysql nginx
1.2.2 安装系统相关的编译环境,php扩展库支持,httpd,mysql编译需要的依赖包
[备注:大多数LAMP部署所用的相关扩展支持包可能都是用到官方下载的源码包.注意下:本篇所用的依赖包和扩展包都是都是基于系统自带的RPM包,比如安装httpd所用到的pcre;编译MySQL时用到cmake,用于支持PHP相关的功能扩展:gd,加密,压缩等PHP等等,都是基于RPM包安装,而并非利用源码包的形式支持相关的功能扩展,下一篇我们将讲到LAMP的另一个安装方式:即所有的需要用到包都是基于源码安装]
yum install wget gcc gcc-c++ make cmake bison bison-devel libaio-devel perl file patch mlocate flex diffutils readline-devel libcap-devel glibc-devel glib2-devel autoconf automake libgcrypt* libtool* openssl openssl-devel ncurses ncurses-devel libxml2 libxml2-devel libmcrypt* curl curl-devel zlib zlib-devel bzip2* gd gd-devel libjpeg libjpeg-devel libpng libpng-devel mcrypt freetype* gettext gettext-devel pcre pcre-devel
说明(以下只用于说明,不用于实际操作):
以下包是编译前必须要安装的依赖包
wget gcc gcc-c++ make cmake ncurses ncurses-devel bison bison-devel libaio-devel perl file patch mlocate flex diffutils readline-devel libcap-devel glibc-devel glib2-devel autoconf automake libgcrypt* libtool* pcre pcre-devel
下面说明是与php常见扩展相关的依赖包
openssl openssl-devel -用于编译php时添加 --with-openssl 选项; libxml2 libxml2-* -用于编译php时添加 --with-libxml-dir 选项; libmcrypt* -用于编译php时添加 --with-mcrypt 选项; curl curl-devel -用于编译php时添加 --with-curl 选项; zlib zlib-devel -用于编译php时添加 --with-zlib[-dir] 选项; bzip2* -用于编译php时添加 --with-bz2 选项; gd gd-devel -用于编译php时添加 --with-gd 选项; libjpeg libjpeg-devel -用于编译php时添加 --with-jpeg-dir 选项; libpng libpng-devel -用于编译php时添加 --with-png-dir 选项; libpng libpng-devel -用于编译php时添加 --with-zlib[-dir] 选项; libXpm libXpm-devel -用于编译php时添加 --with-xpm-dir 选项(可选); freetype* -用于编译php时添加 --with-freetype-dir 选项; gettext gettext-devel -用于编译php时添加 --with-gettext 选项; pcre pcre-devel -用于编译httpd时添加 --with-pcre 选项; perl -用于编译php时添加 --with-pear 选项; mcrypt -用于编译php时添加 --with-mhash 选项;
二:LAMP环境部署正式安装:
2.1编译安装Nginx
在正式编译httpd时,首先需要下载Nginx以及安装编译nginx需要的依赖包
这里版本以1.6.3为例
2.1.1安装编译nginx需要的依赖包(默认包放在/root目录下,包统一解压到/usr/local/src)
# yum install gcc openssl-devel pcre-devel zlib-devel -y
2.1.2下载nginx并添加运行nginx服务账号(默认包放在/root目录下,包统一解压到/usr/local/src)
# wget http://nginx.org/download/nginx-1.6.3.tar.gz # groupadd -r nginx # useradd -r -g nginx -s /bin/false -M nginx
2.1.3 编译安装nginx
# cd ~
# tar -zxf /root/nginx-1.6.3.tar.gz -C /usr/local/src
# cd /usr/local/src/nginx-1.6.3
# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nignx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/ \
--http-scgi-temp-path=/var/tmp/nginx/scgi/ \
--with-pcre
# make && make install
备注:
安装完后nginx的相关路径如下:
nginx启动命令:/usr/local/nginx/sbin
默认网页文件存放路径:/usr/local/nginx/
相关配置文件路径:/etc/nginx
访问,错误日志文件路径:/var/log/nginx
2.1.4 使用sed修改nginx的配置文件,指定nginx的运行用户
# sed ‘3 iuser nginx;‘ -i /etc/nginx/nginx.conf
注意:不修改nginx的运行用户的话,会报如下错误,如下图所示,
2.1.5 启动nginx服务
# /usr/local/nginx/sbin/nginx
注意:启动nginx可能会报以下错误,如下图所示,
这时需要按照提示创建相关目录,如下:
# mkdir -p /var/tmp/nginx
这时重新执行nginx命令就可以正常启动nginx服务了,如图所示
不过为了方便nginx启动关闭,一般常常将其做成服务启动脚本,代码如下
#!/bin/sh # # nginx - this script starts and stops the nginx daemin # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" lockfile=/var/lock/nginx.lock start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force- reload|configtest}" exit 2 esac
脚本建立完成后,给到它执行的权限并添加到开机自启动即可,命令如下
# chmod 777 /etc/init.d/nginx # chkconfig --add nginx # chkconfig --level 2345 nginx on
这时就可以利用系统自带的服务命令启动nginx服务,如下
下面就可访问nginx的主页面了,如下:
至此,nginx安装就完成了!
2.2编译安装php
2.3.1 到php官网下载php相对应的版本,这里我上传的是php-5.5.25.tar.gz
(默认包放在/root目录下,包统一解压到/usr/local/src)
# tar -zxvf php-5.5.25.tar.gz -C /usr/local/src/ # cd /usr/local/src/php-5.5.25/ # ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --with-zlib --with-libxml-dir --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-gd --with-mhash --enable-gd-native-ttf --with-iconv --with-png-dir --with-jpeg-dir --with-gettext --with-curl --with-pear --with-freetype-dir --enable-bcmath --enable-xml --enable-mbstring --enable-shmop --enable-soap --enable-sysvsem --enable-calendar --enable-zip --enable-ftp --enable-maintainer-zts --enable-fpm # make && make install
2.3.2 进入到php源码解压目录,拷贝相关样例模板到/etc下命名为php.ini,并编辑样例模板优化php相关性能
<---拷贝php开发样例模板配置文件---->
# cd /usr/local/src/php-5.5.25/ # cp /usr/local/src/php-5.5.25/php.ini-development /etc/php.ini
<---编辑php.ini----->
# vim /etc/php.ini
找到如下内容,并修改
# ;default_charset = "UTF-8" //取消掉注释符号";"将值设为"utf-8"; # engine = On //将此选项的开关为 on ; # short_open_tag = on //将此选项的开关为 on ,表示支持php标语; # ;date.timezone = Asia/Shanghai //取消掉注释符号";",将值设为"Asia/Shanghai"或"PRC";
2.3.3 进入php的安装目录下的etc目录,新建一个php-fpm.conf文件,如下
# vim /usr/local/php/etc/php-fpm 内容如下: [global] rlimit_files = 51150 [www] user = daemon group = daemon listen = 127.0.0.1:9000 ;listen = /dev/shm/php-fpm.sock listen.backlog = 65535 listen.owner = daemon listen.group = daemon listen.mode = 0660 pm = static pm.max_children = 30 pm.start_servers = 6 pm.min_spare_servers = 12 pm.max_spare_servers = 24 pm.max_requests = 10000
2.3.4 在系统服务目录下创建一个php启动服务脚本,如下
vim /etc/init.d/php-fpm #! /bin/sh ### BEGIN INIT INFO # Provides: php-fpm # Required-Start: $remote_fs $network # Required-Stop: $remote_fs $network # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts php-fpm # Description: starts the PHP FastCGI Process Manager daemon ### END INIT INFO prefix=/usr/local/php exec_prefix=${prefix} php_fpm_BIN=${exec_prefix}/sbin/php-fpm php_fpm_CONF=${prefix}/etc/php-fpm.conf php_fpm_PID=${prefix}/var/run/php-fpm.pid php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID" wait_for_pid () { try=0 while test $try -lt 35 ; do case "$1" in ‘created‘) if [ -f "$2" ] ; then try=‘‘ break fi ;; ‘removed‘) if [ ! -f "$2" ] ; then try=‘‘ break fi ;; esac echo -n . try=`expr $try + 1` sleep 1 done } case "$1" in start) echo -n "Starting php-fpm " $php_fpm_BIN --daemonize $php_opts if [ "$?" != 0 ] ; then echo " failed" exit 1 fi wait_for_pid created $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; stop) echo -n "Gracefully shutting down php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -QUIT `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed. Use force-quit" exit 1 else echo " done" fi ;; status) if [ ! -r $php_fpm_PID ] ; then echo "php-fpm is stopped" exit 0 fi PID=`cat $php_fpm_PID` if ps -p $PID | grep -q $PID; then echo "php-fpm (pid $PID) is running..." else echo "php-fpm dead but pid file exists" fi ;; force-quit) echo -n "Terminating php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -TERM `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; restart) $0 stop $0 start ;; reload) echo -n "Reload service php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -USR2 `cat $php_fpm_PID` echo " done" ;; *) echo "Usage: $0 {start|stop|force-quit|restart|reload|status}" exit 1 ;; esac
脚本建立完成后,给到它执行的权限并添加到开机自启动即可,命令如下
# chmod 777 /etc/init.d/php-fpm # chkconfig --add php-fpm # chkconfig --level 2345 php-fpm on
2.3.5 启动相关的服务,并查看端口侦听状态
# service php-fpm start # netstat -ntl | grep php-fpm
2.3编译安装mysql
2.3.1 mysql安装前准备(安装mysql前先需要添加相关的mysql运行账号及相关安装目录和权限)
<--添加mysql运行账号-->
# groupadd -r mysql //添加mysql服务组账号; # useradd -r -g mysql -s -M /sbin/nologin mysql //添加mysql服务服务账号,设置不登陆系统;
<--添加mysql程序相关目录-->
# mkdir -p /usr/local/mysql //创建mysql安装目录; # mkdir -p /usr/local/mysql/data //创建mysql数据存放目录;
2.3.2编译安装mysql(默认包放在/root目录下,包统一解压到/usr/local/src)
# tar -zxvf mysql-5.5.25.tar.gz -C /usr/local/src/ # cd /usr/local/src/mysql-5.5.25/ # cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 -DSYSCONFDIR=/etc -DWITH_SSL=yes # make && make install
2.3.3 给mysql相关目录设置属主权限
LAMP基础环境部署