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 <http://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 <somepath>/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
时间: 2024-07-28 23:17:56

LANMP On CentOS 6的相关文章

CentOS 6、7下IPSEC/L2TP VPN一键安装脚本(openswan+xl2tpd)

CentOS 6.7下IPSEC/L2TP VPN一键安装脚本(openswan+xl2tpd) 概念性的东西这里不再赘述,网上有太多,一键安装脚本也有很多,但是很多不能用,能用的也仅仅只是在CentOS6下使用,CentOS7基本没看到这些安装脚本.于是花了一些时间来折腾测试,写这个脚本方便以后VPN的一键安装搭建.其中用的开源软件包是openswan和xl2tpd,,中间碰到过很多很多问题,如openswan和xl2tpd之间的兼容性问题. 请容许我这样做,脚本依赖<OneinStack>

CentOS 6、7下pptp vpn一键安装脚本

之前有折腾过<CentOS 6.7下IPSEC/L2TP VPN一键安装脚本>,不稳定.不支持IOS,因此换成pptp,并已经添加到<lnmp一键安装包>.这个脚本可以单独使用,直接复制或下载执行即可,不用依赖安装包的其它脚本. CentOS 6.7下pptp vpn一键安装脚本,安装如下: wget http://mirrors.linuxeye.com/scripts/vpn_centos.sh chmod +x ./vpn_centos.sh ./vpn_centos.sh

CentOS 6、7 下 pptp vpn 一键 安装 脚本

wget http://mirrors.linuxeye.com/scripts/vpn_centos.sh chmod +x ./vpn_centos.sh ./vpn_centos.sh 如下源码: #!/bin/bash # # Author: yeho <lj2007331 AT gmail.com> # Blog: http://blog.linuxeye.com # # Installs a PPTP VPN-only system for CentOS # Check if us

Centos6.7下安装配置VPN

在Vultr上买了台VPS准备做VPN,不贵5刀,位置是日本东京的.ping值在100-200之间,还好算说的过去. Vultr地址 系统选择的Centos6 的版本是6.7 在网上查了查linux下安装VPN的资料,解决方案真不少,有用OpenVPN的,有的是PPTP的.OpenVPN的稍麻烦些,需要证书什么的.因为是自己用,所以采用了PPTP的方案. 有好多同行,已经把PPTP这块的东西都写好了脚本,到时候直接运行脚本就行了. 教程参考地址 Centos6.7 下PPTP VPN一键安装脚本

一键搭建VPN服务器

经常要在外网之间折腾的朋友,势必会想尽办法利用各种翻墙工具(如google自带的翻墙.psiphon.lantern.shadowsocksX....). 其实这些工具本人都一一用过,感觉就是各种不稳定,而起速度还很慢.shadowsocksX相对还算好些,毕竟是要花钱买的,但也是遭到国内封杀,有时也连不上.考虑到经常需要,所以我就在亚太新加坡节点弄了一台做VPN代理服务器.用来翻墙.速度和稳定程度都还不错. 以前搭建VPN都要手工配置各种文件,相当麻烦.现在我推荐一个脚本给大家,vpn_cen

centos下安装lanmp

一: 安装nginx-1.6.1 #wget http://nginx.org/download/nginx-1.6.1.tar.gz #解压到/mnt2/software_source/ #cd /mnt2/software_source/nginx-1.6.1 # ./configure  --prefix=/mnt2/software_install/nginx-1.6.1_install 1) PCRE library  not found yum -y install pcre-dev

lanmp安装一(centos+apache+nginx+mysql+php=lanmp地址下载)

背景 centos7 官网地址https://www.centos.org/download/ 下载选择DVD版进入(也就是标准版,系统齐全) 点击任何一个国家的下载链接 下载地址  http://mirrors.cqu.edu.cn/CentOS/7/isos/x86_64/CentOS-7-x86_64-DVD-1708.iso =============================================================== Apache 2.4.27 官网地址

使用 shell 脚本实现 LANMP 一键安装

使用 shell 脚本来实现 LANMP 系统的一键安装.使用的操作系统是 CentOS 6 ,不区分 32 位和 64 位,要求机器可以连通互联网.支持 LAMP 和 LNMP ,MySQL 支持 5.1 和 5.6 两个版本, php 支持 5.3 和 5.6 两个版本, apache 2.2 ,nginx 1.8. 代码如下: #!/bin/bash echo "It will install lamp or lnmp." sleep 1 # get the archive of

lanmp环境的搭建

wget http://super.5ul.cn:8000/linux-soft/bash/ffdisk.sh sh ffdisk.sh 1:把centos自带的yum源换成阿里云的yum源 cd /etc/yum.repos.d/ rm -rf * wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo yum makecache 2:wget http://dl.wdlinu