LNMP编译安装

Posted by 纸飞机 on August 1, 2018

1. LNMP编译安装

1.1. 安装依赖包

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel curl curl-devel e2fsprogs e2fsprogs-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers python-devel libicu-devel chrpath nspr-devel readline-devel ncurses-devel boost boost-devel pcre-devel js-devel git tcsh js js-devel libevent libevent-devel subversion git-core tcsh tcl net-snmp net-snmp-devel net-snmp-utils cmake postgresql-devel libxslt-devel

1.2. Nginx 1.11.13

1.2.1. 下载Nginx

wget http://nginx.org/download/nginx-1.11.13.tar.gz

1.2.2. 添加www用户组

/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www

1.2.3. 建立安装目录

mkdir -p /usr/local/webserver/nginx

1.2.4. 编译安装

./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre
make && make install

1.2.5. 修改Nginx配置文件

vi /usr/local/webserver/nginx/conf/fastcgi_params
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

vi /usr/local/webserver/nginx/conf/nginx.conf
worker_processes  4;
events {
    use epoll;
    worker_connections  8192;
}
gzip  on;

1.2.6. 添加service

vi /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/webserver/nginx/logs/nginx.pid
# config: /usr/local/webserver/nginx/conf/nginx.conf
nginxd=/usr/local/webserver/nginx/sbin/nginx
nginx_config=/usr/local/webserver/nginx/conf/nginx.conf
nginx_pid=/usr/local/webserver/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
   echo "nginx already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   return $RETVAL
}
# Stop nginx daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        killproc $nginxd
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
reload)
        reload
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac
exit $RETVAL
chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx --level 2345 on

1.3. Mysql 5.7

1.3.1. 下载Mysql

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.19.tar.gz

1.3.2. 创建mysql用户组

/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql

1.3.3. 创建目录

mkdir -p /usr/local/webserver/mysql

1.3.4. 编译安装

MYSQL5.7之后需要依赖Boost的C++扩展,而且只能是1.59.0版本,可以使用-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost自动下载Boost

或者wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz下载后解压到/usr/local/boost去掉-DDOWNLOAD_BOOST=1

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql -DMYSQL_UNIX_ADDR=/usr/local/webserver/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk,gb2312 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/usr/local/webserver/mysql/data -DMYSQL_USER=mysql -DWITH_DEBUG=0 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost

make && make install

chmod +w /usr/local/webserver/mysql
chown -R mysql:mysql /usr/local/webserver/mysql
cp support-files/my-default.cnf /etc/my.cnf
chown mysql:mysql /etc/my.cnf
cp /usr/local/webserver/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld 
chkconfig --level 345 mysqld on

1.3.5. 修改my.conf

vi /etc/my.conf
innodb_buffer_pool_size = 128M
basedir=/usr/local/webserver/mysql
datadir=/usr/local/webserver/mysql/data
port = 3306
server_id = 1
socket = /usr/local/webserver/mysql/mysql.sock

1.3.6. 初始化库文件

MySQL5.7.6之后使用-initialize初始化bin/mysqld --initialize --user=mysql

MySQL5.7.6之前只用mysql_install_db初始化bin/mysql_install_db --user=mysql

/usr/local/webserver/mysql/bin/mysqld --initialize --user=mysql

记下最后的密码

1.3.7. 启动mysql

service mysqld start

1.3.8. 修改密码

/usr/local/webserver/mysql/bin/mysql -uroot -p

输入初始化时的密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

1.4. PHP 5.6

1.4.1. 下载PHP5.6

wget http://cn.php.net/distributions/php-5.6.30.tar.gz

1.4.2. 安装依赖

1.4.2.1. libiconv

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make && make install

若遇到gets undeclared则注释掉warning

vi srclib/stdio.in.h
//_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");

1.4.2.2. libmcrypt

wget http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make && make install

1.4.2.3. mhash

wget http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make && make install

1.4.2.4. 链接库文件

ln -s /usr/local/lib/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.20 /usr/lib64/

