生產環境編譯安裝LAMP環境

#!/bin/bash

#

#LAMP自動化部署腳本(我的生產環境一般都將所有需要用的軟件放在/data/source_package目錄,同步到服務器,這樣更快速和安全。)


#################安裝前準備##################

#1、清除系統默認安裝的http、mysql、php

rpm -q mysql-server &> /dev/null && rpm -e --nodeps mysql-server

rpm -q mysql &> /dev/null && rpm -e --nodeps mysql

rpm -q httpd &> /dev/null && rpm -e --nodeps httpd

rpm -q php &> /dev/null && rpm -e --nodeps php

rpm -q php-fpm &> /dev/null && rpm -e --nodeps php-fpm


#2、安裝gcc、gcc-c++ make wget編譯包

yum -y install gcc gcc-c++ make 


#################安裝http##################


#1、安裝httpd編譯需的依賴包

yum -y install pcre pcre-devel openssl openssl-devel


#2、安裝apr支持包

cd /data/source_package/lamp/http/

tar zxvf apr-1.4.6.tar.gz

cd apr-1.4.6

./configure --prefix=/data/soft/apr  

make && make install

cd ../


#3、安裝apr-util支持包

tar zxvf apr-util-1.5.2.tar.gz

cd apr-util-1.5.2

./configure --prefix=/data/soft/apr-util --with-apr=/data/soft/apr

make && make install

cd ../


#4、安裝apr-iconv支持包

tar zvxf apr-iconv-1.2.1.tar.gz

cd apr-iconv-1.2.1

./configure --prefix=/data/soft/apr-iconv --with-apr=/data/soft/apr 

make && make install

cd ../


#5、安裝httpd

tar zxvf httpd-2.4.4.tar.gz

cd httpd-2.4.4

./configure --prefix=/data/soft/apache --sysconfdir=/etc/httpd --with-apr=/data/soft/apr --with-apr-iconv=/data/soft/apr-iconv --with-apr-util=/data/soft/apr-util --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre  --enable-modules=most --enable-mpms-shared=all --with-mpm=event

make && make install


#8、備份httpd運行文件

cp /etc/httpd/httpd.conf /etc/httpd/httpd.conf.bak


#9、配置httpd文件

sed -i '192a ServerName localhost:80' /etc/httpd/httpd.conf       

sed -i '194a Pidfile "/var/run/httpd.pid"' /etc/httpd/httpd.conf  


#10、設置httpd服務自啓動

cp /data/soft/apache/bin/apachectl /etc/rc.d/init.d/httpd

sed -i '2a #chkconfig: 345 85 15' /etc/rc.d/init.d/httpd

chmod 775 /etc/rc.d/init.d/httpd

chkconfig --add httpd

chkconfig httpd on


#11、啓動httpd服務

service httpd start


#################安裝mysql##################

#1、安裝mysql編譯需的依賴包

yum -y install bison cmake openssl openssl-devel ncurses ncurses-devel 


#2、創建mysql 用戶組

groupadd mysql

useradd -g mysql mysql


#3、創建mysql安裝目錄 

mkdir /data/soft/mysql


#4、mysql安裝目錄、授權

chown -R mysql:mysql /data/soft/mysql

chmod 775 /data/soft/mysql


#5、創建mysql數據目錄 

mkdir /data/soft/mysql/data


#6、mysql 數據目錄 授權

chown -R mysql:mysql /data/soft/mysql/data

chmod 775 /data/soft/mysql/data


#7、創建mysql日誌目錄 

mkdir /data/soft/mysql/logs


#8、mysql日誌目錄授權 

chown -R mysql:mysql /data/soft/mysql/logs

chmod 775 /data/soft/mysql/logs


#9、mysql pid文件目錄授權

chown -R mysql:mysql /tmp

chmod 775 /tmp


#10、解壓mysql

cd /data/source_package/lamp/mysql

tar zxvf mysql-5.5.17.tar.gz

cd mysql-5.5.17


#11、編譯、安裝mysql

cmake -DCMAKE_INSTALL_PREFIX=/data/soft/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306

make && make install


#12、設置MYSQL運行配置/etc/my.cnf文件

cat > /etc/my.cnf << EOF

# Example MySQL config file for large systems.

#

# This is for a large system with memory = 512M where the system runs mainly

# MySQL.

#

# MySQL programs look for option files in a set of

# locations which depend on the deployment platform.

# You can copy this option file to one of those

# locations. For information about these locations, see:

# http://dev.mysql.com/doc/mysql/en/option-files.html

#

# In this file, you can use all long options that a program supports.

# If you want to know which options a program supports, run the program

# with the "--help" option.


# The following options will be passed to all MySQL clients

[client]

#password = your_password

port = 3306

socket = /tmp/mysql.sock

default-character-set=utf8


# Here follows entries for some specific programs


# The MySQL server

[mysqld]

character_set_server=utf8 #設置服務端編碼

max_connections=2000         #根據服務器性能調節

basedir=/data/soft/mysql #設置安裝目錄,這樣在系統啓動時才能正確運行到/etc/rc.d/init.d/mysql start


