您的位置:首页 > 其它

生产环境编译安装LNMP环境

2015-12-29 15:31 537 查看
#!/bin/bash
#
#LNMP自动化部署脚本(我的生产环境一般都将所有需要用的软件放在/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

#################安装nginx##################
#1、安装nginx编译需要的依赖包
yum -y install pcre pcre-devel openssl openssl-devel

#2、创建nginx用户和nginx组。
groupadd -g 108 -r nginx
useradd -u 108 -r -g 108 nginx

#3、安装nginx
cd /data/source_package/lnmp/nginx
tar zxvf nginx-1.6.1.tar.gz
cd nginx-1.6.1
./configure \
--prefix=/data/soft/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre
make && make install

#4、设置nginx服务自启动
echo '#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/data/soft/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac'>/etc/rc.d/init.d/nginx

#5、设置nginx可执行权限,并开启nginx服务
chmod 755 /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig nginx on

#6、启动nginx服务
service nginx 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/lnmp/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 = 4M
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-fpm##################
#1、安装php-fpm编译需要的依赖包
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/lnmp/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、创建软连接
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-fpm
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/ --disable-phar --with-pcre-regex --with-zlib --with-bz2 --with-curl --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 --enable-fpm --disable-debug
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/logs/php_error.log@' /etc/php.ini

#9、把PHP路径添加到环境变量
echo 'export PATH=$PATH:/data/soft/php/bin' >> /etc/profile
source /etc/profile

#10、为php-fpm提供Sysv init脚本,并将其添加至服务列表
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod 775 /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on

#11、配置php-fpm文件
cp /data/soft/php/etc/php-fpm.conf.default /data/soft/php/etc/php-fpm.conf
sed -i 's@pm.max_children = 5@pm.max_children = 150@g' /data/soft/php/etc/php-fpm.conf
sed -i 's@pm.start_servers = 2@pm.start_servers = 8@g' /data/soft/php/etc/php-fpm.conf
sed -i 's@pm.min_spare_servers = 1@pm.min_spare_servers = 5@g' /data/soft/php/etc/php-fpm.conf
sed -i 's@pm.max_spare_servers = 3@pm.max_spare_servers = 10@g' /data/soft/php/etc/php-fpm.conf
sed -i 's@;pid = run/php-fpm.pid@pid = /data/soft/php/var/run/php-fpm.pid@g' /data/soft/php/etc/php-fpm.conf

#12、启动php-fpm
service php-fpm start

###########################整合nginx和php###########################
#1、创建网站访问目录和日志目录
mkdir /data/www/
chmod 777 /data/www
mkdir /data/www/logs
chmod 777 /data/www/logs

#2、编辑/etc/nginx/nginx.conf,添加index.php格式的主页,并修改网站访问目录为/data/www
cat >/etc/nginx/nginx.conf <<EOF
#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root /data/www;
index index.php index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1; #}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /data/www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}

# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}
EOF

#3、编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容
cat > /etc/nginx/fastcgi_params <<EOF
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING \$query_string;
fastcgi_param REQUEST_METHOD \$request_method;
fastcgi_param CONTENT_TYPE \$content_type;
fastcgi_param CONTENT_LENGTH \$content_length;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
fastcgi_param SCRIPT_NAME \$fastcgi_script_name;
fastcgi_param REQUEST_URI \$request_uri;
fastcgi_param DOCUMENT_URI \$document_uri;
fastcgi_param DOCUMENT_ROOT \$document_root;
fastcgi_param SERVER_PROTOCOL \$server_protocol;
fastcgi_param REMOTE_ADDR \$remote_addr;
fastcgi_param REMOTE_PORT \$remote_port;
fastcgi_param SERVER_ADDR \$server_addr;
fastcgi_param SERVER_PORT \$server_port;
fastcgi_param SERVER_NAME \$server_name;
EOF

#4、重启nginx服务
service nginx restart

#5、在/data/www新建test.php的测试页面,测试php是否能正常工作。
cat > /data/www/test.php <<EOF
<?php
phpinfo();
?>
EOF

#6、打开浏览器,在地址栏输入http://ip/test.php来运行该文件

#################安装Xcache##################
#1、安装Xcache
cd /data/source_package/lnmp/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-non-zts-20100525/

#2、复制xcache.so文件到/data/soft/php/include/php/ext/
cp /data/soft/php/lib/php/extensions/no-debug-non-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 nginx restart

#5、重新测试入http://ip/test.php,看是否加载到xcache

本文出自 “kundylee” 博客,请务必保留此出处http://kundylee.blog.51cto.com/6605245/1729556
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: