阿汤博客-承接中小企业服务器维护和网站维护,有意者可以联系博主!

生产环境Linux+Apache+php+memcach安装

运维维护 40℃ 0评论

一、Apache安装

1、安装依赖包

#yum install -y pcre pcre-devel apr apr-devel zlib-devel gcc openssl-devel expat-devel

 

2、创建相应文件夹

#mkdir -pv /app/{httpd24,libmemcached,memcached,php}

#mkdir /root/src

#mkdir -pv /data/{www,logs}

 

3、下载aprapr-util编译安装

#cd /src

#wget https://mirrors.aliyun.com/apache/apr/apr-1.6.3.tar.gz

#wget https://mirrors.aliyun.com/apache/apr/apr-util-1.6.1.tar.gz

#tar zxf apr-util-1.6.1.tar.gz

#tar zxf apr-1.6.3.tar.gz

 

#编译安装apr

#cd apr-1.6.3

#./configure –prefix=/usr/local/apr

#make && make install

 

#编译安装apr-util

#cd apr-util-1.6.1

#./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr

#make && make install

 

4、下载编译安装Apache2.4.34

#wget https://mirrors.aliyun.com/apache/httpd/httpd-2.4.34.tar.gz

#tar -zxf httpd-2.4.34.tar.gz

#cp -rf apr-1.6.3 httpd-2.4.34/srclib/apr

#cp -rf apr-util-1.6.1 httpd-2.4.34/srclib/apr-util

#cd httpd-2.4.34

#./configure –prefix=/app/httpd24 –enable-so –enable-ssl –enable-rewrite –with-zlib –with-pcre –with-included-apr –enable-modules=most –enable-mpms-shared=all –with-mpm=prefork  –with-apr=/usr/local/apr/ –with-apr-util=/usr/local/apr-util/

#make

#make install

 

5、配置Apache

# echo "export PATH=$PATH:/app/httpd24/bin" > /etc/profile.d/httpd.sh

#source /etc/profile.d/httpd.sh

#cp /app/httpd24/bin/apachectl /etc/init.d/httpd

#chmod +x /etc/init.d/httpd

#vim /etc/init.d/httpd  #在第二行(#!/bin/sh)下面添加

#chkconfig: 2345 80 90

#description: Apache Web Server

#chkconfig –add httpd  #添加服务到chkconfig

#firewall-cmd –zone=public –add-port=80/tcp –permanent

 

#groupadd www && useradd -g www -s /sbin/nologin -M www

#chown -R www:www /app/httpd24/

#chown -R www:www /data/www/

 

6、配置httpd.conf

# vim /app/httpd24/conf/httpd.conf

LoadModule rewrite_module modules/mod_rewrite.s #去掉注释,开启伪静态模块

 

LoadModule php5_module modules/libphp5.so  #添加php

找到AddType application/x-gzip .gz .tgz添加如下两行,Apache支持PHP扩展

AddType application/x-httpd-php  .php

AddType application/x-httpd-php-source  .phps

 

找到#ServerName www.example.com:80改成ServerName localhost #否则重启会报错httpd: Could not reliably determine the server's fully qualified domain name, using xxxx. Set the 'ServerName' directive globally to suppress this message

 

末尾添加,隐藏Apache版本号

ServerTokens ProductOnly

ServerSignature Off

 

更改用户和组为www

User www

Group www

 

7、启用虚拟机

注释DocumentRoot "/app/httpd24/htdocs"

找到#Include /etc/httpd/extra/httpd-vhosts.conf

