个人工具

UbuntuHelp:ApacheMySQLPHP

来自Ubuntu中文

Oneleaf讨论 | 贡献2007年5月13日 (日) 11:23的版本 (New page: {{From|https://help.ubuntu.com/community/ApacheMySQLPHP}} {{Languages|php5}} Parent page: Programming Applications This is to help people setup and install a ...)

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航, 搜索


Parent page: Programming Applications


This is to help people setup and install a LAMP (Linux-Apache-MySQL-PHP) server in Ubuntu, including Apache 2, PHP 4 or 5, and MySQL 4.1 or 5.0.


When installing from the Ubuntu 6.06 (Dapper Drake) "Server cd", you have the option of choosing to install a LAMP setup at the inital Ubuntu installation screen. That will install apache2, php5 and mysql 5.0.

In Ubuntu 5.10 (Breezy Badger), PHP5 is available in the main repositories but MYSQL 5.0 can only be installed by compiling.

Check Requirements

Some applications require php4 while others will work with php5. Be sure to install the version of php and the corresponding apache2 module for it. You cannot have both php4 and php5 modules running on the same instance of apache2 at the same time. Installing one may remove the other.

If you have both php4 and php5 installed, be aware of which version of the apache2 php module you have. If libapache2-mod-php5 is already installed, the php4 package will install libapache-mod-php4 and not libapache2-mod-php4 package.

If libapache2-mod-php5 is not installed, installing php4 will install the apache2 php module (libapache2-mod-php4). See this example.

Most web applications will use Apache2, php5 and mysql5.0. If no specific versions are mentioned in your web application's documentation, use those.


To install the default LAMP stack in Ubuntu 6.06 LTS (Dapper Drake)

If you did not use the LAMP installer option from the server cd but want to install those same packages without having to reinstall your operating system, use any method to install the following packages

apache2 php5-mysql libapache2-mod-php5 mysql-server
</code>

All of those packages are in the Ubuntu 6.06 LTS (Dapper Drake) '''main''' repository.  Once LAMP is installed, you need to set a mysql root password and then, depending on your web application, create a database, user and password.   That's it!


=== Installing Apache 2 ===
To only install the apache2 webserver, use [[UbuntuHelp:InstallingSoftware| any method]] to install

<pre>
apache2
</code>

==== Troubleshooting ====
If you get this error:

''apache2: Could not determine the server's fully qualified domain name, using 127.0.0.1 for ServerName''

then edit <code>gksudo gedit /etc/apache2/apache2.conf</code> and add <code>ServerName localhost</code> at the end of the file

If Apache2 is pointing to the wrong directory then you need to edit the file <code>/usr/share/apache2/default</code> and any files in the directory <code>/usr/share/apache2/allowed-sites/</code>

=== Installing PHP 5 ===
To only install PHP5. use [[UbuntuHelp:InstallingSoftware| any method]] to install

<pre>
php5
</code>

If PHP4 is present on your system, also install

<pre>
libapache2-mod-php5
</code>


==== Troubleshooting ====

Does your browser ask if you want to '''download the php''' file instead of displaying it?  If Apache is not actually parsing the php after you restarted it, install <code>libapache2-mod-php5</code>.  It is installed when you install the <code>php5</code> package, but may have been removed inadvertently by packages which need to run a different version of php.  You may also need to actually enable it, by doing <code>sudo a2enmod php5</code> followed by <code>sudo /etc/init.d/apache2 restart</code>.  Be sure to clear your browser's cache before testing your site again.


=== Installing PHP 4 ===
To install PHP4, Use [[UbuntuHelp:InstallingSoftware| any method]] to install

<pre>
php4
</code>

If PHP5 is present on your system, installing php4 will install the php module for apache (version 1.3) and not apache2.  To use php4 with apache2, install

<pre>
libapache2-mod-php4
</code>


==== Troubleshooting ====
Does your browser ask if you want to '''download the php''' file instead of displaying it?  If Apache is not actually parsing the php after you restarted it, install <code>libapache2-mod-php4</code>.  It is installed when you install the <code>php4</code> package, but may have been removed inadvertently by packages which need to run a different version of php.  You may also need to actually enable it, by doing <code>sudo a2enmod php4</code> followed by <code>sudo /etc/init.d/apache2 restart</code>.  Be sure to clear your browser's cache before testing your site again.


=== Installing MYSQL with PHP 5 ===

Use [[UbuntuHelp:InstallingSoftware| any method]] to install

<pre>
mysql-server libapache2-mod-auth-mysql php5-mysql
</code>


=== Installing MYSQL with PHP 4 ===

First enable the universe repository since the packages are not in ''main''


* [[UbuntuHelp:Repositories/CommandLine| I am using Ubuntu server (command line)]]
* [[UbuntuHelp:AddingRepositoriesHowto| I am using a desktop]]

Use [[UbuntuHelp:InstallingSoftware| any method]] to install

<pre>
mysql-server libapache2-mod-auth-mysql php4-mysql
</code>

=== After installing PHP ===

You may need to increase the memory limit that PHP imposes on a script.  Edit the /etc/php5/apache2/php.ini file and increase the ''memory_limit'' value.


=== After installing MySQL ===

==== Set mysql bind address ====

Before you can access the database '''from other''' computers in your network, you have to change its bind address.
'''Note that this can be a security problem, because your database can be accessed by others computers than your own.
Skip this step if the applications which require mysql are running on the same machine.'''

type:
<pre>
nano /etc/mysql/my.cnf
</code>

and change the line:
<pre>
bind-address           = localhost
</code>
to your own internal ip address e.g. 192.168.1.20
<pre>
bind-address           = 192.168.1.20
</code>

If you try to connect without changing the bind-address you will recieve a "Can not connect to mysql error 10061".

==== Set mysql root password ====

Before accessing the database by console you need to type:
<pre>
mysql -u root
</code>

At the mysql console type:
<pre>
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpassword');
</code>

A successful mysql command will show:

''Query OK, 0 rows affected (0.00 sec) ''

Mysql commands can span several lines.  Do not forget to end your mysql command with a semicolon.

'''Note:'''  If you have already set a password for the mysql root, you will need to use:
<pre>
mysql -u root -p
</code>
(Did you forget the mysql-root password?  See MysqlPasswordReset.)

==== Create a mysql database ====

<pre>
mysql> CREATE DATABASE database1;
</code>

==== Create a mysql user ====

For creating a new user with all privileges (use only for troubleshooting), at mysql prompt type:
<pre>mysql> GRANT ALL PRIVILEGES ON *.* TO 'yourusername'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
</code>

For creating a new user with fewer privileges (should work for most web applications) which can only use the database named "database1", at mysql prompt type:
<pre>
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON database1.* TO 'yourusername'@'localhost' IDENTIFIED BY 'yourpassword';
</code>

''yourusername'' and ''yourpassword'' can be anything you like. ''database1'' is the name of the database the user gets access to. ''localhost'' is the location which gets access to your database. You can change it to '%' (or to hostnames or ip addresses) to allow connections from every location (or only from specific locations) to the database. '''Note, that this can be a security problem and should only be used for testing purposes!'''


To exit the mysql prompt type:
<pre>
mysql> \q
</code>

Since the mysql root password is now set, if you need to use mysql again (as the mysql root), you will need to use:
<pre>
mysql -u root -p
</code>

and then enter the password at the prompt.

==== Backup-Settings ====

Please, let's say something in which directories MySql stores the database information and how to configure a backup

==== Alternatively ====
There is more than just one way to set the mysql root password and create a database.  For example '''mysqladmin''' can be used:

<pre>
mysqladmin -u root -p password yourpassword
</code>

and

<pre>
mysqladmin -u root -p create database1
</code>

''mysqladmin'' is a command-line tool provided by the default LAMP install.

=== Phpmyadmin and mysql-admin ===

All mysql tasks including setting the root password and creating databases can be done via a graphical interface using '''phpmyadmin''' or '''mysql-admin'''.

To install one or both of them, first enable the universe repository


* [[UbuntuHelp:Repositories/CommandLine| I am using Ubuntu server (command line)]]
* [[UbuntuHelp:AddingRepositoriesHowto| I am using a desktop]]

Use [[UbuntuHelp:InstallingSoftware| any method]] to install

<pre>
phpmyadmin
</code>

==== Troubleshooting ====
'''If you get blowfish_secret error:'''
Choose and set a phrase for cryptography in the file /etc/phpmyadmin/blowfish_secret.inc.php  and copy the line (not the php tags) into the file /etc/phpmyadmin/config.inc.php or you will receive an error.

==== Alternative: install phpMyAdmin from source ====
See the [https://help.ubuntu.com/community/phpMyAdmin phpMyAdmin] page for instructions on how to install phpmyadmin from source:

==== Mysql-admin ====
Mysql-admin runs locally, on the desktop.  Use [[UbuntuHelp:InstallingSoftware| any method]] to install

<pre>
mysql-admin
</code>

==== For more information ====
You really ought to read [http://dev.mysql.com/doc/mysql/en/default-privileges.html 2.9.3. Securing the Initial MySQL Accounts] from the [http://dev.mysql.com/doc/mysql/en/index.html MySQL Reference Manual].


=== Edit Apache Configuration ===
You may want your current user to be the PHP pages administrator. To do so, edit the Apache configuration file :
<pre>
$ gksudo "gedit /etc/apache2/apache2.conf"
</code>
Search both the strings starting by "User" and "Group", and change the names by the current username and groupname you are using.
Then you'll need to restart Apache. (look at the next chapter concerning apache commands)

Configuration options relating specifically to user websites (accessed through localhost/~username) are in /etc/apache2/mods-enabled/userdir.conf.

=== Edit PHP Configuration to Work With MYSQL (Ubuntu Breezy) ===

''This is not needed for Ubuntu 6.06 LTS (Dapper Drake).  In the latest version, "extension=mysql.so" and "extension=mysqli.so" are present without the ";" and so are enabled out-of-the-box.''

You may need to edit the PHP configuration file to get PHP and MYSQL talking :
<pre>
$ gksudo "gedit /etc/php4/apache2/php.ini"
</code>
or if you are using php5
<pre>
$ gksudo "gedit /etc/php5/apache2/php.ini"
</code>

Remove the ";" for the line ";extension=mysql.so", and restart Apache as is stated below.

=== Edit PHP Configuration to Work With MYSQL (Ubuntu Dapper) ===

In Dapper Drake, "extension=mysql.so" and "extension=mysqli.so" are enabled in the php.ini file out-of-the-box. However, sometimes php is not looking for those files in the right directory. You have then to move your files or modify the php.ini configuration.:

==== First solution ====

locate the directory where the extension files are placed:

<code> locate mysql.so </code>

(change mysql.so in mysqli.so if you want to install the mysqli functions)

-then modify the php.ini file to indicate the right place for the extension directory:

<pre>
$ gksudo "gedit /etc/php4/apache2/php.ini"
</code>

or if you are using php5

<pre>
$ gksudo "gedit /etc/php5/apache2/php.ini"
</code>

Look for the 'extension_dir' property, and set it to the directory where you found the mysql(i).so file:

 extension_dir= "/usr/lib/php5/20051025/"

Restart apache, and test if your mysql(i) functions are working.


==== Second solution ====

-locate the directory where the extension files are placed:

<code> locate mysql.so </code>

(change mysql.so in mysqli.so if you want to install the mysqli functions)

Let's say that you found the file in '/usr/lib/php5/20051025/'

-then check in the php.ini file for the extension directory

<pre>
$ gksudo "gedit /etc/php4/apache2/php.ini"
</code>

or if you are using php5

<pre>
$ gksudo "gedit /etc/php5/apache2/php.ini"
</code>

Look for the 'extension_dir' property. It should be by default '/usr/lib/php5/ext'. If it's not, change it for this value.

-Now create the default directory for extensions:

<pre>
$ sudo mkdir /usr/lib/php5/ext
</code>

-Copy the extension file to the new directory:

<pre>
$ sudo cp /usr/lib/php5/20051025/mysql.so /usr/lib/php5/ext/mysql.so
</code>

Change the first path to the one you found with the locate function, and change mysql.so into mysqli.so if you want to use mysqli functions.

-Restart apache (see below), and test if your mysql(i) functions are working.



=== Run, Stop, And Restart Apache ===
Use the following command to run Apache :
<pre>
$ sudo /usr/sbin/apache2ctl start
</code>
To stop it, use :
<pre>
$ sudo /usr/sbin/apache2ctl stop
</code>
Finally, to restart it, run :
<pre>
$ sudo /usr/sbin/apache2ctl restart
</code>
=== Using Apache ===
You can acces apache by typing 127.0.0.1 or http://localhost (by default it will be listening on port 80) in your browser address bar.
By default the directory for apache server pages is /var/www .
It needs root access in order to put files in. A way to do it is just starting the file browser as root in a terminal:
<pre>
$ sudo nautilus
</code>

or

if you want to make /var/www your own.  (Use only for non-production web servers - this is not the most secure way to do things.)
<pre>
$ sudo chown -R $USER:$USER /var/www
</code>

=== Status ===
To check the status of your PHP installation:
<pre>
 $ gksudo "gedit /var/www/testphp.php"
</code>
and insert the following line
<pre>
 <?php phpinfo(); ?>
</code>

View this page on a web browser at http://yourserveripaddress/testphp.php or http://localhost/testphp.php

=== Securing Apache ===
If you just want to run your Apache install as a development server and want to prevent it from listening for incoming connection attempts, this is easy to do.
<pre>
$ gksudo "gedit /etc/apache2/ports.conf"
$ password:
</code>

Change ports.conf so that it contains:
<pre>
Listen 127.0.0.1:80
</code>

Save this file, and restart Apache (see above). Now Apache will serve only to your home domain, http://127.0.0.1 or http://localhost.

=== Password-Protect a Directory ===

There are 2 ways to password-protect a specific directory.
The recommended way involves editing
<code> /etc/apache2/apache2.conf </code>.
(To do this, you need root access).
The other way involves editing a <code>.htaccess</code> file in the directory to be protected.
(To do this, you need access to that directory).

==== Password-Protect a Directory With .htaccess ====

See EnablingUseOfApacheHtaccessFiles

'''Warning: On at least some versions of Ubuntu, <code>.htaccess</code> files will not work by default.  See EnablingUseOfApacheHtaccessFiles for help on enabling them.'''

=== thumbnails ===

If you direct your web browser to a directory (rather than a specific file),
and there is no "index.html" file in that directory,
Apache will generate an index file on-the-fly listing all the files and folders in that directory.
Each folder has a little icon of a folder next to it.

To put a thumbnail of that specific image (rather than the generic "image icon")
next to each image file (.jpg, .png, etc.):

''... todo: add instructions on how to do thumbnails here, perhaps using [http://aspn.activestate.com/ASPN/CodeDoc/Apache-AutoIndex/AutoIndex.html Apache::AutoIndex 0.08] or [http://aspn.activestate.com/ASPN/CodeDoc/Apache-Album/Album.html Apache::Album 0.95] ... ''


=== Known problems ===

==== Skype incompatibility ====
[[UbuntuHelp:Skype]] uses port 80 for incoming calls, and thus, may block Apache. The solution is to change the port in one of the applications. Usually, port 81 is free and works fine. To change the port number in [[UbuntuHelp:Skype]] go to menu Tools > Options, then click on the Advanced tab, then in the box of the port for incoming calls write your preference.

=== Other Apache Options ===
* ServerSideIncludes - enable SSI in Apache2
* LocalhostSubdomain - access your local files as if you had different subdomains

=== Further Information ===
* StrongPasswords is recommended reading!
* BastilleLinux is also recommended if you're going to be running a live webserver.
* You can compile Self:PHP5FromSource, as well as Self:MYSQL5FromSource.
* Self:PHPOracle will enable you to connect to Oracle databases.
* PhpPear : PHP Extension and Application Repository

[[category:UbuntuHelp]]