port = 3306

socket = /tmp/mysql.sock

skip-external-locking

skip-name-resolve

key_buffer_size = 256M

max_allowed_packet = 20M

table_open_cache = 256

sort_buffer_size = 1M

read_buffer_size = 1M

read_rnd_buffer_size = 4M

myisam_sort_buffer_size = 64M

thread_cache_size = 8

query_cache_size= 16M

# Try number of CPU's*2 for thread_concurrency

thread_concurrency = 8


# Don't listen on a TCP/IP port at all. This can be a security enhancement,

# if all processes that need to connect to mysqld run on the same host.

# All interaction with mysqld must be made via Unix sockets or named pipes.

# Note that using this option without enabling named pipes on Windows

# (via the "enable-named-pipe" option) will render mysqld useless!

#skip-networking


# Replication Master Server (default)

# binary logging is required for replication

log-bin=mysql-bin


# binary logging format - mixed recommended

binlog_format=mixed


# required unique id between 1 and 2^32 - 1

# defaults to 1 if master-host is not set

# but will not function as a master if omitted

server-id = 1


# Replication Slave (comment out master section to use this)

#

# To configure this host as a replication slave, you can choose between

# two methods :

#

# 1) Use the CHANGE MASTER TO command (fully described in our manual) -

#    the syntax is:

#

#    CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,

#    MASTER_USER=<user>, MASTER_PASSWORD=<password> ;

#

#    where you replace <host>, <user>, <password> by quoted strings and

#    <port> by the master's port number (3306 by default).

#

#    Example:

#

#    CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,

#    MASTER_USER='joe', MASTER_PASSWORD='secret';

#

# OR

#

# 2) Set the variables below. However, in case you choose this method, then

#    start replication for the first time (even unsuccessfully, for example

#    if you mistyped the password in master-password and the slave fails to

#    connect), the slave will create a master.info file, and any later

#    change in this file to the variables' values below will be ignored and

#    overridden by the content of the master.info file, unless you shutdown

#    the slave server, delete master.info and restart the slaver server.

#    For that reason, you may want to leave the lines below untouched

#    (commented) and instead use CHANGE MASTER TO (see above)

#

# required unique id between 2 and 2^32 - 1

# (and different from the master)

# defaults to 2 if master-host is set

# but will not function as a slave if omitted

#server-id       = 2

#

# The replication master for this slave - required

#master-host     =   <hostname>

#

# The username the slave will use for authentication when connecting

# to the master - required

#master-user     =   <username>

#

# The password the slave will authenticate with when connecting to

# the master - required

#master-password =   <password>

#

# The port the master is listening on.

# optional - defaults to 3306

#master-port     =  <port>

#

# binary logging - not required for slaves, but recommended

#log-bin=mysql-bin


# Uncomment the following if you are using InnoDB tables

#innodb_data_home_dir = /usr/local/bin/mysql/data

#innodb_data_file_path = ibdata1:10M:autoextend

#innodb_log_group_home_dir = /usr/local/bin/mysql/data

# You can set .._buffer_pool_size up to 50 - 80 %

# of RAM but beware of setting memory usage too high

#innodb_buffer_pool_size = 256M

#innodb_additional_mem_pool_size = 20M

# Set .._log_file_size to 25 % of buffer pool size

#innodb_log_file_size = 64M

#innodb_log_buffer_size = 8M

#innodb_flush_log_at_trx_commit = 1

#innodb_lock_wait_timeout = 50


datadir=/data/soft/mysql/data

pid-file=/data/soft/mysql/data/mysql.pid

tmpdir=/tmp


expire_logs_day=7

log_error=/data/soft/mysql/logs/error.log


[mysqldump]

quick

max_allowed_packet = 16M


[mysql]

no-auto-rehash

# Remove the next comment character if you are not familiar with SQL

#safe-updates


[myisamchk]

key_buffer_size = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M


[mysqlhotcopy]

interactive-timeout

EOF


#13、初始化MYSQL系統數據庫

/data/soft/mysql/scripts/mysql_install_db --user=mysql --basedir=/data/soft/mysql --datadir=/data/soft/mysql/data


#14、設置MYSQL服務自啓動

cp /data/soft/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld

chmod 775 /etc/rc.d/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld on


#15、啓動MYSQL服務

service mysqld start


#16、把MYSQL路徑添加到環境變量

echo 'export PATH=$PATH:/data/soft/mysql/bin:/data/soft/mysql/lib' >> /etc/profile

source /etc/profile


#17、link MYSQL依賴庫到系統庫中

