search
HomeBackend DevelopmentPHP Tutorial5.1 网络基础->1.2.1 配置Apache服务器和PHP(Mac 10.10以上)

本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末

为什么用 “Apache":
1.  使用最广的 Web 服务器、IIS(微软 Intenet Infomation Server)2.  Mac自带,只需要修改几个配置就可以,简单,快捷3.  有些特殊的服务器功能,Apache都能很好的支持(例如:HTTP PUT/DELETE 操作,HTTPS 服务)

步骤如下:
1、在 Finder 中 /Users/用户名 下创建一个 Sites 的文件夹,用于存放网页等文件(不能放在桌面或文档等个人文件夹)
2、打开【终端】
//切换工作目录cd /etc/apache2
3、备份文件:sudo 表示使用管理员权限执行命令,以保护系统文件上会被破坏,使用sudo需要密码
// cp (copy 的缩写)   httpd.conf (源文件)   httpd.conf.bak (目标文件)sudo cp httpd.conf httpd.conf.bak//提示:如果后续操作出现错误!可以使用以下命令,恢复备份过的 httpd.conf 文件sudo cp httpd.conf.bak httpd.conf
4、编辑 httpd.conf:修改配置文件中的"两个路径",指向刚刚创建的文件
//用vim编辑httpd.conf (vim里面只能用键盘,不能用鼠标)sudo vim httpd.conf//查找`DocumentRoot`/DocumentRoot//进入编辑模式i//修改`两处`引号中的路径DocumentRoot "/Users/用户名/Sites"<Directory "/Users/用户名/Sites">//继续向下,找到 Options FollowSymLinks Multiviews//加一个单词 `Indexes`,修改后的结果如下:Options Indexes FollowSymLinks Multiviews//返回命令模式ESC
5、Mac系统默认已经安装php,只是没有激活,要使用php需要修改apache的配置文件
//查找php/php//将光标移动到首行0//删除行首注释 #x//保存并退出:wq
6、拷贝php.ini文件
//切换工作目录cd /etc//etc 目录有点类似于 windows/system32,存放配置文件的目录sudo cp php.ini.default php.ini//执行完后,ls 查看目录,有php.ini和php.ini.default两个文件//重新启动apache服务器//执行完后,看到下面有AHXXX,表示成功 (如: AH00558)sudo apachectl -k restart如果提示以下错误是正常的:httpd: Could not reliably determine the server's fully qualified domain name, using teacher.local for ServerNamehttpd not running, trying to start
7、测试
打开浏览器,在地址栏输入【127.0.0.1】,出现下图表示成功


常见问题:

1、如果通过【127.0.0.1】点击服务器上的文件,出现下载,或者只是显示一小段文字

解决办法:在终端中输入以下两个命令// 关闭 apache 服务器sudo apachectl -k stop// 重新再次启动 apachesudo apachectl -k start

2、每次启动计算机,Apache服务器默认是不会自动启动的,可以启动计算机之后,打开终端,输入以下命令

// 启动 apachesudo apachectl -k start

3、最常见的问题

交换文件已经存在,直接按字母 "d",可以删除交换文件!

4、执行脚本的时候,显示没有或拒绝访问!(可能是用 NTFS 格式的 U 盘拷贝到电脑的脚本!这会把文件本身的权限过滤掉!)以下是在终端中修改文件权限的指令!

// 查看当前文件夹中的文件访问权限ls -la// info.php是没有权限的文件名chmod 644 info.php// *.*是更改所有文件的访问权限为644chmod 644 *.*文件的访问权限:分为三组,3个为一组,出现字母的表示1,-表示0- r 只读- w 可写- x 执行(1组)管理员权限(2组)当前用户权限(3组)普通用户权限3组字母常见组合:110 000 000  ——> 600(管理员可读可写)110 100 100  ——> 644(所有人可读,管理员可读可写)111 111 111  ——> 644(所有人可读可写可执行)不推荐

5、其他命令:

//查询历史输入的命令history

作者:蓝田(Loto) 出处: 简书 如果你觉得本篇文章对你有所帮助,请点击文章末尾下方“喜欢” ^_^ 如有疑问,请在下方评论区回复OR微信OR发送邮件至 shorfng@126.com联系我。

本文版权归作者和本网站共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.