CentOS7安装LNMP+Mongodb环境

CentOS 7   MySQL 5.7.10   Mongo 3.2.1   Nginx 1.8.0   PHP 5.5.31  redis 3.0.6

创新互联主营榕城网站建设的网络公司,主营网站建设方案,成都App制作,榕城h5重庆小程序开发公司搭建,榕城网站营销推广欢迎榕城等地区企业咨询

====================================== CentOS config
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

yum install -y iptables-services
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 9312 -j ACCEPT

:wq! #保存退出
systemctl restart iptables.service
systemctl enable iptables.service

vi /etc/selinux/config  #关闭SELINUX
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加

:wq! #保存退出
setenforce 0 #使配置立即生效

# 修改时间,如果安装时改了时区则略过
date
timedatectl
timedatectl set-timezone Asia/Shanghai
date
hwclock --show
hwclock --set --date '2016-01-27 00:24:10'
hwclock --hctosys
clock -w
shutdown -r now

# 注意,以下每步wget的安装包都存放到/usr/local/src;因为部分用到的安装包需要翻出去下载,不能翻的可在网盘地址【http://pan.baidu.com/s/1boBJxd9】中下载。

============================================= cmake
yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel
yum -y install imake mysql-devel
yum -y install net-tools
yum -y install wget

cd /usr/local/src
wget http://www.cmake.org/files/v2.8/cmake-2.8.11.2.tar.gz
tar -zxvf cmake-2.8.11.2.tar.gz
cd cmake-2.8.11.2
./configure
make
make install

============================================= mysql
cd /usr/local/src
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.10.tar.gz
groupadd mysql #添加mysql组
useradd -g mysql mysql -s /bin/false #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
mkdir -p /home/mysql/data #创建MySQL数据库存放目录
chown -R mysql:mysql /home/mysql/data
mkdir -p /usr/local/mysql
tar -zxvf mysql-5.7.10.tar.gz
cd mysql-5.7.10
# 配置,解决CMake Error,如果已经下载boost则执行以下操作,否则将以下cmake的-DDOWNLOAD_BOOST=0变为1
mkdir /usr/local/boost
cp /usr/local/src/boost_1_59_0.tar.gz /usr/local/boost/boost_1_59_0.tar.gz
cd /usr/local/boost
tar -zxvf boost_1_59_0.tar.gz
cd /usr/local/src/mysql-5.7.10
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/home/mysql/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=0 -DWITH_BOOST=/usr/local/boost
make
make install
cd /usr/local/mysql
# 执行以下初始化命令前保证 /home/mysql/data 下无任何文件和目录!
bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data
mkdir -p /home/mysql/data/undolog  # undo存放空间,必须安装时设置innodb_undo_directory、innodb_undo_tablespaces,非必要用途
chown -R mysql:mysql /home/mysql/data/undolog
# 建立配置文件,如果需要主从复制或者集群,参见另篇博文的mysql部署
cp support-files/my-default.cnf /etc/my.cnf
vi /etc/my.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
net_buffer_length=4M
max_allowed_packet=300M
expire_logs_days=15

default_password_lifetime=0    #设为永不过期,默认360天后过期而安装时无提示,也太坑了

#undo(ibdata) para set on
innodb_undo_log_truncate=ON
innodb_undo_directory=/home/mysql/data/undolog
innodb_undo_tablespaces=4
innodb_max_undo_log_size=10G
[client]
#default-character-set=utf8    #此项最好不添加
:wq! #保存退出

cp support-files/mysql.server /etc/rc.d/init.d/mysqld  #复制启动脚本到init.d下,而且/etc/init.d与/etc/rc.d/init.d已经建立链接
chmod 755 /etc/init.d/mysqld
chkconfig mysqld on
vi /etc/rc.d/init.d/mysqld
basedir=/usr/local/mysql #MySQL程序安装路径
datadir=/home/mysql/data #MySQl数据库存放目录
:wq! #保存退出

systemctl start mysqld.service #启动 service mysqld start
vi /etc/profile #把mysql服务加入系统环境变量:在最后添加下面这一行
export PATH=$PATH:/usr/local/mysql/bin
:wq! #保存退出
source /etc/profile

# 下面这两行把myslq的库文件链接到系统默认的位置,这样你在编译类似PHP等软件时可以不用指定mysql的库文件地址。
ln -s /usr/local/mysql/lib /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql

mkdir /var/lib/mysql #创建目录
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock #添加软链接
mysql_secure_installation #(!坑人,不建议执行!)设置Mysql密码,根据提示按Y 回车输入2次密码,不要使用默认生成密码的方式,记不住

alter user 'root'@'localhost' identified by 'sa'; #或者按这个改默认密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'sa' WITH GRANT OPTION; #允许root远程登录,改密码为'sa'
flush privileges;

# 如果安装后修改data存放,则需——
service mysqld stop
mkdir /home/mysql/data  #mysql用户的目录中建立数据目录data
cd /data/mysql #原数据目录
cp -R * /home/mysql/data
chown -R mysql:mysql /home/mysql/data
vi /etc/rc.d/init.d/mysqld
datadir=/home/mysql/data
:wq!
service mysqld start

============================================= nginx
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz
wget http://www.openssl.org/source/openssl-1.0.1h.tar.gz
wget http://zlib.net/zlib-1.2.8.tar.gz
wget https://github.com/pagespeed/ngx_pagespeed/archive/v1.8.31.4-beta.tar.gz
wget https://dl.google.com/dl/page-speed/psol/1.8.31.4.tar.gz
wget http://nginx.org/download/nginx-1.8.0.tar.gz

tar -zxvf pcre-8.35.tar.gz
mkdir /usr/local/pcre
cd pcre-8.35
./configure --prefix=/usr/local/pcre
make
make install

tar -zxvf openssl-1.0.1h.tar.gz
mkdir /usr/local/openssl
cd openssl-1.0.1h
./config --prefix=/usr/local/openssl
make
make install
vi /etc/profile
export PATH=$PATH:/usr/local/openssl/bin
:wq!
source /etc/profile

tar -zxvf zlib-1.2.8.tar.gz
mkdir /usr/local/zlib
cd zlib-1.2.8
./configure --prefix=/usr/local/zlib
make
make install

tar -zxvf v1.8.31.4-beta.tar.gz
cp 1.8.31.4.tar.gz ./ngx_pagespeed-1.8.31.4-beta
cd ngx_pagespeed-1.8.31.4-beta
tar -xzvf 1.8.31.4.tar.gz

groupadd www
useradd -g www www -s /bin/false
tar -zxvf nginx-1.8.0.tar.gz
mkdir /usr/local/nginx
chown -R www:www /usr/local/nginx
cd nginx-1.8.0
./configure --prefix=/usr/local/nginx --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1h --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.35 --add-module=/usr/local/src/ngx_pagespeed-1.8.31.4-beta
#注意:--with-openssl --with-zlib --with-pcre --add-module 指向的是源码包解压的路径,而不是安装的路径,否则会报错
make
make install
/usr/local/nginx/sbin/nginx
vi /etc/rc.d/init.d/nginx   
###################### 启动脚本,添加以下内容
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# 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="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
sleep 1
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
:wq!

chmod 755 /etc/rc.d/init.d/nginx
chkconfig nginx on  #systemctl enable nginx , chkconfig --add nginx
vi /usr/local/nginx/conf/nginx.conf
worker_processes  1             #根据cpu内核数(more /proc/cpuinfo|grep "model name")调整worker进程数,1个内核对应1个worker进程
gzip  on;  #去掉注释  
client_max_body_size 50M;    #增加该项解除上传限制过小的问题
fastcgi_ignore_client_abort on; #放http段内,解决php fsockopen函数无效问题
client_header_buffer_size 16k;  #放http段内,解决php fsockopen函数无效问题
large_client_header_buffers 4 64k; #放http段内,解决php fsockopen函数无效问题
:wq!
/etc/rc.d/init.d/nginx restart

========================================== php
cd /usr/local/src
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
wget ftp://sunsite.unc.edu/pub/Linux/libs/graphics/t1lib-5.1.2.tar.gz
wget https://bitbucket.org/libgd/gd-libgd/downloads/libgd-2.1.0.tar.gz
wget https://webm.googlecode.com/files/libvpx-v1.3.0.tar.bz2
wget http://download.osgeo.org/libtiff/tiff-4.0.3.tar.gz
wget ftp://ftp.simplesystems.org/pub/png/src/libpng16/libpng-1.6.12.tar.gz
wget http://ring.u-toyama.ac.jp/archives/graphics/freetype/freetype2/freetype-2.5.3.tar.gz
wget http://www.ijg.org/files/jpegsrc.v9a.tar.gz
wget http://cn2.php.net/distributions/php-5.5.31.tar.gz
wget http://pecl.php.net/get/redis-2.2.4.tgz
wget http://pecl.php.net/get/mongo-1.6.12.tgz
wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz    #memcached服务依赖
wget http://www.memcached.org/files/memcached-1.4.25.tar.gz
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz     #memcached扩展依赖
wget http://pecl.php.net/get/memcached-2.2.0.tgz

