He visto dos formas de desarrollo de servidores en Mac:
1. El método de php;
Mac viene con apache y php, ¿cómo abrirlo?
1. Cómo usar apache:
puesta en marcha:
sudo apachectl inicio
Obtenga el número de versión:
sudo apachectl -v
cierre:
parada sudo apachectl
Reanudar:
sudo apachectl reiniciar
Escriba localhost en su navegador y verá ¡Funciona!
2. Cómo usar php:
Introduzca la línea de comando,
cd /etc/apache2/
abierto .
Luego puede ver el archivo httpd.conf, puede usar su herramienta de edición favorita, vi, vim o texto sublime, busque #LoadModule php5_module libexec/apache2/libphp5.so
Retire el signo # al frente.
En la línea de comando:
sudo cp /etc/php.ini.default /etc/php.ini
cp, este comando copia el archivo de origen especificado en el archivo de destino o copia varios archivos de origen en el directorio de destino.
Es decir, copie php.ini.default a php.ini. En este momento, puede modificar php.ini para modificar la función de configuración.
;Ajuste el valor máximo de los archivos PHP enviados a través de los siguientes dos elementos, como el valor máximo de los datos importados en phpMyAdmin
upload_max_filesize = 2M
post_max_size = 8M ;
Use display_errors para controlar si mostrar el mensaje de error del programa PHP, lo cual es muy útil al depurar el programa PHP
mostrar_errores = Desactivado
sudo apachectl reiniciar
Reinicie apache.
sudo cp /Library/WebServer/Documents/index.html.en /Library/WebServer/Documents/info.php
Copie el archivo index.html.en en el directorio raíz de Apache y cámbiele el nombre a info.php.
Si modifica este archivo info.php, agregue <?php phpinfo(); ?>
En este momento, use http://localhost/info.php para ver ¡Funciona! Agregue información de php más tarde.
3. Cómo usar mysql
Descargue mysql , descargué mysql-5.6.15-osx10.7-x86_64.dmg, instale e instale mysql-5.5.27-osx10.6-x86_64.pkg a su vez - MySQL, el paquete de instalación principal En general, la instalación archivo Instalará automáticamente MySQL en /usr/local
la carpeta con el mismo nombre que se muestra a continuación. Si ejecuta " mysql-5.5.27-osx10.6-x86_64.dmg
", instalará MySQL en " /usr/local/mysql-5.6.15-osx10.7-x86_64
".
Instale el segundo archivo, MySQLStartupItem.pkg , y MySQL se iniciará automáticamente en el momento del arranque.
Instale el tercer archivo MySQL.prefPane , verá el ICONO de "MySQL" en las "Preferencias del sistema". Puede controlar el cambio de mysql.
4. Cómo usar Apache
El directorio raíz a nivel de usuario de apache es: ~Sitios, si no, cree uno, puede usar la línea de comando para crear: sudo mkdir Sitios
Hay dos formas de configurar:
(1)/etc/apache2/users/下面看是否有“你的用户名.conf”这个文件没有的话就创建一个, ;
(2)/etc/apache2/httpd.conf修改这个文件中,
找到Directory,做如下修改,下面的是修改后的结果:
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
然后重启apache,就可以通过http://localhost/~username/可以访问用户级目录网页了。这个~username意思就是,如果你的开机用户名是liuyun,那么你就应该写成~liuyun。
现在 PHP 应该已经开始工作了,你可以在用户级根目录下(~/Sites/)放一个PHP测试文件,代码如下:
<?php phpinfo(); ?>浏览器中打开这个文件即可以看见php的信息了。
5.phpMyAdmin的使用方式
phpMyAdmin是用PHP开发的管理MySQL的程序,非常的流行和实用。下载以后,改名为“phpmyadmin”,然后可以放在用户级根目录(~/Sites)下也可以放在“/Library/WebServer/Documents/
”下.
(1)用户级(~/Sites)
调用http://localhost/~username/就可以通过浏览器访问你的用户级目录网页。
(2)系统级(/Library/WebServer/Documents/)
复制“/Library/WebServer/Documents/phpmyadmin/
”中的config.sample.inc.php,并命名为config.inc.php.
调用http://localhost/phpmyadmin/就可以进入phpmyadmin的管理页面了。
二、使用java的方式;
在这里是使用eclipse+tomcat+mysql的方式。
1、软件下载
(1) eclipse
我使用的是mac,因为本人是苹果客户端开发者,所以目前只有苹果机器了。首先下载eclipse for mac,选择mac 64位,下载eclipse-jee-kepler-SR1-macosx-cocoa-x86_64.tar.gz, 解压后可以看到eclipse图标,双击,这些都不用说了。
(2)tomcat
mac自带java,可以打开mac命令行,输入java -version, 我的是java version "1.6.0_65",所以我选择使用tomcat 7下载,如果java是1.7以后,可以选择tomcat 8.
apache-tomcat-7.0.47.tar解压后进入命令行
cd 你的tomcat路径/apache-tomcat-7.0.47/bin
./startup.sh 或者 sh startup.sh
关闭则使用shutdown.sh。
打开浏览器输入http://localhost:8080,这个时候就可以看到tomcat的页面了。webapps文件夹就是它的用户文件夹。
(3)mysql
上面已经写清楚,不用再说了。
2、使用方式
(1)打开eclipse;
(2)点击菜单Eclipse->Preferences...->Server->Runtime Environments
(3)点击Add。。。
创建生成tomcat环境。
(4)创建项目
菜单选择File-》New。。。-》Project-》Web-》Dynamic Web Project
输入Project Name,选择Target Runtime,然后可以next也可以直接finish。然后就可以看到项目已经生成。我在next之后修改了content directory为WebRoot,所以项目显示为如下:
在WebRoot下new一个index.jsp, 修改如下:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>First Web Project</title>
</head>
<body>
<center>Hello World!</center>
</body>
</html> -->
<form name="form1" action="index.jsp" method="post">
<br><br>
<input type="text" name="Vals">
<input type="text" name="Amount">
<input type="submit" name="Submit" value="Submit">
</form>
<%
int intLocal_Vals;
int intLocal_Amount;
if(request.getParameter("Vals") != null && request.getParameter("Amount") != null) {
intLocal_Vals = Integer.parseInt(request.getParameter("Vals"));
intLocal_Amount = Integer.parseInt(request.getParameter("Amount"));
intLocal_Vals=intLocal_Vals>>intLocal_Amount;
out.print("<br>位移后的值为:" +intLocal_Vals);
}
else {
out.print("位移值或位移量不能为空!");
}
%>
运行,
这时,出现
原来之前运行的tomcat还未关闭,只需要在命令行关闭之前的即可。
运行后默认是在eclipse嵌入方式显示,如果希望能在浏览器中显示,可以选择Window-》Web Browser,可以选择浏览器显示。如果不需要debug的话.
平常我们客户端都会有很多的接口,那么我们怎么实现接口的呢?下面就来详细介绍一下吧。
首先在项目里面创建一个servlet,
名字命名为HelloServlet,可以看到代码里面,
package com.example.servlets;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloServlet
*/
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
java.sql.Connection conn = null; //连接
try {
Class.forName("com.mysql.jdbc.Driver");
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","1234");
if(conn==null){
// out.println("get Conn Error");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //建立连接
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //驱动
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().write("Hello, world!");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().write("Hello, world post!");
}
}
参考资料:
http://dancewithnet.com/2010/05/09/run-apache-php-mysql-in-mac-os-x/#php
http://currentbottle.blog.163.com/blog/static/19429412820136263103728/