Home Backend Development PHP Tutorial 老司机传授Ubuntu下Apache+PHP+MySQL环境搭建攻略_PHP

老司机传授Ubuntu下Apache+PHP+MySQL环境搭建攻略_PHP

May 28, 2016 am 11:48 AM
apache linux mysql php ubuntu

一、首先安装MySql:
对于MySql来讲在整个过程中是相对独立的安装,并且我的ubuntu在以前做开发的时候就已经安装了MySql,所以以下只给出安装步骤:
 
1、安装MySql的服务:

sudo apt-get install mysql-server
Copy after login

要注意的是安装过程中会提示你设置mysql的root账户的密码;其他就没什么需要注意的地方了。

2、安装MySql的客户端:

sudo apt-get install mysql-client 
Copy after login

直接输入安装

3、安装MySql的lib依赖库:

sudo apt-get install libmysqlclient15-dev
Copy after login

进行安装。
如果不安装的话会在系统中找不到mysql.h

4、MySQL的一些简单管理:

启动MySQL服务:

sudo start mysql
Copy after login

停止MySQL服务:

sudo stop mysql
Copy after login

修改 MySQL 的管理员密码:

sudo mysqladmin -u root password newpassword
Copy after login

设置远程访问(正常情况下,mysql占用的3306端口只是在IP 127.0.0.1上监听,拒绝了其他IP的访问(通过netstat可以查看到)。取消本地监

听需要修改 my.cnf 文件:):

    sudo vi /etc/mysql/my.cnf

    bind-address = 127.0.0.1 //找到此内容并且注释

Copy after login


5、MySQL安装后的目录结构分析(此结构只针对于使用apt-get install 在线安装情况):

数据库存放目录:/var/lib/mysql/

相关配置文件存放目录:/usr/share/mysql

相关命令存放目录:/usr/bin(mysqladmin mysqldump等命令)

启动脚步存放目录:/etc/rc.d/init.d/

以上就是MySql的基本安装了。

二、安装apache2:

1、官网下载http://httpd.apache.org/download.cgi,Unix Source: httpd-2.2.24.tar.gz,我下的是2.2.24版本的tar.gz,然后解压tar.gz

   tar zxvf httpd-2.2.24.tar.gz
Copy after login

把解压出来的文件移动到/usr/src/apache2

   sudo mv -r httpd-2.2.24 /usr/src/apache2 
Copy after login

如果没有apache2文件无法移动的话 手动创建一个

sudo mkdri /usr/src/apache2 
Copy after login

由于修改/usr/下的文件需要root权限,所以需要加上sodu命令,加输入密码。

2、进入/usr/src/apache2/httpd-2.2.24,并且编译安装

   cd /usr/src/apache2/httpd-2.2.24
   ./configure -prefix=/usr/local/apache -enable-module=so -enable-rewrite=shared -enable-authn-dbm
Copy after login


编译完成没错之后
输入make命令,完成后输入:make install开始安装。

3、安装完成后配置httpd.conf:

   vim /usr/local/apache2/conf/httpd.conf  #用编辑器打开httpd.conf 我用的是vim打开的
Copy after login

找到#ServerName 将前面的注释符号#去掉 并且将后面的内容改成localhost:80 整句就是:
ServerName localhost:80
这样服务启动的时候可以通过localhost来进行访问。
如果要修改服务器的监听端口的话可以在httpd.conf中查找Listen 80把 后面的80改成你想要的端口,在把ServerName localhost:80 后面的80改成你想要的端口。

4、启动服务:

   cd /usr/local/apache2/bin #进入/usr/local/apache2/bin目录
   sudo ./apachectl start      #启动apache 服务
Copy after login

然后打开游览器,输入访问地址localhost看看能否显示It Works,如果可以显示就说明已经安装完成。

三、安装配置PHP5

1、首先安装libxml2库,用来解析xml的的lib库输入:sudo apt-get install libxml2-dev 进行安装

2、官网下载php http://www.php.net/downloads.php 然后解压下载来的tar.gz包

   tar zxvf php-5.4.15.tar.gz           #解压
    sudo mv -r php-5.4.15 /usr/src/php5 #移动到/usr/src/php5目录下
Copy after login

