首页 > 代码库 > lamp 基础应用
lamp 基础应用
lamp 基础应用
========================================================================
概述:
Lamp介绍
★资源类型:
静态资源:
原始形式与响应给客户端的结果一致;服务端仅仅提供一些内容展示功能,如网页内容展示,图片列表,动画播放等,服务端不需要和客户端进行互动,所有的资源可以直接去获取,但是无法进行资源上传;
动态资源:
原始形式通常为程序文件(为某种编程语言开发),需要运行后将生成的结果展示给客户端;即:服务端可以与客户端进行互动,如一些论坛等,这些web程序是由web程序来开发的,常见的如PHP程序配合MySql数据库进行信息的检索与提交写入等操作。
客户端技术:javascript
服务端技术:php, jsp, ...
★CGI:Common Gateway Interface 通用网关接口协议
CGI是一种协议,定义了客户端(web服务器程序)与服务端(特定的应用程序服务进程)进行数据交换的一种规范;
★程序:指令+数据
指令:代码
数据:存储
文件系统:单机文件,分布式文件系统;
DBMS:数据库管理系统
SQL:关系数据库
Oracle,
SQL Server:
DB2,
MySQL,
PostgreSQL(PGSQL),
MariaDB,
Percona-Server, ...
NoSQL:非关系型数据库
KV:redis, ...
Document:MongoDB, ...
Column:HBase, ...
Graph:
★开源领域
amp:httpd+php/perl/python+mysql/mongodb
ams:httpd+tomcat+mysql/mongodb
jsp:tomcat, jetty, resin, jboss, websphere, weblogic
php
★PHP
编程语言,嵌入式编程语言,
高度模块化(extensions),
配置文件(/etc/php.ini, /etc/php.d/*.ini);
PHP是通用服务器端脚本编程语言,主要基于web开发以实现动态web页面,它也是最早实现将脚本嵌入HTML源码文档中的服务器端脚本语言之一。同时php还提供了一个命令行接口,因此,其也可以在大多数系统上作为一个独立的shell来使用。
php应用程序:
开源代表:wordpress, discuzX, phpwind, drupal...
<html> ... <?php code ?> ... </html>★httpd+php:三种结合方式
CGI 不常用
Module 模块机制
prefork:php模块为:libphp
worker, event:php模块为:libphp-zts
注意:
使用模块化的方式是httpd和php结合的最好方式;在使用之前要首先判断httpd用的是那个模块(MPM),再才能决定安装哪个程序包。
FastCGI
php以fpm机制独立地监听在一个套接字上;
工作模式类似于httpd的prefork
演示如下:
[root@centos7 ~]# yum install php -y # 安装php [root@centos7 ~]# rpm -ql php /etc/httpd/conf.d/php.conf /etc/httpd/conf.modules.d/10-php.conf /usr/lib64/httpd/modules/libphp5.so /usr/share/httpd/icons/php.gif /var/lib/php/session [root@centos7 ~]# systemctl reload httpd # 重启httpd [root@centos7 ~]# vim /var/www/html/phpinfo.php # 编辑文件,提供一个php页面 1 <?php 2 phpinfo() 3 ?>
浏览器查看如下:
MysQL
1)相关介绍
★插件式存储引擎
★C/S架构的
Server:mysqld, mysqld_safe, mysqld_multi
Client:mysql
★安装Mysql
CentOS 6: mysql-server, mysql
CentOS 7: mariadb-server, mariadb
★MysQL版本:
Community Edtion
Enterprise Ddtion
CentOS 6使用的是Orecle收购sun之前的版本为5.1
额外添加的配置项:
[mysqld]
...
skip_name_resolve
innodb_file_per_table=ON
★MariaDB:
CentOS 7 使用的就是mariad
配置文件:/etc/my.cnf, /etc/my.cnf.d/*.cnf
额外添加的配置项:
[mysql]
skip_name_resolve = ON 跳过主机名解析
innodb_file_per_table = ON
默认连接的主机为当前主机,管理员用户为root,密码为空;
首次安装后建议使用:
mysql_secure_installation命令进行安全设定;
★客户端连接mysql server:
mysql --> mysql protocol --> mysql server
语法:mysql [options] db_name
选项:-hHOST
-uUSERNAME
-pPASSWORD
★mysql的用户账号:username@host
username:用户名
host:此用户可通过哪些客户端主机登录当前服务器上的mysql服务;主机名和IP地址属性于不同的主机;
host可使用通配符:
_:任意单个字符;
%:任意长度以的任意字符;
举例:root@‘10.1.%.%‘ 表示10.1网络段的任意主机都可登录
★mysql> 提示符下可接受输入mysql命令,分两类
客户端命令
help可获取命令列表
服务端命令:SQL语句,必须使用语句结束符,默认为分号;
DDL(数据定义语言):CREATE, ALTER, DROP
DML:INSERT, DELETE, UPDATE, SELECT(增,删,改,查)
GRANT/REVOKE (授权和取消授权)
授权命令:
★数据库的基本操作:
CREATE DATABASE db_name; 创建数据库
DROP DATABASE db_name; 删除数据库
演示如下:
mysql配置启动
[root@CentOS6 ~]# vim /etc/my.cnf 1 [mysqld] 2 datadir=/var/lib/mysql 3 socket=/var/lib/mysql/mysql.sock 4 user=mysql 5 # Disabling symbolic-links is recommended to prevent assorted sec urity risks 6 symbolic-links=0 7 skip_name_resolve # 添加项 8 innodb_file_per_table=ON # 添加项 9 10 [mysqld_safe] 11 log-error=/var/log/mysqld.log 12 pid-file=/var/run/mysqld/mysqld.pid [root@CentOS6 ~]# service mysqld start # 启动
MariaDB配置启动
[root@centos7 ~]# vim /etc/my.cnf # 编辑配置文件 1 [mysqld] 2 datadir=/var/lib/mysql 3 socket=/var/lib/mysql/mysql.sock 4 # Disabling symbolic-links is recommended to prevent assorted sec urity risks 5 symbolic-links=0 6 # Settings user and group are ignored when systemd is used. 7 # If you need to run mysqld under a different user or group, 8 # customize your systemd unit file for mariadb according to the 9 # instructions in http://fedoraproject.org/wiki/Systemd 10 skip_name_resolve = ON # 添加左边这两行 11 innodb_file_per_table = ON 12 13 [mysqld_safe] 14 log-error=/var/log/mariadb/mariadb.log 15 pid-file=/var/run/mariadb/mariadb.pid 16 17 # 18 # include all files from the config directory 19 # 20 !includedir /etc/my.cnf.d # 保存退出,启动mariadb [root@centos7 ~]# systemctl start mariadb [root@centos7 ~]# ss -tnl # 查看监听端口3306 State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 *:3306 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 127.0.0.1:631 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 127.0.0.1:6010 *:* LISTEN 0 128 127.0.0.1:6011 *:* LISTEN 0 128 :::8080 :::* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 128 ::1:631 :::* LISTEN 0 128 :::8088 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 ::1:6010 :::* LISTEN 0 128 :::443 :::* LISTEN 0 128 ::1:6011 :::* [root@centos7 ~]# mysql # 现在可以直接使用mysql命令即连接到mysql服务器上 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> Ctrl-C -- exit! Aborted
首次安装进行安全设定:
[root@centos7 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 6 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [mysql]> SELECT user,host,password FROM user; +------+-----------+----------+ | user | host | password | +------+-----------+----------+ | root | localhost | | # 可以看到root用户只能基于本地登录,且密码为空 | root | centos7 | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | centos7 | | +------+-----------+----------+ 6 rows in set (0.00 sec) MariaDB [mysql]> EXIT Bye # 安全设定如下: [root@centos7 ~]# mysql_secure_installation /usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we‘ll need the current password for the root user. If you‘ve just installed MariaDB, and you haven‘t set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): # 按回车键即可 OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y # 要不要给管理员设定密码 New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y # 要不要删除匿名用户 ... Success! Normally, root should only be allowed to connect from ‘localhost‘. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] n #要不要禁止管理员远程登录 ... skipping. By default, MariaDB comes with a database named ‘test‘ that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] n # 要不要删除测试数据库 ... skipping. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y # 要不要重载权限授权表 ... Success! Cleaning up... All done! If you‘ve completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB! [root@centos7 ~]# mysql # 直接登录发现已经登陆不进去了 ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO) [root@centos7 ~]# mysql -p # 输入密码才可访问 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 15 Server version: 5.5.44-MariaDB MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. MariaDB [(none)]> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [mysql]> SELECT user,host,password FROM user; +------+-----------+-------------------------------------------+ | user | host | password | +------+-----------+-------------------------------------------+ | root | localhost | *41EE0F8759D5340036B009143E1727DB5787A448 | | root | centos7 | *41EE0F8759D5340036B009143E1727DB5787A448 | | root | 127.0.0.1 | *41EE0F8759D5340036B009143E1727DB5787A448 | | root | ::1 | *41EE0F8759D5340036B009143E1727DB5787A448 | +------+-----------+-------------------------------------------+ 4 rows in set (0.00 sec) MariaDB [mysql]> EXIT Bye
1)如上,amp都有了,但是要想php连接mysql还需要安装一个专用的驱动
[root@centos7 ~]# yum info php-mysql Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Installed Packages Name : php-mysql Arch : x86_64 Version : 5.4.16 Release : 36.el7_1 Size : 232 k Repo : installed From repo : CDROM Summary : A module for PHP applications that use MySQL databases URL : http://www.php.net/ License : PHP Description : The php-mysql package contains a dynamic shared object that will add : MySQL database support to PHP. MySQL is an object-relational database : management system. PHP is an HTML-embeddable scripting language. If : you need MySQL support for PHP applications, you will need to install : this package and the php package. [root@centos7 ~]# yum install php-mysql -y [root@centos7 ~]# rpm -ql php-mysql /etc/php.d/mysql.ini /etc/php.d/mysqli.ini /etc/php.d/pdo_mysql.ini /usr/lib64/php/modules/mysql.so /usr/lib64/php/modules/mysqli.so /usr/lib64/php/modules/pdo_mysql.so # php增加模块了,所以需要重启php,因为php是装在httpd之上的,所以要重启httpd服务; #注意:实际生产环境中服务是不能随便重新启动的,这些要在要在我们部署完成后上线之前做好 [root@centos7 ~]# systemctl restart httpd测试如下:
[root@centos7 ~]# vim /var/www/html/php-mysql.php 1 <?php 2 $conn = mysql_connect(‘127.0.0.1‘,‘root‘,‘134296‘); 3 if ($conn) 4 echo ‘success‘; 5 else 6 echo ‘failure‘; [root@centos7 ~]# ls /var/www/html admin lastlog.txt messages.txt phpinfo.php php-mysql.php test.html浏览器访问如下:
关闭服务器 # systemctl stop mariadb,再访问
如上,lamp整个环境部署成功
lamp环境快速部署
★CentOS 7:
# yum install mariadb-server httpd php php-mysql
# systemctl start httpd.service mariadb.service
★CentOS 6:
# yum install httpd php php-mysql mysql-server
# service httpd start
# service mysqld start
★php应用程序:
开源代表:wordpress, discuzX, phpwind, drupal...
如下:应用程序部署在lamp环境中
lamp 基础应用