echo /usr/local/webserver/mysql/lib >> /etc/ld.so.conf.d/mysql-x86_64.conf 
ldconfig -v

1.4.2.5. mcrypt

wget http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install

1.4.3. 创建目录

mkdir -p /usr/local/webserver/php 
mkdir -p /usr/local/webserver/php/etc

1.4.4. 编译安装

./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-iconv-dir=/usr/local/libiconv --with-pdo-mysql=mysqlnd --with-pdo-pgsql --with-pdo-sqlite --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-xmlreader --enable-xmlwriter --enable-soap --enable-calendar --with-curl --with-mcrypt --with-zlib --with-gd --with-pgsql --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --enable-exif --enable-bcmath --with-mhash --enable-zip --with-pcre-regex --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/mysql/bin/mysql_config --with-mysql-sock --enable-mysqlnd --with-jpeg-dir=/usr --with-png-dir=/usr --enable-gd-native-ttf --with-openssl --enable-ftp --with-kerberos --with-gettext --with-xmlrpc --with-xsl --enable-opcache --enable-fpm --with-fpm-user=www --with-fpm-group=www

make && make install

1.4.5. 修改php.ini

cp php.ini-production /usr/local/webserver/php/etc/php.ini

vi /usr/local/webserver/php/etc/php.ini
date.timezone = "Asia/shanghai"
extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20131226/"

1.4.6. 配置服务

cp /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf
mkdir /usr/local/webserver/php/log
vi /usr/local/webserver/php/etc/php-fpm.conf
pid = run/php-fpm.pid
listen = /tmp/php-cgi.sock
pm = static
pm.max_children = 200
pm.max_requests = 2000
request_slowlog_timeout = 3
slowlog = log/$pool.log.slow
listen.owner = www
listen.group = www


cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --level 2345  php-fpm on

使用unix-sock或者listen-9000端口, sock更稳定

1.4.7. 安装扩展

1.4.7.1. imagick

yum install ImageMagick-devel -y
wget http://pecl.php.net/get/imagick-3.4.3.tgz

tar zxvf imagick-3.4.3.tgz
cd imagick-3.4.3/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install

vi /usr/local/webserver/php/etc/php.ini
extension=imagick.so
service php-fpm restart

1.4.7.2. mongodb

wget http://pecl.php.net/get/mongodb-1.2.8.tgz

tar zxvf mongodb-1.2.8.tgz
cd mongodb-1.2.8
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install

vi /usr/local/webserver/php/etc/php.ini
extension=mongodb.so
service php-fpm restart

1.4.7.3. redis

wget http://pecl.php.net/get/redis-3.1.2.tgz

tar zxvf redis-3.1.2.tgz
cd redis-3.1.2
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make && make install

vi /usr/local/webserver/php/etc/php.ini
extension=redis.so
service php-fpm restart

1.5. 整合Nginx+PHP

1.5.1. 修改nginx配置文件

vi /usr/local/webserver/nginx/conf/nginx.conf

