Apache和PHP已经在系统里面预装好了,只要你开启即可使用。这篇文章给大家介绍如何开启并设置好PHP开发环境。 1.开启默认Apache服务 打开终端命令行,输入如下命令就会开启Apache了.然后输入top命令查看进程是否含有http。
|
1 |
sudo apachectl start |
2.修改Apache配置文件 在Finder右键点击“前往文件夹”后输入“/etc/apache2/ ”然后打开httpd.conf。我们要修改开启PHP组件和虚拟目录。 去掉在168、169行的#号
|
1 2 |
LoadModule rewrite_module libexec/apache2/mod_rewrite.so LoadModule php5_module libexec/apache2/libphp5.so |
211行前面加上#号
|
1 |
#Require all denied |
修改默认目录
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
DocumentRoot "/Users/wise/site/default" <Directory "/Users/wise/site/default"> # # Possible values for the Options directive are "None", "All", # or any combination of: # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews # # Note that "MultiViews" must be named *explicitly* --- "Options All" # doesn't give it to you. # # The Options directive is both complicated and important. Please see # http://httpd.apache.org/docs/2.4/mod/core.html#options # for more information. # Options FollowSymLinks Multiviews MultiviewsMatch Any # # AllowOverride controls what directives may be placed in .htaccess files. # It can be "All", "None", or any combination of the keywords: # AllowOverride FileInfo AuthConfig Limit # AllowOverride All # # Controls who can get stuff from this server. # Require all granted </Directory> |
虚拟目录,修改文件/etc/apache2/extra/httpd-vhosts.conf
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
#默认目录 <VirtualHost *:80> DocumentRoot "/Users/wise/site/default" ServerName default </VirtualHost> #工具 <VirtualHost *:80> DocumentRoot "/Users/wise/site/db" ServerName db </VirtualHost> #个人项目 <VirtualHost *:80> DocumentRoot "/Users/wise/site/jinzhe" ServerName jinzhe <Directory "/Users/wise/site/jinzhe"> Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "/Users/wise/site/yckit" ServerName yckit <Directory "/Users/wise/site/yckit"> Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "/Users/wise/site/dmku" ServerName dmku <Directory "/Users/wise/site/dmku"> Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> #私人项目 <VirtualHost *:80> DocumentRoot "/Users/wise/site/sms" ServerName sms </VirtualHost> <VirtualHost *:80> DocumentRoot "/Users/wise/site/icrobot" ServerName icrobot <Directory "/Users/wise/site/icrobot"> Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "/Users/wise/site/lbdlq" ServerName lbdlq <Directory "/Users/wise/site/lbdlq"> Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> <VirtualHost *:80> DocumentRoot "/Users/wise/site/baidu" ServerName baidu <Directory "/Users/wise/site/baidut"> Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> #公司项目 <VirtualHost *:80> DocumentRoot "/Users/wise/site/fishernuts" ServerName fishernuts <Directory "/Users/wise/site/fishernuts"> Options FollowSymLinks AllowOverride All Require all granted </Directory> </VirtualHost> |
3.修改HOSTS文件 在Finder右键点击“前往文件夹”后输入“/etc ”然后打开hosts。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#工具 127.0.0.1 db #个人项目 127.0.0.1 jinzhe 127.0.0.1 dmku 127.0.0.1 yckit 127.0.0.1 yckit.api #私单项目 127.0.0.1 sms 127.0.0.1 icrobot 127.0.0.1 lbdlq 127.0.0.1 baidu 127.0.0.1 jcm #公司项目 127.0.0.1 fishernuts |
4.安装MYSQL 到官网下载:http://dev.mysql.com/downloads/mysql/ 选DMG格式的就好了。他会帮你一键安装好。 复制出一份。php.ini
|
1 |
cp /etc/php.ini.default /etc/php.ini |
创建mysql socket(不设置这个无法连接phpmyadmin)
|
1 2 |
sudo mkdir /var/mysql sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock |
然后修改php.ini中的内容(原因是连接时候php默认去找/var/mysql/mysql.sock了,但是MAC版本的MYSQL改动了文件的位置,放在/tmp下了。)
|
1 |
mysql.default_socket = /tmp/mysql.sock |
设置别名和密码
|
1 2 3 |
alias mysql='/usr/local/mysql/bin/mysql' alias mysqladmin='/usr/local/mysql/bin/mysqladmin' mysqladmin -u root password "123456" |
from:http://jinzhe.net/post/32.html
View Details1. 安装PCRE Download latest PCRE. After download go to download directory from terminal. $ cd ~/Download $ tar xvzf pcre-8.12.tar.gz $ cd pcre-8.12 $ sudo ./configure --prefix=/usr/local $ sudo make $ sudo install 2. 安装Nginx Download latest nginx from Nginx.org. After download, let install $ cd ~/Download $ tar xvzf nginx-1.0.0.tar.gz $ cd nginx-1.0.0 $ sudo ./configure --prefix=/usr/local --with-http_ssl_module --with-ld-opt="-L /usr/local/lib" $ sudo make $ sudo make install 3. 运行Nginx $ cd /usr/local/sbin $ sudo ./nginx 访问http://localhost 4. 关闭Nginx $ cd /usr/local/sbin $ sudo […]
View Details就像其他Unix系统一样,Mac OS X的根目录也是/,Finder中的根目录就是硬盘目录,也就是Macintosh HD。新版本的OS X系统开始隐藏根目录,因为大多数用户不需要访问。下面为大家介绍四种快速访问根目录的方法。 1:通过“前往文件夹”快捷键组合 “前往文件夹”可能是OS X系统Finder中最常用的操作了,用户只需点击左上角的“前往”菜单,然后选择“前往文件夹…”功能。使用快捷键+Shift+G也可以快速激活“前往文件夹”功能,窗口打开后,输入“/”并回车即可。 2:将Macintosh HD快捷方式放入Finder侧边栏 对于经常要访问根目录的用户来说,将Macintosh HD快捷方式会更高效一些。在任何文件夹内(除了“我的所有文件”)右键点击标题栏,然后选择Macintosh HD。最后将Macintosh HD左侧的硬盘图标拖拽至侧边栏即可完成操作。以后需要访问根目录时,只需点击侧边栏的快捷方式即可。 3:在桌面上显示硬盘 打开Finder的“偏好设置”,在“通用”模式下在“硬盘”前面的方块内勾选即可。这样桌面上就会有Macintosh HD,双击即可访问。 4:使用命令行迅速导航 打开终端应用,然后输入下面的命令即可迅速导航至根目录。 cd / 复制代码 上面的命令是在终端中访问根目录,如果你想通过终端打开Finder的根目录,只需下面的命令即可: open / 复制代码 from:http://www.macx.cn/thread-2080254-1-1.html
View Details1. 验证/修复磁盘权限 在 应用程序/实用程序/磁盘工具.app 选择你的系统所在宗卷进行验证,如果有问题则修复(其实也可以直接点“验证并修复磁盘权限”反正修复之前必会先验证的)。 注意非系统宗卷默认是忽略权限的,自然没有验证和修复的说法。 2. 清除不需要的登录项目(就是登录的时候自动运行的程序) 系统预置 – 帐户 – 登录项目,自己看着办,把不需要登录项目减掉。 注意有一些程序即使去掉后还会自动再添加的,比如 Adobe Acrobat 的 AdobeResourceSynchronizer 等。 3. 清除不需要的应用程序 没什么好说的,一些不再用的应用程序可以删掉了,即占空间又碍眼。 4. 清除不需要的预置面板 在系统预置下面的列表中是一些第三方应用程序添加的面板,需要的可以删掉。直接点右键去掉,或者在“~/Library/PreferencePanes”里删除不需要的,可能需要重新启动或者强制清倒废纸篓。 5. 清理桌面 已经有相当多的用户报告说一个干净的桌面可以提升系统运行速度。 别把乱七八糟的东西留在桌面上,找个文件夹收拾起来,或者干脆删除了。 6. 清倒废纸篓 清倒废纸篓可以节省磁盘空间哦~~废话。 7. 如果不需要,就关掉万能辅助 很明显,开着万能辅助肯定会占用内存和处理器时间,关掉用不到的万能辅助选项吧,在系统预置-万能辅助里 8. 如果不需要,关掉蓝牙 开着蓝牙,即费内存又费电,不用的时候关掉吧。用无线MM鼠的用户请忽略。 9. 关掉语音识别 说实话,非英语用户其实开着也是浪费内存和处理器时间。 10. 关闭 Internet 共享 系统预置-共享-Internet,现在用路由器的多, Internet 共享不用就关掉吧。 11. 为启动宗卷(就是系统所在宗卷)预留足够个空闲空间 虚拟内存要用的,所以尽量保证启动宗卷上有10%的空闲空间。 12. 移除不需要的语言包 Monolingual 是个免费的移除语言包的好工具。一些你根本看不懂的语言包去掉就去掉吧,还能省空间。 注意:移除语言包是不可恢复的操作,如果出现问题,必须重装软件才能恢复。请小心决定。 13. 移除改变桌面效果的程序 最近我(指原文作者)发现一个不错的程序可以在每个月在我的桌面上放一个不同小孩,看起来很不错,但一次我在活动监视器里发现这个程序占用了大量了内存和处理器时间。 14. 清理 Dock 上不许要的应用程序 在 Dock 上只需要保留经常使用的应用程序就够了。 15. 为文件关联合适的应用程序 比如图片,如果没有特别要求,最好关联在“预览.app”上,关联在“Photoshop.app”会给每次连按图片文件造成很大的不便。 16. 检查应用程序的架构 如果你使用 Intel-based Mac ,那么用 Universal Binary 版的应用程序将比通过 Rosetta 运行 PowerPC 架构要快。 17. 关掉 Dock 的特效 18. 关掉桌面背景动画 […]
View DetailsWarning: file_put_contents(): Filename cannot be empty 在 file_unmanaged_save_data() (行 1937 在 D:\WEBSITE\dabeizhou.org\includes\file.inc). 文件无法创建。 Warning: file_put_contents(): Filename cannot be empty 在 file_unmanaged_save_data() (行 1937 在 D:\WEBSITE\dabeizhou.org\includes\file.inc). 文件无法创建。 解决方法: 在 管理 >> 配置 >> 媒体 >>文件系统 里面看看你的临时文件路径是什么,是不是有写权限。
View Details你可能已经听说过Windows 10会在默认情况下收集用户的数据,虽然微软声称这是为了提供个人化服务和体验,仍有不少用户对个人数据非常谨慎,不过我们已经发布了如何关闭 Windows 10自动搜索数据的方法。根据外媒on gHacks博文报道,最近微软面向Windows 7和Windows 8.1的新补丁也带来了一些搜集用户数据及追踪操作记录的功能。 涉及这些功能的更新包括KB3068708,KB3022345,KB3075249,KB3080149,均包含了会搜集用户数据的 Diagnostics and Telemetry分析与记录追踪服务,或者UAC自动记录点功能。如何做来关闭这些烦人的用户数据搜集选项?首先你可以避免安装这4个补丁,如果你已经 安装可以通过控制面板卸载这项补丁。或者进入命令行界面输入下列命令来卸载这项补丁。 wusa /uninstall /kb:3068708 /quiet /norestart wusa /uninstall /kb:3022345 /quiet /norestart wusa /uninstall /kb:3075249 /quiet /norestart wusa /uninstall /kb:3080149 /quiet /norestart 并且确保Windows Update更新机制中“隐蔽”这些特定的补丁,以免系统再次尝试下载安装。 from:http://www.oschina.net/news/65699/windows-7-8.1-patch-s-trick
View DetailsC++虽然主要是以C的基础发展起来的一门新语言 C++虽然主要是以C的基础发展起来的一门新语言,但她不是C的替代品,不是C的升级,C++和C是兄弟关系。没有谁比谁先进的说法,更重要的一点是C和C++各自的标准委员会是独立的,最新的C++标准是C++98,最新的C标准是C99.因此也没有先学C再说C++的说法,也不再(注意这个"不再")有C++语法是C语法的超集的说法。 C++/CLI 和 C# 是微软的 C++/CLI 和 C# 是微软的,它们与C和C++没有任何关系,虽然部分语法相似。但哪两种语言不相似呢?都是abc这26个字母。 不要使用TC/TC++/BC/CB等古老的编译器来学习C/C++ 不要使用TC/TC++/BC/CB等古老的编译器来学习C/C++,因为它们太古老了,不支持新的C/C++标准。不要使用CBX/VC++6.0/VC2005等对C/C++标准支持不好的编译器,虽然这些编译器适合工作,但不适合学习,因为它们中的语法陷阱很多。记住唯一适合学习的编译器是gcc/mingw.[antigloss注:Dev-C++ 使用的编译器就是gcc & g++] 不要用""代替<>来包含系统头文件 不要用""代替<>来包含系统头文件,虽然有些编译器允许你这样做,但它不符合C/C++标准。错误的示例:#include "stdio.h",#include "iostream".[antigloss注:习惯上,<> 用于包含标准头文件和系统头文件,"" 用于包含自定义头文件。标准似乎没有明确规定不准用 "" 包含标准头文件和系统头文件。使用 "" 包含标准头文件或者系统头文件只能说是一种不良风格。] 不要将main函数的返回类型定义为void 不要将main函数的返回类型定义为void,虽然有些编译器允许你这样做,但它不符合C/C++标准。不要将函数的int返回类型省略不写,在C++中要求编译器至少给一个警告。错误的示例:void main() {},main() {} [antigloss注:C99和C++98都要求编译器对省略int至少发出一个警告]如果不需要从命令行中获取参数,请用int main(void) ;否则请用int main( int argc, char *argv[] ) 不要把VC++中的 #include "stdafx.h" 贴出来 不要把VC++中的 #include "stdafx.h" 贴出来,它是预编译头文件。如同上菜时不要把厨师也放到托盘中。 [C++]不要#include <iostream.h> [C++]不要#include <iostream.h>,不要#include <string.h>,因为它们已经被C++标准明确的废弃了,请改为 #include <iostream> 和 #include <cstring>.规则就是: a. 如果这个头文件是旧C++特有的,那么去掉。h后缀,并放入std名字空间, 比如 iostream.h 变为 iostream. b. 如果这个头文件是C也有的,那么去掉。h后缀,增加一个c前缀,比如 string.h 变为 cstring;stdio.h 变为 cstdio, 等等。 BTW:不要把string、cstring、string.h三个头文件搞混淆 BTW:windows.h不是C/C++的标准文件,因此它的命名C/C++不管。
View Details在上一节中提到可以使用AuthorizeAttribute进行权限管理:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[Authorize] public ActionResult TestAuthorize() { return View(); } [Authorize(Users="test1,test2")] public ActionResult TestAuthorize() { return View(); } [Authorize(Roles="Admin")] public ActionResult TestAuthorize() { return View(); } |
但是通常情况下,网站的权限并不是固定不变的,当新增角色或者角色改变时,只能修改每个Action对应的特性,当项目较大时工作量可想而知。幸运的是我们可以重写AuthorizeAttribute达到自定义的权限管理。新建一个CustomAuthorizeAttribute类,使这个类继承于AuthorizeAttribute。打开AuthorizeAttribute查看下方法说明,我们只需要重写AuthorizeCore和OnAuthorization就能达到我们的目的。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// Summary: // When overridden, provides an entry point for custom authorization checks. // // Parameters: // httpContext: // The HTTP context, which encapsulates all HTTP-specific information about // an individual HTTP request. // // Returns: // true if the user is authorized; otherwise, false. // // Exceptions: // System.ArgumentNullException: // The httpContext parameter is null. protected virtual bool AuthorizeCore(HttpContextBase httpContext); // // Summary: // Called when a process requests authorization. // // Parameters: // filterContext: // The filter context, which encapsulates information for using System.Web.Mvc.AuthorizeAttribute. // // Exceptions: // System.ArgumentNullException: // The filterContext parameter is null. public virtual void OnAuthorization(AuthorizationContext filterContext); |
在CustomAuthorizeAttribute中重载AuthorizeCore方法,它的处理逻辑如下:首先判断当前账户是否被认证,如果没有,则返回false;然后获取当前账户的类型,并跟给定的类型进行比较,如果类型相同,则返回true,否则返回false。一般网站中权限管理都会使用权限树,然后将角色的权限保存至数据库或者文件中,本例中我们使用XML文件保存每个Action的角色,这样在用户请求Action时,由XML文件获取Action对应的权限,然后检测账户是否有相应的权限。CustomAuthorizeAttribute类的代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public class CustomAuthorizeAttribute : System.Web.Mvc.AuthorizeAttribute { public new string[] Roles { get; set; } protected override bool AuthorizeCore(HttpContextBase httpContext) { if (httpContext == null) { throw new ArgumentNullException("HttpContext"); } if (!httpContext.User.Identity.IsAuthenticated) { return false; } if (Roles == null) { return true; } if (Roles.Length == 0) { return true; } if (Roles.Any(httpContext.User.IsInRole)) { return true; } return false; } public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext) { string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; string actionName = filterContext.ActionDescriptor.ActionName; string roles = GetRoles.GetActionRoles(actionName, controllerName); if (!string.IsNullOrWhiteSpace(roles)) { this.Roles = roles.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); } base.OnAuthorization(filterContext); } } |
当用户请求一个Action时,会调用OnAuthorization方法,该方法中GetRoles.GetActionRoles(actionName, controllerName);根据Controller和Action去查找当前Action需要具有的角色类型,获得Action的Roles以后,在AuthorizeCore中与用户的角色进行比对Roles.Any(httpContext.User.IsInRole),如果没有相应权限则返回false,程序就会自动跳转到登录页面。 GetRoles为XML解析类,代码如下:
|
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class GetRoles { public static string GetActionRoles(string action, string controller) { XElement rootElement = XElement.Load(HttpContext.Current.Server.MapPath("/")+"ActionRoles.xml"); XElement controllerElement = findElementByAttribute(rootElement, "Controller", controller); if (controllerElement != null) { XElement actionElement = findElementByAttribute(controllerElement, "Action", action); if (actionElement != null) { return actionElement.Value; } } return ""; } public static XElement findElementByAttribute(XElement xElement,string tagName, string attribute) { return xElement.Elements(tagName).FirstOrDefault(x => x.Attribute("name").Value.Equals(attribute,StringComparison.OrdinalIgnoreCase)); } } |
相应的权限XMl文件:
|
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8" ?> <Roles> <Controller name="Home"> <Action name="Index"></Action> <Action name="About">Manager,Admin</Action> <Action name="Contact">Admin</Action> </Controller> </Roles> |
当需求发生变化时,只需要修改XML文件即可 使用时,只需要在FilterConfig中注册该filter
|
1 |
filters.Add(new CustomAuthorizeAttribute()); |
当然这只是一个简单的例子,实际应用中会复杂许多,还可能要实现在即的MemberShipProvider和RoleProvider from:http://www.cnblogs.com/jyan/archive/2012/07/24/2606646.html
View Details使用VS2010创建web应用程序时出现如下提示ASP.NET 4.0尚未在 Web 服务器上注册。为了使网站正确运行,可能需要手动将 Web 服务器配置为使用 ASP.NET 4.0,按 F1 可了解更多详细信息 解决方法: 首先设置IIS应用程序池 net framework版本为4.0 然后 开始->所有程序->附件->鼠标右键点击“命令提示符”->以管理员身份运行->%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i from:http://www.cnblogs.com/lvxiouzi/p/3511446.html
View Details由于接触到Python Web开发,正好把最简单的CGI方式研究了一下,话说在Windows下配置Python的Web开发还真的蛮麻烦的,Linux下配置倒挺容易,正好微软有技术文章《Using Python Scripts with IIS》介绍了这些内容,此文介绍了两种方法,一是使用ASP引擎来运行Python脚本,这个可能需要用到ActivePython,当然ASP技术已经过时了,我今天就简单介绍下CGI模块运行方式。 编写简单的支持CGI的Python脚本(本文介绍3.2版本的Python):
|
1 2 3 4 5 |
print("Status: 200 OK") print("Content-type: text/html") print() # 打印一行空白行,用于分隔HTTP Header和正文 print("<h1>Hello World!</h1>") |
这样就可以了,大家可以猜出CGI是将标准输出流重新定向到HTTP输出流来实现网页或者数据传输的。 当然这个在IIS中是不能直接运行的,我们需要配置一下,打开Internet 信息服务(IIS)管理器界面,选择“处理程序映射”。 在接下来出现的界面右侧选择“添加模块映射”。 假设我们的Python 3.2安装于C:\Python32,那么可以向下图这样填写: 然后点击确定,在接下来出现的对话框选择“是”。 好了,我们的配置完成了,重启一下IIS,然后赶快试试刚才的代码吧。可能有人会抱怨,用CGI编写网页一旦报错调试会比较麻烦,比如报下面的错误:
|
1 2 3 |
HTTP 错误 502.2 - Bad Gateway 指定的 CGI 应用程序由于未返回完整的一组 HTTP 头而产生错误行为。它实际返回的头是“Traceback (most recent call last): File "E:\projects\test.py", line 3, in <module> 1/0 ZeroDivisionError: division by zero ”。 |
其实我们只需要在最开始引入import cgitb; cgitb.enable()就可以了,就像下面这样:
|
1 2 3 4 5 6 |
import cgitb; cgitb.enable() print("Status: 200 OK") print("Content-type: text/html") print() # 打印一行空白行,用于分隔HTTP Header和正文 print("<h1>Hello World!</h1>") |
这样一旦出错,就会以友好的方式将错误输出来。 对于表单的处理,可以参考import cgi模块(cgi.FieldStorage),网上有很多此方面的介绍,我就不多说了,Enjoy it! from:http://wangye.org/blog/archives/684/
View Details