MySQL服务
安装服务 yum -y install ncurses-devel yum -y install cmake yum -y install bison groupadd mysql useradd -g mysql mysql cd /lamp/mysql-5.5.48 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -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 ---------------------------------------------------------- 配置服务 cd /usr/local/mysql/ chown -R mysql . chgrp -R mysql . #修改mysql目录权限 /usr/local/mysql/scripts/mysql_install_db --user=mysql #创建数据库授权表,初始化数据库 chown -R root . chown -R mysql data #修改mysql目录权限 cp support-files/my-medium.cnf /etc/my.cnf #复制mysql配置文件 二次授权 /usr/local/mysql/scripts/mysql_install_db --user=mysql ------------------------------------------------- 启动MySQL服务: 1.用原本源代码的方式去使用和启动mysql /usr/local/mysql/bin/mysqld_safe --user=mysql & 2.重启以后还要生效: vim /etc/rc.local /usr/local/mysql/bin/mysqld_safe --user=mysql & 3.设定mysql密码 /usr/local/mysql/bin/mysqladmin -uroot password 123456 清空历史命令 history -c /usr/local/mysql/bin/mysql -u root -p mysql> \s 查看配置 mysql> quit 退出
安装PHP
yum -y install "libtool*" cd /lamp/php-7.0.7 ./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc/ --with-apxs2=/usr/local/apache2/bin/apxs --with-libxml-dir=/usr/local/libxml2/ --with-jpeg-dir=/usr/local/jpeg6/ --with-png-dir=/usr/local/libpng/ --with-freetype-dir=/usr/local/freetype/ --with-mcrypt=/usr/local/libmcrypt/ --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-soap --enable-mbstring=all --enable-sockets --with-pdo-mysql=/usr/local/mysql --with-gd --without-pear make && make install ------------------------------------------------------- 生成php.ini mkdir /usr/local/php/etc/ cp /lamp/php-7.0.7/php.ini-production /usr/local/php/etc/php.ini vim /usr/local/apache2/etc/httpd.conf AddType application/x-httpd-php .php .phtml AddType application/x-httpd-php-source .phps 重启Apache服务:/usr/local/apache2/bin/apachectl stop /usr/local/apache2/bin/apachectl start vim /usr/local/apache2/htdocs/test.php <?php phpinfo(); ?> 测试 浏览器 192.168.2.43/test.php
添加环境变量 vim /etc/profile export PATH="/usr/local/php/bin:$PATH" export PATH="/usr/local/mysql/bin:$PATH" export PATH="/usr/local/apache2/bin:$PATH" source /etc/profile php -v mysql -u root -p apachectl -v
收尾
安装openssl yum -y install openssl-devel 必须安装 cd /lamp/php-7.0.7/ext/openssl mv config0.m4 config.m4 否则报错:找不到config.m4 /usr/local/php/bin/phpize ./configure --with-openssl --with-php-config=/usr/local/php/bin/php-config make make install vim /usr/local/php/etc/php.ini 722 extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20151012/" #打开注释,并修改 extension="openssl.so"; apachectl stop apachectl start
安装phpMyAdmin cp -r /lamp/phpMyAdmin-4.1.4-all-languages /usr/local/apache2/htdocs/phpmyadmin cd /usr/local/apache2/htdocs/phpmyadmin cp config.sample.inc.php config.inc.php vim config.inc.php //$cfg['Servers'][$i]['auth_type'] = 'cookie'; $cfg['Servers'][$i]['auth_type'] = 'http'; 测试 浏览器 192.168.2.43/phpmyadmin/index.php root 123456
安装memcache服务
1. Linux下安装操作: 1.1 #安装memcache扩展模块 unzip pecl-memcache-php7.zip cd pecl-memcache-php7 /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_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20151012/" extension="memcache.so"; #重启apache,通过浏览器 在phpinfo中可以找到这个模块 1.2 #安装缓存服务 memcached-1.4.4-3.el6.i686.rpm mount /dev/sr0 /mnt/cdrom/ 挂载ISO镜像文件 yum -y install memcached useradd memcache 添加memcache用户 memcached -d -m 128 -l 127.0.0.1 -p 11211 -u memcache 启动服务 chkconfig memcached on 设置开机自启动
Apache服务器配置
一 简介 1 www:world wide web http 协议: 超文本传输协议 HTML语言: 超文本标识语言 2 URL:统一资源定位 协议+域名:端口+网页文件名 http://www.sina.com.cn:80/admin/index.html 二 安装 1、lamp源码安装 2、二进制包安装 yum安装 httpd mysql mysql-server php php-devel php-mysql 三 相关文件 apache配置文件 源码包安装:/usr/lcoal/apache2/etc/httpd.conf /usr/local/apache2/etc/extra/*.conf 二进制包安装:/etc/httpd/conf/httpd.conf 默认网页保存位置: 源码包:/usr/local/apache2/htdocs/ 二进制包安装:/var/www/html/ 日志保存位置 源码包:/usr/local/apache2/logs/ 二进制包: /var/log/httpd/ 二进制包默认使用日志处理程序 /var下都会轮替 源码包才 需要设置 日志处理: 1日志切割 apache自带日志里面自带日志切割 2日志轮替 linux自带日志管理logrorate.conf 加入/usr/local/apache2/logs/access_log{ daily rotate 30 } logrotate -f /etc/logrotate.conf 所有日志都要进行日i www! he htdocs bu yiyang! 志轮替 四 配置文件 vim /root/.bashrc alias sta=’/usr/local/apache2/bin/apachectl start’ alias sto=’/usr/local/apache2/bin/apachectl stop’ source /root/.bashrc 注意:apache配置文件严格区分大小写 1 针对主机环境的基本配置 31 ServerRoot apache主目录 52 Listen 监听端口 LoadModule 加载的相关模块 User Group 用户和组 ServerAdmin 管理员邮箱 ServerName 服务器名(没有域名解析时,使用临时解析) ErrorLog "logs/error_log 错误日志 CustomLog "logs/access_log" common 正确访问日志 DirectoryIndex index.html index.php 默认网页文件名,优先级顺序 Include etc/extra/httpd-vhosts.conf 子配置文件中内容也会加载生效 2 主页目录及权限 DocumentRoot "/usr/local/apache2//htdocs" 主页目录 <Directory "/usr/local/apache2//htdocs"> #Directory关键字定义目录权限 Options Indexes FollowSymLinks #options None:没有任何额外权限 All: 所有权限 Indexes:浏览权限(当此目录下没有默认网页文件时,显示目录内容) FollowSymLinks:准许软连接到其他目录 AllowOverride None #定义是否允许目录下.htaccess文件中的权限生效 None:.htaccess中权限不生效 All:文件中所有权限都生效 AuthConfig:文件中,只有网页认证的权限生效。 Require all granted 访问控制列表 403错误 404错误 网页找不见 3 目录别名 扩展网站目录,增加服务器 子配置文件名 etc/extra/httpd-autoindex.conf Alias /www/ "/usr/local/apache2//www/" <Directory "/usr/local/apache2//www"> Options Indexes MultiViews AllowOverride None Require all granted </Directory> mkdir /usr/local/apache2/www/ 浏览器测试 http://192.168.1.253/www/ 4 虚拟主机 1)分类 基于IP的虚拟主机: 一台服务器,多个IP,搭建多个网站 基于端口的虚拟主机:一台服务器,一个ip,搭建多个网站,每个网络使用不同端口访问 基于名字的虚拟主机: 一台服务器,一个ip,搭建多个网站,每个网站使用不同域名访问 2)步骤: ① 解析试验域名 www.sina.com www.sohu.com C:\WINDOWS\system32\drivers\etc\hosts ② 规划网站主目录 /share/sina--------------www.sina.com /share/sohu ------------ www.sohu.com ③ 修改配置文件 vim /usr/local/apache2/etc/httpd.conf Include etc//extra/httpd-vhosts.conf #打开虚拟主机配置文件 vim /usr/local/apache2/etc/extra/httpd-vhosts.conf <Directory "/share/sina"> Options Indexes AllowOverride None Require all granted </Directory> <Directory "/share/sohu"> Options Indexes AllowOverride None Require all granted </Directory> <VirtualHost 192.168.150.253> #注意,只能写ip ServerAdmin webmaster@sina.com #管理员邮箱 DocumentRoot "/share/sina" #网站主目录 ServerName www.sina.com #完整域名 ErrorLog "logs/sina-error_log" #错误日志 CustomLog "logs/sina-access_log" common #访问日志 </VirtualHost> <VirtualHost 192.168.150.253> ServerAdmin webmaster@sohu.com DocumentRoot "/share/sohu" ServerName www.sohu.com ErrorLog "logs/sohu.com-error_log" CustomLog "logs/sohu.com-access_log" common </VirtualHost> 5 rewrite 重写功能 在URL中输入一个地址,会自动跳转为另一个 1)域名跳转 www.sina.com ------> www.sohu.com 开启虚拟主机,并正常访问 [root@localhost ~]# vim /usr/local/apache2/etc/httpd.conf LoadModule rewrite_module modules/mod_rewrite.so #打开重写模块,记得重启apache 修改配置文件,使sina目录的.htaccess文件生效 [root@localhost etc]# vim /usr/local/apache2/etc/extra/httpd-vhosts.conf <Directory "/share/sina"> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> vim /share/sina/.htaccess RewriteEngine on #开启rewrite功能 RewriteCond %{HTTP_HOST} www.sina.com 把以www.sina.com 开头的内容赋值给HTTP_HOST变量 RewriteRule .* http://www.sohu.com .* 输入任何地址,都跳转到http://www.sohu.com 2)网页文件跳转 vim /share/sina/.htaccess RewriteEngine on RewriteRule index(\d+).html index.php?id=$1 #输入index(数值).html时,跳转到index.php文件,同时把数值当成变量传入index.php
Apache执行停止改名
命令别名 alias vim /root/.bashrc alias sto='/usr/local/apache2/bin/apachectl stop' alias sta='/usr/local/apache2/bin/apachectl start' source /root/.bashrc sto sta
目录别名 扩容 增加服务目录
1)建立扩容目录 mkdir /usr/local/apache2/www/ 2)修改主配置文件 vim /usr/local/apache2/etc/httpd.conf 453 Include etc//extra/httpd-autoindex.conf 3)修改子配置文件 vim /usr/local/apache2/etc/extra/httpd-autoindex.conf 29 Alias /www/ "/usr/local/apache2/www/" 30 31 <Directory "/usr/local/apache2/www/"> 32 Options Indexes 33 AllowOverride None 34 Require all granted 35 </Directory> 4)重启服务 测试 sto sta 测试 192.168.2.251/www/
虚拟主机
步骤:1)域名解析 文件解析 C:\Windows\System32\drivers\etc\hosts 添加下面两个网址 192.168.2.251 www.sina.com 192.168.2.251 www.sohu.com 2)网站目录规划 (想显示内容要在里面创建index.html ,里面写入中文会乱码,英文正常显示) mkdir -p /share/sina/ mkdir /share/sohu/ 3)修改主配置文件 vim /usr/local/apache2/etc/httpd.conf 465 Include etc//extra/httpd-vhosts.conf 4)修改子配置文件 vim /usr/local/apache2/etc/extra/httpd-vhosts.conf 23 <Directory "/share/sina/"> 24 Options Indexes 25 AllowOverride None 26 Require all granted 27 </Directory> 28 29 <Directory "/share/sohu/"> 30 Options Indexes 31 AllowOverride None 32 Require all granted 33 </Directory> 35 <VirtualHost 192.168.2.251> 36 ServerAdmin webmaster@sina.com 37 DocumentRoot "/share/sina/" 38 ServerName www.sina.com 39 ErrorLog "logs/sina-error_log" 40 CustomLog "logs/sina-access_log" common 41 </VirtualHost> 42 43 <VirtualHost 192.168.2.251> 44 ServerAdmin webmaster@sohu.com 45 DocumentRoot "/share/sohu/" 46 ServerName www.sohu.com 47 ErrorLog "logs/sohu-error_log" 48 CustomLog "logs/sohu-access_log" common 49 </VirtualHost> 5)重启服务 测试 sto sta 测试 www.sina.com www.sohu.com
重定向
rewrite 重写/重定向 www.sina.com -> www.sohu.com 1)修改主配置文件 vim /usr/local/apache2/etc/httpd.conf 147 LoadModule rewrite_module modules/mod_rewrite.so 2)修改子配置文件 vim /usr/local/apache2/etc/extra/httpd-vhosts.conf 23 <Directory "/share/sina/"> 24 Options Indexes FollowSymLinks 25 AllowOverride All 26 Require all granted 27 </Directory> 3)建立权限文件.htaccess vim /share/sina/.htaccess 1 RewriteEngine on 2 RewriteCond %{HTTP_HOST} www.sina.com 3 RewriteRule .* http://www.sohu.com .* 代表任意所有,要从上往下看,这里.*代表sina.com 4) 重启服务 测试 sto sta 测试 www.sina.com -> www.sohu.com
网页跳转
1)修改权限文件 vim /share/sina/.htaccess 1 RewriteEngine on 2 RewriteRule index(\d+).html index.php?id=$1 2)建立index.php文件 vim /share/sina/index.php 1 <?php 2 echo "hello rewrite!"; 3 ?> 3)重启服务测试 sto sta 测试 www.sina.com/index5.html
LNMP环境搭建-Nginx服务
LNMP环境搭建
准备工作: 恢复初始化安装 设置IP 关闭防火墙 配置光盘yum源 安装步骤:1.解压缩 tar -zxvf lnmp1.2-full.tar.gz ls 2.进入解压目录 cd lnmp1.2-full 3.安装 ./install.sh lnmp 环境目录和文件: Nginx 目录: /usr/local/nginx/ MySQL 目录 : /usr/local/mysql/ MySQL数据库所在目录:/usr/local/mysql/var/ PHP目录 : /usr/local/php/ PHPMyAdmin目录 : /home/wwwroot/default/phpmyadmin/ 默认网站目录 : /home/wwwroot/default/ Nginx日志目录:/home/wwwlogs/ Nginx主配置文件:/usr/local/nginx/conf/nginx.conf MySQL配置文件:/etc/my.cnf PHP配置文件:/usr/local/php/etc/php.ini 管理命令 lnmp start | restart | stop | status lnmp nginx start | stop | restart | status 配置文件 ulimit -a ulimit -n 51200 检查nginx配置文件语句错误 /usr/local/nginx/sbin/nginx -t 平滑重启nginx进程 pkill -HUP nginx
虚拟主机
1)域名解析 文件解析 C:\Windows\System32\drivers\etc\hosts 192.168.2.251 www.sina.com 192.168.2.251 www.sohu.com 2)网站目录规划 mkdir /home/wwwroot/sina/ mkdir /home/wwwroot/sohu/ vim /home/wwwroot/sina/index.html vim /home/wwwroot/sohu/index.html 3)修改配置文件 vim /usr/local/nginx/conf/nginx.conf 66 listen 80; 4)建立虚拟主机文件 vim /usr/local/nginx/conf/vhost/v.conf 1 server { 2 listen 80; 3 server_name www.sina.com; 4 index index.html index.htm index.php; 5 root /home/wwwroot/sina/; 6 7 include enable-php.conf; 8 } 9 server { 10 listen 80; 11 server_name www.sohu.com; 12 index index.html index.htm index.php; 13 root /home/wwwroot/sohu/; 14 15 include enable-php.conf; 16 } 5) 重启服务 测试 pkill -HUP nginx 测试 www.sina.com www.sohu.com
列表页显示
vim /usr/local/nginx/conf/vhost/v.conf 加入 autoindex on; 开启列表页显示功能 (两个里面都加) cd /home/wwwroot/sina mv index.html a.html(需要改名)
状态监控模块添加
vim /usr/local/nginx/conf/vhost/v.conf 9 server { 10 listen 80; 11 server_name www.sohu.com; 12 index index.html index.htm index.php; 13 root /home/wwwroot/sohu/; 14 location /nginx_status{ stub_status on; access_log off; } 15 include enable-php.conf; 16 } 测试 www.sohu.com/nginx_status
rewrite重写、重定向
域名跳转 www.sina.com -> www.sohu.com vim /usr/loca/nginx/conf/vhost/v.conf server { listen 80; server_name www.sina.com; index index.html index.htm index.php; root /home/wwwroot/sina; if ($http_host = www.sina.com) { rewrite (.*) http://www.sohu.com permanent; } } 重启服务 测试 pkill -HUP nginx 测试 www.sina.com -> www.sohu.com
网页文件跳转
1)修改配置文件 vim /usr/local/nginx/conf/vhost/v.conf 1 server { 2 listen 80; 3 server_name www.sina.com; 4 index index.html index.htm index.php; 5 root /home/wwwroot/sina/; 6 rewrite index(\d+).html /index.php?id=$1 last; 7 include enable-php.conf; 8 } 2)建立 php文件 vim /home/wwwroot/sina/index.php 1 <?php 2 echo "hi Nginx rewrite"; 3 ?> 3)重启服务 测试 pkill -HUP nginx 测试 www.sina.com/index5.html
代理负载均衡(反向代理)
准备工作: S 192.168.2.251 Nginx 负载均衡 S1 192.168.2.102 Nginx 网站解析 S2 192.168.2.191 Nginx 网站解析 实验步骤: 1)修改S 192.168.2.251 配置文件 vim /usr/local/nginx/conf/nginx.conf 63 upstream myweb1 { 64 server 192.168.2.102:80; 65 server 192.168.2.191:80; 66 } 67 server { 68 listen 80; 69 server_name www.sohu.com; 70 location / { 71 proxy_pass http://myweb1; 72 proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header; 73 proxy_set_header Host $host; 74 proxy_set_header X-Forwarded-For $remote_addr; 75 } 76 } 77 } 2)修改S1 192.168.2.102 关闭虚拟主机 关闭虚拟主机: cd /usr/local/nginx/conf/ vim nginx.conf # include vhost/*.conf; 检测这个有没有注释或者删了就行 cd /home/wwwroot/default/ vim index.html S111111111111 3)修改S1 192.168.2.191 关闭虚拟主机 cd /home/wwwroot/default/ vim index.html S2222222222 4) 重启S 服务 测试 pkill -HUP nginx 测试 www.sohu.com 刷新 S1 S2