ln -s /data/soft/mysql/lib/* /usr/lib64/


#18、設置MYSQL管理員密碼

mysqladmin -uroot password "passwd"


#################安裝php##################

#1、安裝php編譯需的依賴包

yum -y install libmng libxml2 libxml2-devel libevent libevent-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel bzip2 bzip2-devel gmp gmp-devel gd gd-devel


#2、安裝libmcrypt支持

cd /data/source_package/lamp/php

tar zxvf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure

make && make install

cd ../


#3、安裝mhash支持包

tar jxvf mhash-0.9.9.9.tar.bz2

cd mhash-0.9.9.9

./configure

make && make install

cd ../


#4、創建lib庫軟連接

ln -sv /usr/local/lib/* /usr/lib64/


#5、安裝mcrypt支持包

tar zxvf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8

./configure   

make && make install

cd ../


#6、安裝php

tar jxvf php-5.4.17.tar.bz2

cd php-5.4.17

./configure --prefix=/data/soft/php --with-config-file-path=/etc  --with-mysql=/data/soft/mysql/ --with-mysqli=/data/soft/mysql/bin/mysql_config  --with-pdo-mysql=/data/soft/mysql/ --with-iconv-dir=/data/soft/  --with-apxs2=/data/soft/apache/bin/apxs --disable-phar --with-pcre-regex  --with-zlib --with-bz2 --with-curl --with-gmp --enable-dba --with-libxml-dir  --with-zlib-dir  --with-mhash --with-gd --enable-gd-native-ttf --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-mbstring --with-mcrypt --enable-pcntl --enable-xml --disable-rpath --enable-shmop --enable-sockets --enable-zip --enable-bcmath --disable-ipv6 

make && make install


#7、複製php.ini文件

cp php.ini-production /etc/php.ini


#8、修改php.ini文件

sed -i 's@memory_limit = 128M@memory_limit = 512M@' /etc/php.ini

sed -i '919a date.timezone = "PRC"' /etc/php.ini

sed -i 's@;error_log = syslog@error_log = /data/www/log/php_error.log@' /etc/php.ini


#9、把PHP路徑添加到環境變量

echo 'export PATH=$PATH:/data/soft/php/bin' >> /etc/profile

source /etc/profile


#10、修改httpd.conf的文件

sed -i '381a AddType application/x-httpd-php  .php .phtml' /etc/httpd/httpd.conf

sed -i '382a AddType application/x-httpd-php-source .phps' /etc/httpd/httpd.conf

sed -i '383a PHPIniDir "/etc/php.ini"' /etc/httpd/httpd.conf

sed -i 's@DirectoryIndex index.html@DirectoryIndex index.php index.html@' /etc/httpd/httpd.conf

sed -i 's@DocumentRoot "/data/soft/apache/htdocs"@#DocumentRoot "/data/soft/apache/htdocs"@' /etc/httpd/httpd.conf

sed -i 's@#Include /etc/httpd/extra/httpd-vhosts.conf@Include /etc/httpd/extra/httpd-vhosts.conf@' /etc/httpd/httpd.conf


#11、配置虛擬主機

cp /etc/httpd/extra/httpd-vhosts.conf /etc/httpd/extra/httpd-vhosts.conf.bak

cat > /etc/httpd/extra/httpd-vhosts.conf << EOF

<VirtualHost *:80>

    DocumentRoot "/data/www"

    ErrorLog "/data/www/logs/httpd_error_log"

  <Directory "/data/www">

    Options none

    AllowOverride None

    Require all granted

  </Directory>

</VirtualHost>

EOF


#12、創建網頁目錄和日誌錄

mkdir /data/www

chmod 777 /data/www

mkdir /data/www/logs

chmod 777 /data/www/logs 


#13、修改完成後必須重啓Apache服務器,才能重新加載配置文件使修改生效。

service httpd restart


#14、試PHP環境是否可以正常運行,在/data/www/目錄下建一個test.php文件,內容如下:

cat > /data/www/test.php << EOF

<?php


phpinfo();


?>

EOF


#15、打開瀏覽器,在地址欄輸入http://ip/test.php來打開測試網頁


#################安裝Xcache##################

#1、安裝Xcache

cd /data/source_package/lamp/xcache

tar zxvf xcache-3.1.0.tar.gz

cd xcache-3.1.0

/data/soft/php/bin/phpize

./configure --with-php-config=/data/soft/php/bin/php-config --enable-xcache --enable-xcache-coverager --enable-xcache-optimizer

make && make install

#安裝完畢之後,會提示一個安裝路徑,記下來。

#比如:Installing shared extensions: /data/soft/php/lib/php/extensions/no-debug-zts-20100525/


#2、複製xcache.so文件到/data/soft/php/include/php/ext/

cp /data/soft/php/lib/php/extensions/no-debug-zts-20100525/xcache.so /data/soft/php/include/php/ext/


#3、配置/etc/php.ini文件,在最後追加Xcache配置

echo '[xcache-common]

extension = xcache.so


[xcache.admin]

xcache.admin.user = "admin"

xcache.admin.pass = "f6c770a1c6a0cd6b5639baf16d4c6a3d"


[xcache]

xcache.cacher = On

xcache.shm_scheme = "mmap"

xcache.size = 32M

; cpu number (cat /proc/cpuinfo |grep -c processor)

xcache.count = 2

xcache.slots = 8k

xcache.ttl = 0

xcache.gc_interval = 0

xcache.var_size = 2M'>> /etc/php.ini


#4、重啓httpd服務。

service httpd restart


#5、重新測試入http://ip/test.php,看是否加載到xcache


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章