3、编译安装php

   cd /usr/src/php/php-5.4.15  #进入php所在目录
    ./configure  
    -prefix=/usr/local/php5   #php安装目录
    -with-apxs2=/usr/local/apache2/bin/apxs  #apxs所在的目录,如果没有就sudo find / -name apxs 查询一下所在的目录
    -with-mysql=/usr/   #这个是查找mysql.h的目录,不知道为什么我的mysql.h在/usr/include/中 但是 写这个路径没用必须写/usr/ 如果不知道mysql.h在哪就 sudo find / -name mysql.h查找一下
    -with-mysqli=/usr/bin/mysql_config #mysql_config所在的目录,如果不再这个目录就find一下。
    -with-gd  #打开对gd库的支持,如果应为这个报错,就安装一下gd库
    -with-pear #打开pear命令的支持
    -with-libxml-dir  #打开对刚刚安装的libxml2的支持
Copy after login

整条编译语句是:

代码如下:

./configure -prefix=/usr/local/php5 -with-apxs2=/usr/local/apache2/bin/apxs -with-mysql=/usr/ -with-mysqli=/usr/bin/mysql_config -with-gd -with-pear -with-libxml-dir


如果编译时出现error的话 一般都是 依赖库lib 出问题,看提示缺少什么依赖库,然后安装一下就行。
完成后make一下 然后再输入make install进行安装。

4、手动指定php.ini文件:
打开/usr/local/apache2/conf/httpd.conf,在LoadModule php_module modules/libphp5.so这行下面加入PHPIniDir /etc/php.ini 指定php.ini的路径,然后进入你解压的php-5.4.15.tar.gz的目录将php.ini-development复制到你指定的路劲/etc里并且重命名为php.ini:

   cd /usr/src/php/php-5.4.15 #进入php目录
   sudo cp php.ini-development /etc #复制到/etc
   sudo mv php.ini-development php.ini #重命名为php.ini
Copy after login

5、修改httpd.conf中的php支持和默认访问index.php:
打开/usr/local/apache2/conf/httpd.conf,把中的内容改成:

   <Directory  />
   Options FollowSymLinks
   DirectoryIndex index.php index.html  #指定默认先查找index.php 如果没有就查找index.html
   AllowOverride None
   Order deny,allow
   Deny from all
   </Directory>
Copy after login

接着在AddType application/x-gzip .gz .tgz下面一行中加入:

   AddType application/x-httpd-php .php
   AddType application/x-httpd-php-source .phps
Copy after login

完成后保存文件,如果提示文件是只读模式无法保存的话,那么肯定是权限不够在用打开httpd.conf的命令时 要加上sudo

6、启动\重启apache服务,在服务器根目录下编写php文件

   sudo /usr/local/apache2/bin/apachectl stop #停止服务
   sudo /usr/local/apache2/bin/apachectl start #启动服务
   sudo /usr/local/apache2/bin/apachectl rstart #重启服务
Copy after login

   cd /usr/lcoal/apache2/htdocs #进入apache2默认根目录
Copy after login

      创建一个index.php文件 在里面编写然后打开游览器输入localhost看看能否显示php的信息,如果可以则配置成功。


四、其他配置:
    
1、手动配置apache的默认的根目录
      用编辑器打开/usr/local/apache2/conf/httpd.conf目录然后找到DocumentRoot /usr/lcoal/apache2/htdocs把这个路径改成你要的路径 然后在往下一点找到把里面的路径也改成你想要指定的路径就可以了。

2、查看php.ini所在的路径:
      很多时候不知道指定php.ini的路径在哪看,只要编写文件 然后用游览器访问,看到php所有信息之后 找到:Loaded Configuration File这一行的 右边的路径信息 就是指定的php.ini路径的地址了。
      
     

Statement of this Website
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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
Composer: Aiding PHP Development Through AI Composer: Aiding PHP Development Through AI Apr 29, 2025 am 12:27 AM

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

MySQL: The Database, phpMyAdmin: The Management Interface MySQL: The Database, phpMyAdmin: The Management Interface Apr 29, 2025 am 12:44 AM

MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

macOS and Linux: Compatibility and User Experience macOS and Linux: Compatibility and User Experience Apr 30, 2025 am 12:05 AM

macOS and Linux have their own advantages in compatibility and user experience. macOS has excellent compatibility within the Apple ecosystem, and the user experience is simple and intuitive; Linux has outstanding hardware compatibility and software flexibility. The user experience varies from distribution to distribution, emphasizing personalization and control.

An efficient way to batch insert data in MySQL An efficient way to batch insert data in MySQL Apr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

See all articles