LANMP On CentOS 6

摘要

——在CentOS6.2-x86_64上安装Apache,Nginx,MySQL,Php

环境:最小化安装系统

yum install lrzsz mlocate.x86_64 wget lsof unzip \
setuptool ntsysv \
system-config-firewall-base.noarch \
system-config-date.noarch system-config-date-docs.noarch \
system-config-firewall.noarch system-config-firewall-tui.noarch \
system-config-kdump.noarch system-config-keyboard.x86_64 \
system-config-kickstart.noarch system-config-language.noarch \
system-config-lvm.noarch system-config-network-tui.noarch \
system-config-printer.x86_64 system-config-printer-libs.x86_64 \
system-config-printer-udev.x86_64 system-config-services.noarch \
system-config-services-docs.noarch system-config-users.noarch system-config-users-docs.noarch

预先设置环境变量

export MYSQL_HOME="/usr/local/mysql"
export APACHE_HOME="/usr/local/apache2"
export NGINX_HOME="/usr/local/nginx"
export PHP_HOME="/usr/local/php"
export PATH="$PATH:$MYSQL_HOME/bin:$APACHE_HOME/bin:$PHP_HOME/bin:$NGINX_HOME/sbin"

安装MySQL

tar zxf mysql-5.6.26-linux-glibc2.5-x86_64.tar.gz 
mv mysql-5.6.26-linux-glibc2.5-x86_64 /usr/local/mysql
useradd mysql -s /sbin/nologin
mkdir -p /data/mysql/data
chown -R mysql:mysql /usr/local/mysql /data/mysql/data
---------------------------------------------
#可选:ln -s /usr/local/mysql/lib/libmysqlclient.so.16 \
/usr/lib/libmysqlclient.so.16

配置启动MySQL

1. 先修改 mysql 的配置 my.cnf

mv /etc/my.cnf /etc/my.cnf.old
vi /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
user=mysql
explicit_defaults_for_timestamp=true
innodb_file_per_table=1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/tmp/mysql.sock

2. mysql 初始化安装

/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/data/mysql/data \
--user=mysql
#for mysql 5.7.x
/usr/local/mysql/bin/mysqld --initialize \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/data/mysql/data
#额外的工作
ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_root_password';

3. 将 mysql 加入开机启动

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 2 mysqld off
chkconfig --level 345 mysqld on
vi /etc/init.d/mysqld
#查找并修改以下变量内容:
basedir=/usr/local/mysql
datadir=/data/mysql/data

4. 启动 mysql

/usr/local/mysql/bin/mysqld_safe &
#或者 /etc/init.d/mysqld start
#或者 service mysqld start

安装Apache

yum install zlib-devel gcc gcc-c++ pcre.x86_64 pcre-devel.x86_64 pcre-static.x86_64
wget http://archive.apache.org/dist/httpd/httpd-2.4.17.tar.gz
tar zxf httpd-2.4.17.tar.gz
wget http://www.us.apache.org/dist//apr/apr-1.5.2.tar.gz
tar zxf apr-1.5.2.tar.gz
cp -r apr-1.5.2 httpd-2.4.17/srclib/apr
wget http://www.us.apache.org/dist//apr/apr-util-1.5.4.tar.gz
tar zxf apr-util-1.5.4.tar.gz
cp -r apr-util-1.5.4 httpd-2.4.17/srclib/apr-util
cd httpd-2.4.17
./configure --prefix=/usr/local/apache2 \
--sysconfdir=/data/httpd/conf \
--enable-so \
--enable-dav \
--enable-module=so \
--enable-mods-shared=all \
--enable-rewrite \
--with-mpm=prefork \
--enable-cache \
--with-included-apr 
make
make install
cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd
vi /etc/init.d/httpd
#在 #!/bin/sh 下面添加:
# chkconfig: 2345 14 90
# description: Activates/Deactivates Apache Web Server
#最后,运行chkconfig把Apache添加到系统的启动服务组里面:
chkconfig --add httpd
chkconfig --level 2 httpd off
chkconfig --level 345 httpd on
#安装时的--sysconfigdir参数只修改了httpd.conf本身的父文件夹,而httpd.conf内容中的Include指令依然搜索--prefix制定的apache程序目录
#解决方法:
#在httpd.conf中加入
Define extra_conf_parent_path /foo/bar/httpd
Include ${extra_conf_parent_path}/conf/extra/httpd-vhosts.conf

安装PHP

