LAMP环境搭建

简介

本文简述LAMP环境搭建,便于我们日后搭建测试环境及生产环境。

编译安装mysql

1.安装依赖并添加mysql用户

yum -y install gcc gcc-c++ ncurses-devel cmake unzip
useradd mysql

2.创建mysql安装目录及数据目录

mkdir -p /usr/local/mysql
mkdir -p /data/mysql/data
mkdir -p /data/mysql/logs

3.修改mysql目录所有者和组

chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql 

4.编译安装

tar -zxvf mysql-5.6.22.tar.gz
cd mysql-5.6.22
#从mysql5.5起,mysql源码安装开始使用cmake了,设置编译参数 
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/data/mysql/data \
-DEXTRA_CHARSETS=all \
-DMYSQL_TCP_PORT=3306
make && make install

注:重新运行配置,需要删除CMakeCache.txt文件

5.初始化配置

/usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysql/data --user=mysql  --basedir=/usr/local/mysql

6.复制mysql服务启动配置文件

cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

7.复制mysql服务启动脚本及加入PATH路径

#拷贝服务脚本到init.d目录
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld   
#编辑/etc/profile文件,
vim /etc/profile  
#在文件末尾添加  
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH  
export PATH
#关闭文件,运行下面的命令,让配置立即生效  
source /etc/profile

8.启动mysql服务并设置开机启动

service mysqld start
chkconfig --level 35 mysqld on
检查mysql服务是否启动
netstat -tulnp | grep 3306

若启动正常,则登录测试mysql -u root -p
密码为空,如果能登陆上,则安装成功。
9.设置密码及访问权限

grant all privileges on *.* to ‘test’@'10.10.10.58' identified by ‘test’;
set password for root@localhost = password('test');
flush privileges;

安装apache

1.安装apr-1.5.0

tar -zxvf apr-1.5.0.tar.gz 
./configure --prefix=/usr/local/apr
make && make install

2.安装apr-util-1.5.3

tar -zxvf apr-util-1.5.3.tar.gz 
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install    

3.安装pcre-8.36

./configure --prefix=/usr/local/pcre
make && make install

4.安装apache

tar -zxvf httpd-2.4.7.tar.gz 
./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --enable-modules=mall --enable-rewrite --enable-mpms-shared=all --with-mpm=event --enable-v4-mapped --enable-so
make && make install

5.启动apache并检查

/usr/local/apache/bin/apachectl -k start
netstat -ntlp |grep :80

若端口存在,说明明apache服务安装并启动成功。

安装php

1.安装依赖

yum install -y libxml2-devel libjpeg-devel libpng-devel freetype-devel openssl-devel libcurl-devel
rpm -ivh libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpm 
rpm -ivh libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpm

2.安装php

./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-openssl --with-mcrypt --with-zlib --with-libxml-dir --enable-xml --with-freetype-dir --with-curl --enable-sockets --with-config-file-path=/usr/local/php/etc --with-mysql --with-mysqli --with-pdo-mysql --with-mbstring --enable-mbstring --with-gd
make && make install

注意:若无–with-mbstring –enable-mbstring –with-gd,否则需要在后面php扩展中安装。
安装完毕后,需要复制php.ini配置文件

cp /usr/local/src/php/php-5.4.22/php.ini-production /usr/local/php/etc/php.ini

另,apahce运行php,需要在/usr/local/apache/conf/http.conf中添加以下几行,才能使apache正常运行php程序:

LoadModule php5_module        modules/libphp5.so
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

3.安装php扩展模块

(1)安装mongodb扩展

tar -zxvf mongo-1.2.10.tgz
cd mongo-1.2.10
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config 
make && make install

在/usr/local/php/etc/php.ini配置添加extension=mongo.so

检查php及扩展是否成功,在/usr/local/apache/htdoc下添加phpinfo.php文件

vim phpinfo.php
<?php
phpinfo();
?>

重启apache后访问ip/phpinfo.php,若出现php信息界面并能查看到安装的相关扩展模块,则说明php及扩展模块安装成功。

修改php时区

vim /usr/local/php/etc/php.ini
date.timezone = PRC
date.timezone = "Asia/Shanghai"