改成Include conf/vhost/*.conf

#mkdir /app/httpd24/conf/vhost

#vim /app/httpd24/conf/vhost/vhosts1.conf

<VirtualHost *:80>

ServerAdmin [email protected]

DocumentRoot "/data/www"

ServerName 192.168.10.80  #顶级域名

ServerAlias 192.168.10.80   #二级域名

ErrorLog "/data/logs/vhost1-error.log"

CustomLog "/data/logs/vhost1-access.log" combined

<Directory "/data/www">

#    SetOutputFilter DEFLATE

    Options FollowSymlinks #禁止目录遍历

    AllowOverride All     #支持伪静态.htaccess

    Order allow,deny

    Allow from all

    DirectoryIndex  index.php index.html

    Require all granted

</Directory>

</VirtualHost>

#/etc/init.d/httpd restart

 

二、PHP安装

1、安装相关依赖包文件

#yum install gcc gcc+ gcc-c++ bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel jemalloc jemalloc-devel libpng-devel libjpeg-devel freetype-devel libtool -y

 

#部分依赖无法yum安装需手动下载安装

#wget https://mirrors.aliyun.com/epel/7/x86_64/Packages/l/libmcrypt-2.5.8-13.el7.x86_64.rpm

#https://mirrors.aliyun.com/epel/7/x86_64/Packages/l/libmcrypt-devel-2.5.8-13.el7.x86_64.rpm

#rpm -ivh libmcrypt-2.5.8-13.el7.x86_64.rpm  libmcrypt-devel-2.5.8-13.el7.x86_64.rpm

或者直接添加阿里云epel源:

#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

然后再执行yum install libmcrypt-devel libmcrypt -y

 

2、下载并编译安装PHP

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

#groupadd php && useradd -g php -s /sbin/nologin -M php

#tar -zxf php-5.6.37.tar.gz

#cd php-5.6.37

#./configure –prefix=/app/php –with-config-file-path=/app/php –enable-inline-optimization –disable-debug –disable-rpath –enable-shared –enable-opcache –enable-fpm –with-apxs2=/app/httpd24/bin/apxs –with-fpm-user=php –with-fpm-group=php –with-mysql=mysqlnd –with-mysqli=mysqlnd –with-pdo-mysql=mysqlnd –with-gettext –enable-mbstring –with-iconv –with-mcrypt –with-freetype-dir –with-jpeg-dir –with-png-dir –with-libpng –with-gd –with-mhash –with-openssl –enable-bcmath –enable-soap –with-libxml-dir –enable-pcntl –enable-shmop –enable-sysvmsg –enable-sysvsem –enable-sysvshm –enable-sockets –with-curl –with-zlib –enable-zip –with-bz2 –with-readline

#make

#make test

#make install

 

3、配置PHP

#cp /root/src/php-5.6.37/php.ini-development /app/php/php.ini

 

#cp /app/php/etc/php-fpm.conf.default /app/php/etc/php-fpm.conf

#cp /root/src/php-5.6.37/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

# chmod +x /etc/init.d/php-fpm

 

#echo "export PATH=$PATH:/app/php/bin" > /etc/profile.d/php.sh

#source /etc/profile.d/php.sh

#chkconfig –add php-fpm  #添加服务到chkconfig

 

三、安装memcached

1、安装依赖

#yum install -y libevent-devel libevent

 

2、下载编译安装

#wget  http://www.memcached.org/files/memcached-1.4.24.tar.gz

# tar zxvf memcached-1.4.24.tar.gz

#cd memcached-1.4.24

#./configure –prefix=/app/memcached

#make && make install

 

3、配置memcache

#echo "export PATH=$PATH:/app/memcached/bin" > /etc/profile.d/memcached.sh

#source /etc/profile.d/memcached.sh

#groupadd memcached

#useradd memcached -g memcached -s /sbin/nologin #创建运行用户

#memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid #命令启动

 

创建启动脚本vim /etc/init.d/memcached

#!/bin/bash

#

# Init file for memcached

#

# chkconfig: 2345 86 14

# description: Distributed memory caching daemon

#

# processname: memcached

# config: /etc/sysconfig/memcached

 

. /etc/rc.d/init.d/functions

 

## Default variables

PORT="11211"

USER="memcached"

MAXCONN="1024"

CACHESIZE="64"

 

RETVAL=0

prog="/app/memcached/bin/memcached"

desc="Distributed memory caching"

lockfile="/app/memcached/memcached.pid"

start() {

        echo -n $"Starting $desc (memcached): "

        daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE

        RETVAL=$?

        echo

        [ $RETVAL -eq 0 ] && touch $lockfile

        return $RETVAL

}

 

stop() {

        echo -n $"Shutting down $desc (memcached): "

        killproc $prog

        RETVAL=$?

        echo

        [ $RETVAL -eq 0 ] && rm -f $lockfile

        return $RETVAL

}

 

restart() {

        stop

        start

}

 

reload() {

        echo -n $"Reloading $desc ($prog): "

        killproc $prog -HUP

        RETVAL=$?

        echo

        return $RETVAL

}

 

case "$1" in

  start)

        start

        ;;

  stop)

        stop

        ;;

  restart)

        restart

        ;;

  condrestart)

        [ -e $lockfile ] && restart

        RETVAL=$?

        ;;       

  reload)

        reload

        ;;

  status)

        status $prog

        RETVAL=$?

        ;;

   *)

        echo $"Usage: $0 {start|stop|restart|condrestart|status}"

        RETVAL=1

esac

 

exit $RETVAL

# chmod +x /etc/init.d/memcached

# chkconfig  –add memcached

#/etc/init.d/memcached start

 

四、安装PHP扩展

1、编译安装memcache扩展库

# wget http://pecl.php.net/get/memcache-3.0.8.tgz

#tar -zxf memcache-3.0.8.tgz

#cd memcache-3.0.8

#phpize

#./configure –with-php-config=/app/php/bin/php-config

#make

#make test

#make install

 

2、编译安装libmemcached-扩展库

#wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz

#tar zxvf libmemcached-1.0.18.tar.gz

#cd libmemcached-1.0.18

#./configure –prefix=/app/libmemcached

#make && make install

 

3、编译安装memcached扩展库

# wget http://pecl.php.net/get/memcached-2.2.0.tgz

#tar zxvf memcached-2.2.0.tgz

#cd memcached-2.2.0

#phpize

#./configure –with-php-config=/app/php/bin/php-config –with-libmemcached-dir=/app/libmemcached –disable-memcached-sasl

#make

#make test

#make install

 

4、编译安装phpredis扩展库

#git clone https://github.com/phpredis/phpredis

#cd phpredis/

#phpize

#./configure –with-php-config=/app/php/bin/php-config

#make

#make test

#make install

 

5、编译安装gd扩展库

#cd /root/src/php-5.6.37/ext/gd

# phpize

#./configure –with-php-config=/app/php/bin/php-config –with-png-dir –with-freetype-dir –with-jpeg-dir –with-gd

#make

#make install

 

6、配置php.ini

#时区修改

# sed -i 's/expose_php = On/expose_php = Off/' /app/php/php.ini   #隐藏版本号

#vim /app/php/php.ini   里面修改时区

date.timezone = "Asia/Shanghai"

extension=memcache.so

extension=memcached.so

extension=redis.so

extension_dir="/app/php/lib/php/extensions/no-debug-non-zts-20131226/"

 

五、系统优化

1、内核参数优化

# vim /etc/sysctl.conf

net.ipv4.icmp_echo_ignore_broadcasts = 1

net.ipv4.icmp_ignore_bogus_error_responses = 1

net.ipv4.tcp_syncookies = 1

net.ipv4.conf.all.log_martians = 1

net.ipv4.conf.default.log_martians = 1

net.ipv4.conf.all.accept_source_route = 0

net.ipv4.conf.default.accept_source_route = 0

net.ipv4.conf.all.rp_filter = 1

net.ipv4.conf.default.rp_filter = 1

net.ipv4.conf.all.accept_redirects = 0

net.ipv4.conf.default.accept_redirects = 0

net.ipv4.conf.all.secure_redirects = 0

net.ipv4.conf.default.secure_redirects = 0

net.ipv4.conf.all.send_redirects = 0

net.ipv4.conf.default.send_redirects = 0

kernel.exec-shield = 1

kernel.randomize_va_space = 1

fs.file-max = 65535

kernel.pid_max = 65536

net.core.netdev_max_backlog = 4096

net.ipv4.tcp_window_scaling = 1

net.ipv4.tcp_max_syn_backlog = 4096

net.ipv4.tcp_max_tw_buckets = 4096

net.ipv4.tcp_keepalive_time = 20

net.ipv4.ip_forward = 0

net.ipv4.tcp_mem = 192000 300000 732000

net.ipv4.tcp_rmem = 51200 131072 204800

net.ipv4.tcp_wmem = 51200 131072 204800

net.ipv4.tcp_keepalive_intvl = 5

net.ipv4.tcp_keepalive_probes = 2

net.ipv4.tcp_orphan_retries = 3

net.ipv4.tcp_syn_retries = 3

net.ipv4.tcp_synack_retries = 3

net.ipv4.tcp_retries2 = 5

net.ipv4.tcp_fin_timeout = 30

net.ipv4.tcp_max_orphans = 2000

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_tw_recycle = 1

vm.min_free_kbytes=409600

vm.vfs_cache_pressure=200

vm.swappiness = 40

vm.dirty_expire_centisecs = 1500

vm.dirty_writeback_centisecs = 1000

vm.dirty_ratio = 2

vm.dirty_background_ratio = 100

 

# sysctl -p

 

#vim /etc/security/limits.conf

把下面两项都设置为65535

* soft nofile 65535  #每个用户可用的最大进程数量(软限制)

* hard nofile 65535  #单个用户可用的最大进程数量(硬限制)

#立即生效执行:

#ulimit -n 65535    #允许每个进程可以同时打开文件

#ulimit -u 65535   #每个用户最大进程使用数

 

2、Apache优化

#vim /app/http24/conf/httdp.conf

找到#Include conf/extra/httpd-mpm.conf

改成Include conf/vhost/httpd-mpm.conf

找到#Include conf/extra/httpd-default.conf

改成Include conf/vhost/httpd-default.conf

 

#cp /app/httpd24/conf/extra/httpd-default.conf /app/httpd24/conf/vhost/

#sed -i 's/KeepAlive On/KeepAlive Off/g' /app/httpd24/conf/vhost/httpd-default.conf #关闭keeplive,如果不关闭设置timeout时间短一些

 

#vim /app/httpd24/conf/vhost/httpd-mpm.conf

<IfModule mpm_prefork_module>

    StartServers             5   #启动时进程数

    MinSpareServers         5   #空闲进程

MaxSpareServers         10  #最大空闲进程

ServerLimit             1500  #最大进程数

    MaxRequestWorkers      1000  #大并发进程数

    MaxConnectionsPerChild 4000   #每个子进程最大连接数, 0不限制

</IfModule>

转载请注明:阿汤博客 » 生产环境Linux+Apache+php+memcach安装

喜欢 (0)or分享 (0)