Linux环境部署之LNMP编译安装

周末一同事在阿里云主机(centos)搭建个个人博客,需要帮忙部署LNMP环境(Linux+Nginx+Mysql+PHP),很久没有搞过环境安装了,现将整个安装过程记录一下

环境变量预备
安装第三方安装源epel
yum安装公共和依赖软件包
编译安装常用lib库及依赖软件
安装mysql
编译安装php
编译安装memcached和redis
编译安装php扩展
编译安装nginx
系统参数调优
错误问题记录

环境变量预备

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

if [[ -z $(awk -F: '$1=="www"{print}' /etc/passwd) ]];then
/usr/sbin/useradd -M www -s /sbin/nologin
fi

if [[ -z $(awk -F: '$1=="mysql"{print}' /etc/passwd) ]];then
/usr/sbin/useradd -M mysql -s /sbin/nologin
fi

if [[ $(uname -m) == "x86_64" ]];then _BIT="64";else _BIT="32";fi

安装第三方安装源epel

rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

yum安装公共和依赖软件包

LANG=C
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 ncurses ncurses-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel sysstat gd gd-devel net-snmp net-snmp-libs lrzsz libtool libtool-ltdl libtool-ltdl-devel make sharutils

编译安装常用lib库及依赖软件

###常用lib库 Libiconv ,实现字符编码间的转换

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

###常用lib库 Libmcrypt ,一个功能强大的加密算法扩展库,在保证应用的应用性时,通过该扩展库的加密算法保证安全性

wget --no-check-certificate https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download -O libmcrypt-2.5.8.tar.gz
tar zxf libmcrypt-2.5.8.tar.gz && cd libmcrypt-2.5.8 && ./configure && make && make install;
cd libltdl && ./configure --enable-ltdl-install && make && make install;cd ../../

###常用lib库 Mhash ,hash算法扩展库

wget --no-check-certificate https://sourceforge.net/projects/mhash/files/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz/download -O mhash-0.9.9.9.tar.gz
tar zxf mhash-0.9.9.9.tar.gz && cd mhash-0.9.9.9 && ./configure && make && make install;cd ../

###常用lib库libevent 是一个用C语言编写的、轻量级的开源高性能网络库

if [[ ${_BIT} == 64 ]];then
    rm -f /usr/lib64/libevent*
    rm -f /usr/local/lib/libevent*
else
    rm -f /usr/lib/libevent*
    rm -f /usr/local/lib/libevent*
fi

wget --no-check-certificate https://github.com/downloads/libevent/libevent/libevent-1.4.12-stable.tar.gz
tar zxf libevent-1.4.12-stable.tar.gz && cd libevent-1.4.12-stable && ./configure && make && make install; cd ../

###curl

wget --no-check-certificate https://curl.haxx.se/download/curl-7.20.0.tar.gz
tar zxf curl-7.20.0.tar.gz && cd curl-7.20.0 && ./configure --prefix=/usr/local --with-ssl --enable-file --enable-http --enable-ftp --enable-ipv6 && make && make install;cd ../

echo '/usr/local/lib/' > /etc/ld.so.conf.d/libs.conf
/sbin/ldconfig

###pcre ,PCRE(Perl Compatible Regular Expressions中文含义:perl语言兼容正则表达式)是一个用C语言编写的正则表达式函数库,由菲利普.海泽(Philip Hazel)编写。PCRE是一个轻量级的函数库,比Boost之中的正则表达式库小得多。PCRE十分易用,同时功能也很强大,性能超过了POSIX正则表达式库和一些经典的正则表达式库。因此,几乎所有web应用想使用正则功能都要安装此软件包

wget --no-check-certificate https://sourceforge.net/projects/pcre/files/pcre/7.9/pcre-7.9.tar.gz/download -O pcre-7.9.tar.gz 
tar zxf pcre-7.9.tar.gz && cd pcre-7.9 && ./configure && make && make install; cd ../

###常用lib库 Mcrypt ,一个功能强大的加密算法扩展库,在保证应用的应用性时,通过该扩展库的加密算法保证安全性

wget --no-check-certificate https://sourceforge.net/projects/mcrypt/files/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz/download -O mcrypt-2.6.8.tar.gz
tar zxf mcrypt-2.6.8.tar.gz && cd mcrypt-2.6.8 && ./configure && make && make install; cd ../

### jpegsrc,包含jpeg库,这些库用于读写jpeg文件

wget http://ijg.org/files/jpegsrc.v7.tar.gz
tar zxf jpegsrc.v7.tar.gz && cd jpeg-7 && ./configure --prefix=/usr/local/libjpeg && make && make install ; cd ../

### freetype,FreeType库是一个完全免费(开源)的、高质量的且可移植的字体引擎,它提供统一的接口来访问多种字体格式文件,包括TrueType, OpenType, Type1, CID, CFF, Windows FON/FNT, X11 PCF等。支持单色位图、反走样位图的渲染。FreeType库是高度模块化的程序库,虽然它是使用ANSI C开发,但是采用面向对象的思想,因此,FreeType的用户可以灵活地对它进行裁剪

