生產環境Linux+Apache+php+memcach安裝

2018年10月20日13:44:21 2 5,219 ℃

一、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 admin@admin.com

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>

【騰訊云】云服務器、云數據庫、COS、CDN、短信等云產品特惠熱賣中

發表評論

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前評論:2   其中:訪客  0   博主  0

    • avatar daxi 0

      學無止境,認真拜讀!

      • avatar xing 0

        來看看,因為,總能學到東西!