include vhost/*.conf;

mkdir /usr/local/webserver/nginx/conf/vhost

vi /usr/local/webserver/nginx/conf/vhost/local.conf
server {
	listen       80 default_server;
	server_name  _;
	root   /home/www/local;
	index  index.html index.htm index.php;
	charset utf-8;
	error_page   500 502 503 504  /50x.html;
	
	location ~ \.php$ {
		fastcgi_pass unix:/tmp/php-cgi.sock;
		fastcgi_index  statistic.php;
		include        fastcgi_params;
	}
}

service nginx restart
mkdir -p /home/www/local
vi /home/www/local/phpinfo.php

<?php
phpinfo();
?>

访问http://your_ip/phpinfo.php

1.6. MongoDB安装

由于各种的问题,编译V8比较困难,所以直接下载二进制包

1.6.1. 下载MongoDB

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.2.tgz

1.6.2. 解压安装

tar zxvf mongodb-linux-x86_64-3.4.2.tgz
mv mongodb-linux-x86_64-3.4.2 /usr/local/webserver/mongodb

1.6.3. 添加配置文件

mkdir /usr/local/webserver/mongodb/etc
mkdir /usr/local/webserver/mongodb/data
vi /usr/local/webserver/mongodb/etc/mongo.conf
systemLog:
    destination: file
    path: "/usr/local/webserver/mongodb/mongodb.log"
    logAppend: true
storage:
    dbPath: "/usr/local/webserver/mongodb/data"
    repairPath: "/usr/local/webserver/mongodb/data/repair"
    engine: mmapv1
    journal: 
        enabled: true
    directoryPerDB: true
    mmapv1:
        nsSize: 16
        quota:
            enforced: true
            maxFilesPerDB: 1024
processManagement:
    fork: true
    pidFilePath: "/usr/local/webserver/mongodb/mongodb.pid"
net:
    bindIp: 127.0.0.1
    port: 27017
security:
    authorization: disabled
/usr/local/webserver/mongodb/bin/mongod -f /usr/local/webserver/mongodb/etc/mongo.conf

/usr/local/webserver/mongodb/bin/mongo
use admin
db.createUser(
{
    user: "admin",
    pwd: "hemengze",
    roles:
    [
      {
        role: "root",
        db: "admin"
      }
    ]
  }
)

1.6.4. 启用认证

/usr/local/webserver/mongodb/bin/mongod -shutdown -f /usr/local/webserver/mongodb/etc/mongo.conf

vi /usr/local/webserver/mongodb/etc/mongo.conf
authorization: enabled

/usr/local/webserver/mongodb/bin/mongod -f /usr/local/webserver/mongodb/etc/mongo.conf

1.6.5. 配置服务

vi /etc/init.d/mongo
#!/bin/bash
# mongo Startup script for the Mongodb Database Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Mongodb is a high-performance Database server.
# processname: mongo
# pidfile: /usr/local/webserver/mongodb/mongodb.pid
# config: /usr/local/webserver/mongodb/etc/mongo.conf
mongod=/usr/local/webserver/mongodb/bin/mongod
mongo_config=/usr/local/webserver/mongodb/etc/mongo.conf
mongo_pid=/usr/local/webserver/mongodb/mongodb.pid
RETVAL=0
prog="mongo"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $mongod ] || exit 0
# Start mongo daemons functions.
start() {
if [ -e $mongo_pid ];then
   echo "${prog} already running...."
   exit 1
fi
   echo -n $"Starting $prog: "
   daemon $mongod -f ${mongo_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/mongo
   return $RETVAL
}
# Stop mongo daemons functions.
stop() {
        echo -n $"Stopping $prog: "
        $mongod -shutdown -f ${mongo_config}
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/mongo
}
# See how we were called.
case "$1" in
start)
        start
        ;;
stop)
        stop
        ;;
restart)
        stop
        start
        ;;
status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|status|help}"
        exit 1
esac
exit $RETVAL
chmod +x /etc/init.d/mongo
chkconfig --add mongo
chkconfig --level 2345 mongo on

1.7. 安装Redis

1.7.1. 下载源码

wget http://download.redis.io/redis-stable.tar.gz

1.7.2. 安装redis

tar zxvf redis-stable.tar.gz
cd redis-stable
make
cd src
make install PREFIX=/usr/local/webserver/redis
cd ../

1.7.3. 安装redis服务

utils/install_server.sh

下面的配置可根据实际目录自己分配
Port           : 6379
Config file    : /usr/local/webserver/redis/etc/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/webserver/redis/bin/redis-server
Cli Executable : /usr/local/webserver/redis/bin/redis-cli

添加PATH

vi /etc/profile
export PHP_HOME=/usr/local/webserver/php
export MYSQL_HOME=/usr/local/webserver/mysql
export REDIS_HOME=/usr/local/webserver/redis
export MONGO_HOME=/usr/local/webserver/mongodb
export PATH=$PHP_HOME/bin:$MYSQL_HOME/bin:$REDIS_HOME/bin:$MONGO_HOME/bin:$PATH

source /etc/profile