(2)memcached及memcached扩展
a.安装memcached扩展

tar -zxvf libmemcached-1.0.18.tar.tar
cd libmemcached-1.0.18
./configure --prefix=/usr/local/libmemcached --with-memcached
make && make install
tar -zxvf memcached-2.2.0.tgz
cd memcached-2.2.0
/usr/local/php/bin/phpize
./configure --enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
make && make install

b.安装memcached

tar -zxvf libevent-2.0.22-stable.tar.gz
./configure --prefix=/usr/local/libevent
Make && make install
tar -zxvf memcached-1.4.22.tar.gz
cd memcached-1.4.22
./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent  
make && make install

(3)安装eaccelerator扩展

tar -zxvf eaccelerator-eaccelerator-42067ac.tar.gz
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make && make install
mkdir -p /usr/local/eaccelerator_cache
chmod –R 777 /usr/local/eaccelerator_cache
#修改配置文件php.ini
extension=eaccelerator.so
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter="!*.yml.php"
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.allowed_admin_path = "/web/test"

(4)安装soap扩展

cd /usr/local/src/php/php-5.4.22/ext/soap
/usr/local/php/bin/phpize
./configure --enable-soap  --with-php-config=/usr/local/php/bin/php-config
make && make install
#修改配置文件php.ini
extension=soap.so

(5)安装gd扩展

cd /usr/local/src/php/php-5.4.22/ext/gd/
/usr/local/php/bin/phpize
./configure --with-png-dir --with-freetype-dir --with-jpeg-dir --with-gd -with-php-config=/usr/local/php/bin/php-config
make && make install
#修改配置文件php.ini
extension=gd.so

(6)安装mbstring扩展

cd /usr/local/src/php/php-5.4.22/ext/mbstring/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
Make && make install
#修改配置文件php.ini
Extension=mbstring.so

(7)安装exif扩展

cd /usr/local/src/php/php-5.4.22/ext/exif
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
Make && make install
#修改配置文件php.ini
Extension=exif.so

(8)安装memcache扩展

tar –zxvf memcache-2.2.7.taz
cd memcache
/usr/loca/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config ---enable-memcache
make && make install
#修改配置文件php.ini
Extension=memcache.so

安装nginx

1.解压依赖包

unzip headers-more-nginx-module-master.zip 
tar -zxvf echo-nginx-module-0.57.tar.gz 
tar -zxvf nginx-1.6.0.tar.gz 
tar -zxvf ngx_cache_purge-2.1.tar.gz
tar -zxvf pcre-8.36.tar.gz 
tar -zxvf zlib-1.2.8.tar.gz

2.编译安装

cd /usr/local/src/nginx/nginx-1.6.0
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx1.6 --sbin-path=/usr/local/nginx1.6 --conf-path=/usr/local/nginx1.6/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-pcre=/usr/local/src/nginx/pcre-8.36 --with-zlib=/usr/local/src/nginx/zlib-1.2.8 --add-module=/usr/local/src/nginx/ngx_cache_purge-2.1 --add-module=/usr/local/src/nginx/headers-more-nginx-module-master
make && make install

3.添加用户

useradd nginx

4.检查配置文件并启动

/usr/local/nginx1.6/nginx -t
/usr/local/nginx1.6/nginx
netstat -ntlp |grep :80

若80端口存在,则说明nginx安装并启动成功

配置apache

1.创建apache根目录并授权

mkdir -p /web
useradd apache
chown -R apache.apache /web
chmod -R 755 /web

2.修改httpd.conf配置文件
修改如下几行:

Listen 81
User apache
Group apache
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
#配置虚拟主机
<VirtualHost x.x.x.x:81>
    ServerName test.cn
    DocumentRoot /web
    DirectoryIndex index.html
    <Directory "/web">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

注意:由于apache版本的问题,此处配置可能会不同
3.修改httpd-mpm.conf文件

vim /usr/local/apache/conf/extra/httpd-mpm.conf 
<IfModule mpm_event_module>
   ServerLimit  50
    ThreadLimit 100
    StartServers        50
    MinSpareThreads      50
    MaxSpareThreads      2000
    ThreadsPerChild      40
    MaxRequestWorkers    2000
    MaxConnectionsPerChild  2000