tar -zxvf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure
make
make install

tar -zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
make install

tar xvf libvpx-v1.3.0.tar.bz2
mkdir /usr/local/libvpx
cd libvpx-v1.3.0
./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9
make
make install

tar -zxvf tiff-4.0.3.tar.gz
mkdir /usr/local/tiff
cd tiff-4.0.3
./configure --prefix=/usr/local/tiff --enable-shared
make
make install

tar -zxvf libpng-1.6.12.tar.gz
mkdir /usr/local/libpng
cd libpng-1.6.12
./configure --prefix=/usr/local/libpng --enable-shared
make
make install

tar -zxvf freetype-2.5.3.tar.gz
mkdir /usr/local/freetype
cd freetype-2.5.3
./configure --prefix=/usr/local/freetype --enable-shared
make
make install

tar -zxvf jpegsrc.v9a.tar.gz
mkdir /usr/local/jpeg
cd jpeg-9a
./configure --prefix=/usr/local/jpeg --enable-shared
make
make install

tar -zxvf libgd-2.1.0.tar.gz
mkdir /usr/local/libgd
cd libgd-2.1.0
./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/ --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx
make
make install

tar -zxvf t1lib-5.1.2.tar.gz
mkdir /usr/local/t1lib
cd t1lib-5.1.2
./configure --prefix=/usr/local/t1lib --enable-shared
make without_doc  #如果想用make则需先装latex:yum -y install tetex-latex
make install

\cp -frp /usr/lib64/libltdl.so*  /usr/lib/
\cp -frp /usr/lib64/libXpm.so* /usr/lib/
tar -zxvf php-5.5.31.tar.gz
cd php-5.5.31
export LD_LIBRARY_PATH=/usr/local/libgd/lib
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype
make  #如果出undefined reference to libiconv_open ,使用make ZEND_EXTRA_LIBS='-liconv'
make install

cp php.ini-production /usr/local/php/etc/php.ini  #复制php配置文件到安装目录
rm -rf /etc/php.ini  #删除系统自带配置文件
ln -s /usr/local/php/etc/php.ini /etc/php.ini   #添加软链接到 /etc目录
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  #拷贝模板文件为php-fpm配置文件
ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf  #添加软连接到 /etc目录
vi /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid #取消前面的分号
user = www #设置php-fpm运行账号为www
group = www #设置php-fpm运行组为www
:wq! #保存退出

cp /usr/local/src/php-5.5.31/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm   #拷贝php-fpm到启动目录
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig php-fpm on
vi /usr/local/php/etc/php.ini
#编辑配置文件,列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
找到:disable_functions =
修改为:disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkDNSrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
找到:;date.timezone = 修改为:date.timezone = PRC #设置时区
找到:expose_php = On 修改为:expose_php = Off #禁止显示php版本的信息
找到:short_open_tag = Off 修改为:short_open_tag = On #支持php短标签
找到:opcache.enable=0 修改为:opcache.enable=1 #php支持opcode缓存
找到:opcache.enable_cli=1 修改为:opcache.enable_cli=0 #php支持opcode缓存
在[opcache]添加:zend_extension=opcache.so #开启opcode缓存功能
:wq!

vi /usr/local/nginx/conf/nginx.conf    #配置nginx支持php, 做如下修改
user www www; #首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否则php运行出错
index index.html index.htm index.php; #添加index.php
取消FastCGI server部分location的注释,注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
:wq!

vi /etc/php-fpm.conf       # 根据内存(grep MemTotal /proc/meminfo | cut -f2 -d:)调整并发请求处理数,每个php-fpm进程占用20m左右物理内存;单独调高无用,需要对应调整spare_server的配置。
pm.max_children = 20
pm.start_servers = 8       # start_servers = min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.min_spare_servers = 4
pm.max_spare_servers = 12