yum install bison 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 curl curl-devel \
e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel \
openssl openssl-devel openldap openldap-devel openldap-clients \
openldap-servers net-snmp net-snmp-devel libevent libevent-devel
wget http://jaist.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
wget http://jaist.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.gz
tar -zxf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make
make install
tar -zxf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make
make install
wget https://github.com/skvadrik/re2c/releases/download/0.16/re2c-0.16.tar.gz
tar zxf re2c-0.16.tar.gz
cd re2c-0.16
./configure
make
make install
cd php-5.6.12
./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-config-file-path=/etc \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-pdo-mysql=/usr/local/mysql \
--with-gd \
--with-gettext \
--with-iconv \
--with-zlib \
--with-openssl \
--with-curl \
--with-freetype-dir=/usr/include/freetype2/freetype/ \
--with-bz2 \
--with-jpeg-dir \
--with-png-dir \
--with-mcrypt \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-sockets \
--enable-pcntl \
--enable-xml \
--enable-maintainer-zts 
 
make
make install
若有:
Sorry, I cannot run apxs.  Possible reasons follow:
1. Perl is not installed
2. apxs was not found. Try to pass the path using --with-apxs2=/path/to/apxs
3. Apache was not built using --enable-so (the apxs usage page is displayed)
The output of /usr/local/apache2/bin/apxs follows:
./configure: /usr/local/apache2/bin/apxs: /replace/with/path/to/perl/interpreter: bad interpreter: No such file or directory
configure: error: Aborting
则修改/usr/local/apache2/bin/apxs的/replace/with/path/to/perl/interpreter为perl程序路径(/usr/bin/perl)
 