wget --no-check-certificate https://sourceforge.net/projects/freetype/files/freetype2/2.3.5/freetype-2.3.5.tar.gz/download -O freetype-2.3.5.tar.gz
tar zxf freetype-2.3.5.tar.gz && cd freetype-2.3.5 && ./configure --prefix=/usr/local/freetype && make && make install; cd ../

### 常用lib库 libpng,包含 libpng 库.这些库被其他程式用于读写png文件

wget --no-check-certificate https://sourceforge.net/projects/libpng/files/libpng12/older-releases/1.2.40/libpng-1.2.40.tar.gz/download -O libpng-1.2.40.tar.gz
tar zxf libpng-1.2.40.tar.gz && cd libpng-1.2.40 && ./configure --prefix=/usr/local/libpng && make && make install; cd ../

### gettext-devel开发包,Gettext 软件包包含用于国际化和地区化的工具

yum -y install gettext-devel

###gd库 ,gd库是php处理图形的扩展库,gd库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。 在网站上GD库通常用来生成缩略图或者用来对图片加水印或者对网站数据生成报表

wget http://pkgs.fedoraproject.org/repo/pkgs/gd/gd-2.0.33.tar.gz/be0a6d326cd8567e736fbc75df0a5c45/gd-2.0.33.tar.gz
tar zxvf gd-2.0.33.tar.gz && cd gd-2.0.33/ && sed -i -e '16s#png.h#/usr/local/libpng/include/png.h#' gd_png.c && ./configure --prefix=/usr/local/gd --with-jpeg=/usr/local/libjpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype && make && make install ; cd ../
sed -i -e "28i\\\n void *data;" /usr/local/gd/include/gd_io.h

### yum 安装 libmcrypt-devel

yum -y erase libmcrypt
yum -y install libmcrypt-devel

安装mysql

这里我下载的是 mysql-5.1.72-linux-x86_64-glibc23.tar.gz 软件包,直接下载解压就可以使用

wget http://downloads.mysql.com/archives/get/file/mysql-5.1.72-linux-x86_64-glibc23.tar.gz
tar zxf mysql-5.1.72-linux-x86_64-glibc23.tar.gz
mv mysql-5.1.72-linux-x86_64-glibc23 /usr/local/mysql
chown mysql.mysql -R /usr/local/mysql
echo '/usr/local/mysql/lib/' > /etc/ld.so.conf.d/mysql.conf ldconfig

编译安装memcached 和 redis

memcached 和 redis 是高性能的缓存系统

安装memcached

### Libmemcached()
yum -y install gcc44 gcc44-c++ libstdc++44-devel
export CC=/usr/bin/gcc44
export CXX=/usr/bin/g++44
wget https://launchpad.net/libmemcached/1.0/1.0.14/+download/libmemcached-1.0.14.tar.gz
tar zxf libmemcached-1.0.14.tar.gz && cd libmemcached-1.0.14 && ./configure --with-memcached --disable-sasl && make && make install
unset CC
unset CXX
cd ../

编译安装redis

## 编译安装 redis 
wget http://download.redis.io/releases/redis-3.0.7.tar.gz 
tar zxf redis-3.0.7.tar.gz
cd redis-3.0.7
make && make install

编译安装php

wget http://museum.php.net/php5/php-5.4.9.tar.gz
tar zxvf php-5.4.9.tar.gz && cd php-5.4.9
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=mysqlnd --with-iconv-dir=/usr/local --with-freetype-dir=/usr/local/freetype --with-jpeg-dir=/usr/local/libjpeg --with-png-dir=/usr/local/libpng --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local --without-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd=/usr/local/gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --enable-sysvsem --enable-sysvshm --enable-sysvmsg --with-xmlrpc --enable-zip --enable-soap --without-pear --enable-zip --with-bz2 --enable-calendar --enable-ftp --enable-ctype --enable-exif --disable-ipv6 --with-sqlite3 --disable-pdo --disable-phar 
make ZEND_EXTRA_LIBS='-liconv' && make install

yes|cp php.ini-production /usr/local/php/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
mkdir -p /usr/local/php/logs
cd ../

安装php扩展

### eaccelerator 扩展

安装eaccelerator

wget --no-check-certificate https://github.com/eaccelerator/eaccelerator/tarball/master -O eaccelerator-eaccelerator-42067ac.tar.gz
tar zxvf eaccelerator-eaccelerator-42067ac.tar.gz && cd eaccelerator-eaccelerator-42067ac && /usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-eaccelerator-shared-memory --with-eaccelerator-content-caching --enable-eaccelerator && make && make install
cd ../

启用eaccelerator

echo "[Zend]" >> /usr/local/php/etc/php.ini
echo "zend_extension=\"/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.shm_size=\"64\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.cache_dir=\"/dev/shm\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.enable=\"1\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.optimizer=\"1\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.check_mtime=\"1\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.debug=\"0\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.filter=\"\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.shm_max=\"0\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.shm_ttl=\"3600\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.shm_prune_period=\"3600\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.shm_only=\"1\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.compress=\"1\"" >> /usr/local/php/etc/php.ini
echo "eaccelerator.compress_level=\"9\"" >> /usr/local/php/etc/php.ini