#******* 安装扩展php-openssl
cd /usr/local/src/php-5.5.31/ext/openssl/
mv config0.m4 config.m4
/usr/local/php/bin/phpize
./configure --with-openssl --with-php-config=/usr/local/php/bin/php-config
make
make install
vi /usr/local/php/etc/php.ini
加入 extension="openssl.so"

#******* 安装扩展php-redis
tar -zxvf redis-2.2.4.tgz
cd redis-2.2.4
/usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519
 
如果出现:
Cannot find autoconf. Please check your autoconf installation and the  $PHP_AUTOCONF  environment variable is set correctly and then rerun this script.
用下面的方法解决:
# wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
# tar -zvxf m4-1.4.9.tar.gz
# cd m4-1.4.9/
# ./configure && make && make install
# cd ../
# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
# tar -zvxf autoconf-2.62.tar.gz
# cd autoconf-2.62/
# ./configure && make && make install
 
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
vi /usr/local/php/etc/php.ini
加入 extension="redis.so"

# 所有此般编译后的so都应出现在该路径/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/

#******* 安装扩展MongoDB database driver
tar -zxvf mongo-1.6.12.tgz
cd mongo-1.6.12
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
vi /usr/local/php/etc/php.ini
加入 extension="mongo.so"

#******* 安装memcache服务与memcached扩展
tar -zxvf libevent-2.0.22-stable.tar.gz
cd libevent-2.0.22-stable
./configure --prefix=/usr/local/libevent
make
make install
tar -zxvf memcached-1.4.25.tar.gz
mv memcached-1.4.25 memcached-svr-1.4.25
cd memcached-svr-1.4.25
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
make
make install
vi /etc/init.d/memcached
###################### 启动脚本,添加以下内容
#! /bin/bash  
#  
# memcached     start/stop the memcached daemon  
#  
# chkconfig: 35 80 70  
# description: memcached is a memory cache server.  
#

prog="memcached"  
exec=/usr/local/memcached/bin/memcached
lockfile=/var/lock/subsys/memcached  
 
# source function library.  
. /etc/rc.d/init.d/functions  
 
start() {  
    if [ $UID -ne 0 ]; then  
        echo "User has insufficient privilege."  
        exit 4  
    fi  
    [ -x $exec ] || exit 5  
    echo -n $"starting $prog: "  
    daemon $exec -u root -d -P /var/run/memcached.pid  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && touch $lockfile  
}  
 
stop() {  
    if [ $UID -ne 0 ]; then  
        echo "User has insufficient privilege."  
        exit 4  
    fi  
    echo -n $"Stopping $prog: "  
        if [ -n "`pidfileofproc $exec`" ]; then  
                killproc $exec  
 
        else  
                failure $"stopping $prog"  
        fi  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && rm -f $lockfile  
}  
 
restart() {  
    stop  
    start  
}  
 
rh_status() {  
    # run checks to determine if the service is running or use generic 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")  
        rh_status_q || exit 7  
        $1  
        ;;  
    "status")  
        rh_status  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart}"  
        exit 2  
        ;;  
esac  
exit $?
:wq!

chmod 755 /etc/init.d/memcached
chkconfig memcached on

tar -zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
./configure --prefix=/usr/local/libmemcached --with-memcached=/usr/local/memcached/bin/memcached
make
make install
tar -zxvf memcached-2.2.0.tgz
mv memcached-2.2.0 memcached-so-2.2.0
cd memcached-so-2.2.0
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
make
make install
vi /usr/local/php/etc/php.ini
加入 extension="memcached.so"

/etc/init.d/nginx restart #重启nginx
service php-fpm start #启动php-fpm