</IfModule>

更改完配置后,apache需要先stop再start,才能使参数生效,直接通过restart并不会使参数生效。

注意:根据服务器实际资源,对以上参数进行适当调整,过高或过低否则会出现内存耗尽或请求响应慢等情况。

5.启动apache并测试

/usr/local/apache/bin/apachectl -k stop
/usr/local/apache/bin/apachectl -k start

浏览器访问test.cn:81/index.html若出现访问内容,则说明程序部署成功。

配置nginx

1.创建相关目录

mkdir -p /usr/local/nginx1.6/conf.d
mkdir -p /data/nginx/cache/proxy_temp_dir
mkdir -p /data/nginx/cache/cache1
chown -R nginx.nginx /data/nginx/cache

2.修改主配置文件nginx.conf

    user  nginx;
    worker_processes  4;
    worker_rlimit_nofile 102400;
    error_log  logs/error.log error;
    pid       /var/run/nginx.pid;
    events {
    worker_connections  102400;
    use epoll;
    }
    http {
    include       mime.types;
    default_type  application/octet-stream;
        log_format main '$time_local - $upstream_addr $server_addr:$server_port ' '$request_method $uri $args ' '- $remote_addr $server_protocol [$http_user_agent] [$http_cookie] $http_referer ' '$host $status 0 0 $bytes_sent $request_length 0' '"$upstream_cache_status"';
    sendfile        on;
    keepalive_timeout  65;
    proxy_intercept_errors on;
    #gzip模块设置
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.0;
    #默认为1.1,nginx和后端upstream使用1.0
    gzip_comp_level 2;
    gzip_types       text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    #不要主动关闭客户端连接,防止返回499错误
    proxy_ignore_client_abort on;
    #client post body buffer
    client_body_buffer_size 512k;
    client_max_body_size 8m;
    #client header buffer
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    #open file 
    open_file_cache max=102400 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;
    #upstream proxy buffer      
    proxy_buffer_size 16k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_connect_timeout 300;
    proxy_read_timeout 300;
    proxy_send_timeout 300;
    #set cache
    proxy_temp_path   /data/nginx/cache/proxy_temp_dir;
    proxy_cache_path /data/nginx/cache/cache1 levels=2:2 keys_zone=cache1:1024m inactive=30m max_size=1950m;
    #client limit,限制客户端的连接数和访问频率,根据实际情况进行调整
    limit_req_zone  $binary_remote_addr  zone=two:10m   rate=50r/s;
    limit_conn_zone $binary_remote_addr  zone=one:10m;
    #返回403,404错误隐藏版本号
    server_tokens off;
    proxy_hide_header X-Mod-Pagespeed;
    proxy_hide_header Pragma;
    more_clear_headers 'X-Powered-By';
    more_set_headers 'Server: nginx';
    #配置后端服务器,用于负载均衡,可根据实际情况调整
    upstream backend {
    server x.x.x.x1:81  weight=5 max_fails=2 fail_timeout=10s;
    server x.x.x.x2:81 weight=5 max_fails=2 fail_timeout=10s;
    }
    include conf.d/*.conf;
}

3.添加站点配置文件

    vim /usr/lo cal/nginx1.6/conf.d/test.conf
    server {
        listen       80;
        #设置访问连接数及访问频率,可根据实际情况调整
        limit_conn one 20;
        limit_req zone=two burst=10 nodelay;
        server_name  test.cn;
        access_log  logs/access.test.log  main;
        location / {
                        proxy_set_header host $host;
                        proxy_set_header    X-Real-IP $remote_addr;
                        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
                        proxy_cache cache1;
                        proxy_cache_key $host$request_uri;
                        proxy_cache_valid 200 302 4m;
                        proxy_cache_valid 404 10m;
                        proxy_cache_valid 500 501 502 503 10s;
                        expires 1m;
                        proxy_pass http://backend;
        }
   }

4.检查配置文件、启动并访问测试

/usr/local/nginx1.6/nginx -t
/usr/local/nginx1.6/nginx
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值