若有:
chmod 755 /usr/local/apache2/modules/libphp5.so chmod: cannot access `/usr/local/apache2/modules/libphp5.so': No such file or directory apxs:Error: Command failed with rc=65536
则:
yum install libtool
./libtool --version,确保php和apache的libtool版本一致
---------------------------------------------------------
若有:
checking whether to enable embedded MySQLi support… no
checking for mysql_set_server_option in -lmysqlclient… no
configure: error: wrong mysql library version or lib not found. Check config.log for more information.
则:
在php/ext/mysqli中
yum -y install mysql-devel
/usr/local/php/bin/phpize
./configure --enable-embedded-mysqli=shared --enable-shared --with-php-config=/usr/local/php/bin/php-config
make
make install
---------------------------------------------------------
若有:
configure: error: Cannot find libmysqlclient_r under/usr/local/mysql. Note that the MySQL client library is not bundledanymore!
cd /usr/local/mysql/lib
ln -s libmysqlclient.so.20.0.9 libmysqlclient_r.so
---------------------------------------------------------
若有:
gcc: Internal error: Killed (program cc1)
Please submit a full bug report.
See //bugzilla.redhat.com/bugzilla> for instructions.
make: *** [ext/date/lib/parse_date.lo] Error 1
则:
cd libmcrypt-2.5.8/libltdl/
pwd
/byrd/tools/libmcrypt-2.5.8/libltdl
./configure --enable-ltdl-install
ldconfig
cd ../../5.6.12
make && make install

安装Nginx

groupadd www
useradd -g www -s /sbin/nologin -M www
---------------------------------------------
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz
tar zxf libunwind-1.1.tar.gz
cd libunwind-1.1
./configure
make
make install
---------------------------------------------
git clone https://github.com/gperftools/gperftools.git
cd gperftools
./configure --enable-frame-pointers
make
make install
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
---------------------------------------------
cd nginx-1.9.4
./configure --prefix=/usr/local/nginx \
--conf-path=/data/nginx/conf/nginx.conf \
--user=www --group=www \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-google_perftools_module
make
make install
#创建模块需要的目录
mkdir /tmp/tcmalloc
chmod 777 /tmp/tcmalloc -R

vi /conf/nginx.conf

user www;
worker_processes 8;
pid logs/nginx.pid;
#可选,根据编译时的参数
google_perftools_profiles /tmp/tcmalloc;
worker_rlimit_nofile 51200;
events {
    use epoll;
    worker_connections 51200;
}
http {
    include mime.types;
    default_type application/octet-stream;
    
    access_log off;
    error_log /dev/null;
    
    include proxy.conf;
    
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    #client_max_body_size 8m; #move to proxy.conf
    
    sendfile on;
    tcp_nopush on;
    
    keepalive_timeout 120;
    #fastcgi_connect_timeout 300;
    #fastcgi_send_timeout 300;
    #fastcgi_read_timeout 300;
    #fastcgi_buffer_size 64k;
    #fastcgi_buffers 4 64k;
    #fastcgi_busy_buffers_size 128k;
    #fastcgi_temp_file_write_size 128k;
    tcp_nodelay on;
    
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    
    log_format access_format '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';
    
    include vhosts/*.conf;
}

proxy.conf

proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For   $proxy_add_x_forwarded_for;
client_max_body_size    50m;
client_body_buffer_size 256k;
proxy_connect_timeout   1200;
proxy_send_timeout      1200;
proxy_read_timeout      1200;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
#Nginx cache
client_body_temp_path client_body 1 2;
proxy_temp_path proxy_temp 1 2;
#client_body_temp_path      /tmpfs/client_body_temp 1 2;
#proxy_temp_path            /tmpfs/proxy_temp 1 2;
#fastcgi_temp_path          /tmpfs/fastcgi_temp 1 2;

vi /etc/rc.d/init.d/nginx

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/data/nginx/conf/nginx.conf
nginx_pid=/data/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx $nginx_pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
chmod 755 /etc/rc.d/init.d/nginx
 
chkconfig --add nginx
chkconfig --level 2 nginx off
chkconfig --level 345 nginx on
#启动nginx后,验证:
lsof -n | grep tcmalloc 
lsof -n | grep nginx
#为 https 的 nginx 生成证书
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
openssl rsa -in server.key -out server_nopwd.key
openssl x509 -req -days 365 -in server.csr -signkey server_nopwd.key -out server.crt

null.conf

server {
        listen  80 default_server;
        server_name    _;
        index index.html index.htm index.php;
        root    /data/www/null;
        location / {
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   html;
        }
        access_log    off;
}

  

MySQL运行时加载tcmalloc

根据mysql安装位置而定

vi /usr/local/mysql/bin/mysqld_safe
#在# executing mysqld_safe的下一行,加入以下内容
export LD_PRELOAD="/usr/local/lib/libtcmalloc.so"(见:前文安装的libunwind和gperftools)

nginx代理apache后台获取IP问题

跑在后方 apache 上的应用获取到的IP都是Nginx所在服务器的IP ,或者是本机 127.0.0.1 。

最明显就是查看 apache 的访问日志。发现始终都是内网的IP。

可以通过修改 nginx proxy 的参数令后端应用获取到 Nginx 发来的请求报文获取到外网的IP。

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

但这解决的问题单单只是应用上,apache 日志上所获取的ip依然还是本地。

solution:

apache 2.2.x

第三方的mod配合Nginx proxy使用。

说明:http://stderr.net/apache/rpaf/

下载:http://stderr.net/apache/rpaf/download/ 最新版本是 mod_rpaf-0.6.tar.gz

tar zxvf mod_rpaf-0.6.tar.gz

cd mod_rpaf-0.6

Apache 的目录按自己的环境修改,并选择相应的安装方式:

/usr/local/apache2/bin/apxs -i -a -c mod_rpaf.c #Apache 1.3.x 的安装方式

/usr/local/apache2/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c #Apache 2.x 的安装方式

安装完成后在httpd.conf添加如下:

LoadModule rpaf_module modules/mod_rpaf-2.0.so

RPAFenable On

RPAFsethostname On

RPAFproxy_ips 127.0.0.1 192.168.1.50 #Nginx所在服务器的IP

RPAFheader X-Forwarded-For

保存退出后重启apache

apache 2.4.x

apache 2.4 自带了mod_remoteip:

参考:http://imcat.in/replacing-mod_rpaf-with-mod_remoteip-in-apache-2-4-nginx-real_ip-problem-solution/

参考:https://blog.linuxeye.com/378.html

或者依然使用apache 2.4 对应的mod_rpaf:

参考:http://www.meidahua.com/2015/apache-2-4-mod_rpaf.html

参考:http://jayanvenugopalan.blogspot.hk/2014/07/install-modrpaf-with-apache-24.html

MySQL从服务器my.cnf

server-id = 2
replicate-ignore-db=mysql
relay-log = /data/mysql/logs/mysql-relay-bin
relay-log-index = /data/mysql/logs/mysql-relay-bin.index
master-info-file = /data/mysql/logs/mysql-master.info
relay-log-info-file = /data/mysql/logs/mysql-relay-log.info

  

CHANGE MASTER TO MASTER_HOST='119.254.81.66', MASTER_USER='backup_user', MASTER_PASSWORD='db_sub-1';
“OR”
CHANGE MASTER TO MASTER_HOST='192.168.0.200', MASTER_USER='backup_user', MASTER_PASSWORD='db_sub-1';
start salve;

为php追加若干扩展

wget https://pecl.php.net/get/redis-2.2.8.tgz
tar zxf redis-2.2.8.tgz 
cd redis-2.2.8
phpize
./configure
make
make install

  

pecl install mongo
pecl install mongodb
pecl install memcache
pecl install memcached # 需要libmemcached的支持
http://www.phpgao.com/vps_ssh.html

 

 

转载于:https://www.cnblogs.com/saonian/p/6796077.html

你可能感兴趣的:(LANMP On CentOS 6)