#******* 测试nginx+php
cd /usr/local/nginx/html/  #进入nginx默认网站根目录
rm -rf /usr/local/nginx/html/*  #删除默认测试页
vi index.php
phpinfo();
?>
:wq!

chown -R www.www /usr/local/nginx/html/
chmod 700 /usr/local/nginx/html/ -R

# 在浏览器中打开服务器IP地址,会看到php的界面

============================================== MongoDB
cd /usr/local/src
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.1.tgz
tar -zxvf mongodb-linux-x86_64-rhel70-3.2.1.tgz
mv /usr/local/src/mongodb-linux-x86_64-rhel70-3.2.1 /usr/local/mongodb  #运行目录

groupadd mongodb
useradd -g mongodb mongodb -s /bin/false
mkdir -p /home/mongodb/db  #数据目录
mkdir -p /home/mongodb/log  #日志目录
chown -R mongodb:mongodb /usr/local/mongodb
chown -R mongodb:mongodb /home/mongodb

vi /usr/local/mongodb/mongodb.conf
port=27017
pidfilepath=/usr/local/mongodb/mongo.pid
fork=true
directoryperdb=true
dbpath=/home/mongodb/db
logpath=/home/mongodb/log/mongodb.log
logappend=true
shardsvr=true
#auth=true  #开启认证
#bind_ip=192.168.1.18   # Listen to local interface only. Comment out to listen on all interfaces.
:wq!

vi /etc/rc.d/init.d/mongod   
############################# 启动脚本,添加以下内容
#!/bin/sh
# chkconfig: - 64 36
# description:mongodb 3.2.1
case $1 in
start)
/usr/local/mongodb/bin/mongod  --maxConns 20000  --config /usr/local/mongodb/mongodb.conf
;;
stop)
/usr/local/mongodb/bin/mongo 127.0.0.1:27017/admin --eval "db.shutdownServer()"
;;
status)
/usr/local/mongodb/bin/mongo 127.0.0.1:27017/admin --eval "db.stats()"
;;
esac
:wq!

chmod 755 /etc/rc.d/init.d/mongod
chkconfig mongod on
service  mongod  start

vi /etc/profile  #添加环境变量,在最后一行添加下面的代码
export PATH=$PATH:/usr/local/mongodb/bin
:wq!

source /etc/profile  #使配置立即生效

#启动MongoDB方式 1
/usr/local/mongodb/bin/mongod --port 27017 --fork --dbpath=/home/mongodb/db/ --logpath=/home/mongodb/log/mongodb.log --logappend
netstat -lanp | grep "27017"  #查看MongoDB是否启动
cd /usr/local/mongodb/bin/
./mongo  #进入MongoDB数据库控制台
use admin  #进入admin数据库
db.shutdownServer()  #关闭MongoDB数据库
exit #退出
#启动MongoDB方式 2
mongo  #进入MongoDB控制台
show dbs #查看默认数据库
use admin  #切换到admin数据库
exit #退出MongoDB控制台
#启动MongoDB方式 3
cd /usr/local/mongodb/bin/
./mongod --config /usr/local/mongodb/mongodb.conf  #启动MongoDB
./mongo 127.0.0.1:27017/admin --eval "db.shutdownServer()"  #关闭MongoDB

#建立索引
>mongo db.table01.ensureIndex({"myid":1});

============================================== sphinx
cd /usr/local/src
wget http://sphinxsearch.com/files/sphinx-2.2.8-release.tar.gz
tar -zxvf sphinx-2.2.8-release.tar.gz
mv /usr/local/src/sphinx-2.2.8-release /usr/local/sphinx
cd /usr/local/sphinx
./configure --prefix=/usr/local/sphinx  –-with-mysql
# 出现下面的就可以进行下一步:
configuration done
-------------------
You can now run 'make install' to ...

make && make install    #安装结束,到/usr/local 目录看是否有刚才指定的Sphinx目录,有安装成功

# 在make时如果出现undefined reference to libiconv的错误:
/usr/local/sphinx/src/sphinx.cpp:20060:undefined reference to `libiconv_open'
/usr/local/sphinx/src/sphinx.cpp:20078: undefined reference to `libiconv'
/usr/local/sphinx/src/sphinx.cpp:20084: undefined reference to `libiconv_close'
collect2: ld returned 1exit status
make[2]:***[indexer]Error1
make[2]:Leaving directory `/home/sphinx/src'
make[1]: *** [all] Error 2
make[1]: Leaving directory `/home/sphinx/src'
make: *** [all-recursive] Error 1

解决办法:打开configure文件,找到“#define USE_LIBICONV 1”,将注释去掉,并将1改成0。
执行:
#make clean
#make && make install

# 创建索引文件和存放目录
mkdir /home/sphinx
mkdir /home/sphinx/indexdelta   #创建增量索引存放目录
useradd sphxrun  #不使用root运行
chown sphxrun:sphxrun -R /home/sphinx
chown sphxrun:sphxrun -R /usr/local/sphinx/var
vi /usr/local/sphinx/etc/sphinx.conf   #修改对应索引的数据存放目录
:wq!

vi /etc/rc.d/init.d/sphinx
#################### 启动脚本,添加以下内容,【!!!注意!!!启动账户为新建的用户“sphxrun”】
#!/bin/sh  
# sphinx: Startup script for Sphinx search  
#  
# chkconfig: 345 86 14  
# description:  This is a daemon for high performance full text \  
#               search of MySQL and PostgreSQL databases. \  
#               See http://www.sphinxsearch.com/ for more info.  
#  
# processname: searchd  
# pidfile: $sphinxlocation/var/log/searchd.pid  
   
# Source function library.  
. /etc/rc.d/init.d/functions  
   
processname=searchd  
servicename=sphinx  
sphinxlocation=/usr/local/sphinx  
pidfile=$sphinxlocation/var/log/searchd.pid  
searchd=$sphinxlocation/bin/searchd
indexer=$sphinxlocation/bin/indexer
cfgfile=$sphinxlocation/etc/sphinx.conf
   
RETVAL=0  
   
PATH=$PATH:$sphinxlocation/bin  
   
start() {
    su - sphxrun -c "$searchd -c $cfgfile > /dev/null 2>&1"
    ret=$?  
    if [ $ret -eq 0 ]; then  
        action $"Starting Sphinx: " /bin/true  
    else  
        action $"Starting Sphinx: " /bin/false  
    fi  
    return $ret
}
stop() {  
    echo -n $"Stopping Sphinx daemon: "  
   
    su - sphxrun -c "$searchd --config $cfgfile --stop"
    #killproc -p $pidfile $servicename -TERM  
    RETVAL=$?  
    echo  
    if [ $RETVAL -eq 0 ]; then  
        rm -f /var/lock/subsys/$servicename  
        rm -f $pidfile  
    fi  
}
indexer(){  
    su - sphxrun -c "$indexer --config $cfgfile --all --rotate > /dev/null 2>&1"
    ret=$?  
    if [ $ret -eq 0 ]; then  
        action $"sphinx making index: " /bin/true  
    else  
        action $"sphinx making index: " /bin/false  
    fi  
    return $ret  
}  
   
# See how we were called.  
case "$1" in  
    start)  
        start
        ;;  
    stop)  
        stop  
        ;;  
    status)  
        status $processname  
        RETVAL=$?  
        ;;  
    restart)  
        stop  
        sleep 3  
        start
        sleep 3
        ;;  
    indexer)
        indexer
        ;;  
    condrestart)  
        if [ -f /var/lock/subsys/$servicename ]; then  
            stop  
            sleep 3  
            start
            sleep 3
        fi  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart|indexer|condrestart}"  
        ;;  
esac  
exit $RETVAL
:wq!

chmod 755 /etc/rc.d/init.d/sphinx
chkconfig sphinx on

# 如果sphinx文件太大,删除对应存放目录文件
cd /home/sphinx/
rm * -fr
mkdir indexdelta

#如果提示searchd.pid错误,则执行下两步
vi /usr/local/sphinx/var/log/searchd.pid
写入11770

============================================== jvm
yum search java #找到合适的版本,然后执行install
yum install -y java-1.8.0-openjdk-devel
java -version
vi    /etc/profile
export JAVA_HOME=/usr/lib/jvm/java-1.8.0  
export JRE_HOME=${JAVA_HOME}/jre  
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib  
export PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile

============================================== redis server
cd /usr/local/src
wget http://download.redis.io/releases/redis-3.0.6.tar.gz
tar -zxvf redis-3.0.6.tar.gz
cd redis-3.0.6
make MALLOC=libc  #如果不加参数,linux下会报错,没试过,反正是加上了参数编译的
make test
make install   #将可执行文件拷贝到/usr/local/bin目录
mkdir /home/redis   #数据data和日志log目录
./utils/install_server.sh   #问答式的配置如下,执行脚本使得Redis随系统启动
###################################################
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /home/redis/log/redis_6379.log
Data dir       : /home/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
###################################################
chkconfig --list   #检查是否开机启动
# 测试
redis-cli
127.0.0.1:6379> set foo bar
OK
127.0.0.1:6379> get foo
"bar"
src/redis-server &   #启动redis (后台开启服务器)
src/redis-cli shutdown   #关闭redis


分享题目:CentOS7安装LNMP+Mongodb环境
网站网址:http://myzitong.com/article/pihegs.html