###PHP memcached 扩展

wget http://pecl.php.net/get/memcached-2.1.0.tgz
tar zxvf memcached-2.1.0.tgz && cd memcached-2.1.0
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcached --enable-memcached-json
yes|cp -r ../php-5.4.9/ext/json/ /usr/local/php/include/php/ext/ 
make && make install
cd ../

###PHP redis 扩展

wget https://pecl.php.net/get/redis-2.2.8.tgz
tar zxvf redis-2.2.8.tgz && cd redis-2.2.8
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-redis --enable-redis-igbinary && make && make install
cd ../

编译安装nginx

wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar zxf nginx-1.8.1.tar.gz && cd nginx-1.8.1 && ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module && make && make install;
cd ../

系统参数调优

对linux系统参数相关调优

echo '*               soft    nofile           65535' >> /etc/security/limits.conf
echo '*               hard    nofile           65535' >> /etc/security/limits.conf

echo "net.ipv4.tcp_fin_timeout = 10" >> /etc/sysctl.conf
echo "net.ipv4.tcp_keepalive_time = 600" >> /etc/sysctl.conf
echo "net.ipv4.tcp_keepalive_probes = 1" >> /etc/sysctl.conf
echo "net.ipv4.tcp_keepalive_intvl = 1" >> /etc/sysctl.conf
echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.conf
echo "net.ipv4.tcp_max_tw_buckets = 2000" >> /etc/sysctl.conf
echo "net.ipv4.tcp_max_syn_backlog = 2048" >> /etc/sysctl.conf
echo "net.core.netdev_max_backlog =  65536" >> /etc/sysctl.conf
echo "net.ipv4.tcp_window_scaling = 0"  >> /etc/sysctl.conf
echo "net.ipv4.tcp_sack = 0" >> /etc/sysctl.conf
echo "net.core.somaxconn = 65500" >> /etc/sysctl.conf
echo "net.core.wmem_default = 8388608" >> /etc/sysctl.conf
echo "net.core.rmem_default = 8388608" >> /etc/sysctl.conf
echo "net.core.rmem_max = 16777216" >> /etc/sysctl.conf
echo "net.core.wmem_max = 16777216" >> /etc/sysctl.conf
echo "net.ipv4.tcp_timestamps = 0" >> /etc/sysctl.conf
echo "net.ipv4.tcp_synack_retries = 1" >> /etc/sysctl.conf
echo "net.ipv4.tcp_syn_retries = 1" >> /etc/sysctl.conf
echo "net.ipv4.tcp_tw_recycle = 1" >> /etc/sysctl.conf
echo "net.ipv4.tcp_tw_reuse = 1" >> /etc/sysctl.conf
echo "net.ipv4.tcp_mem = 94500000 915000000 927000000" >> /etc/sysctl.conf
echo "net.ipv4.tcp_max_orphans = 3276800" >> /etc/sysctl.conf
echo "net.ipv4.ip_local_port_range = 10240  65535" >> /etc/sysctl.conf
echo "net.ipv4.ip_conntrack_max = 10485760" >> /etc/sysctl.conf

yes|cp /usr/share/zoneinfo/Asia/Chongqing /etc/localtime
rdate -s time-nw.nist.gov
/sbin/hwclock
/sbin/modprobe ip_nat_ftp
/sbin/sysctl -p

错误问题记录

错误一:

php 编译的时候报错误如下

checking for libmcrypt version... >= 2.5.6
checking for mcrypt_module_open in -lmcrypt... yes
checking for MSSQL support via FreeTDS... no
checking for MySQL support... yes
checking for specified location of the MySQL UNIX socket... no
configure: error: Cannot find libmysqlclient under /usr.
Note that the MySQL client library is not bundled anymore!

原因:

如果你的mysql 是编译安装的话 ,可能会报错 ; php 编译 configure 的时候应该要加上 –with-MySQL=”mysql安装”,不然就会报此错误,原因是要php支持mysql,在编译安装的时候需要找到 mysql.h 库文件

解决:

find / -name mysql.h

如果没有找到,说明MySQL的开发库没有安装,将在这一步安装

yum install mysql-devel

php编译的时候加上查找到mysql.h文件的路径前缀,比如我执行得到的结果是:/usr/include/mysql/mysql.h,这里只需要写–with-mysql=/usr就可以了

错误二:

php编译的过程中跑着跑着出现了错误如下:

cc: Internal error: Killed (program cc1)

原因:

很明显这是编译进程被系统给 kill 掉了,原因无非就是内存太小导致的,常见于内存比较少的 VPS 。(阿里云服务器内存太小)

解决:

就是在编译 PHP 时 configure 增加一个选项 –disable-fileinfo 即可

转载请注明:Huangdc » Linux环境部署之LNMP编译安装

results matching ""

    No results matching ""