关于使用Joomla!和LAMP的一般提示
- —开始在LAMP上使用Joomla!
- 日期:2015年10月25日 10:52, 星期日
- 作者: Jesse Riggs
Note
This article is intended to become a full tutorial on how to install and manage LAMP and Joomla. Currently, it is just a few notes that I have been taking. Either way, I hope it can be useful to people out there.
What Is Joomla?
Joomla is a Content Management System (CMS) for dynamically generating web pages that includes an administrative web interface. It allows the user to create web pages within a web browser, as easily as writing a Word or ODT document. Javascript features can be added to the pages to enhance usability.
Joomla works as a set of PHP scripts that are executed by a web server and then return an HTML document that is viewed by a client. This is done by passing information about a page request through the URL to the index.php script on your server. This script runs several other scripts that query a database and build an HTML document with the information retrived.
In Joomla all pages are stored in the database as text. This means there will be no HTML documents that reside on the hosting server. HTML snippets will be retreived from the database for which a layout will be built. Modules and content will be inserted into the layout; building the final page.
CSS scripts will format the text to the appropriate font and size. These scripts control a lot of the look and feel of the site, such as: background, color, borders and alignment.
Overview And Layout
Today I want to explain how to build a Joomla website the right way. I will arrange each chapter as such: I will give a quick overview of the component we are installing and why we need it. I will give a link to the recommended tutorial. I will then explain how the component will work in the greater scheme of your website. Then I will give some tips on how to resolve problems I have encountered. I will also direct you to the appropriate README and man files to further explain each component. There is a virtue to a full understanding of each part of a website.
Above, I allude to three important components: the server (Apache Server), the PHP interpreter and the database (MySQL server). Installing and configuring these components on the Linux machine will be the first step I will describe.
So, we need to set up a Joomla website on a server so the whole world can see your work. Many hosting companies offer scripts to do this and this is what I have done. But, if these scripts are not offered to you then you can follow the same steps described to install on your localhost over ssh. It is really simple but to maintain and continue the site with best practices, we must have a local copy to which we can make changes before committing to a live site.
The second step is to set up a working copy of your site on your Linux machine. I use Fedora and this is the distribution I will describe throughout the documentation. Most of what I do can be done on Ubuntu as well but keep in mind that the package management is a little different. And, the directory hierarchy is little different. If you are an Arch user or Slackware then you will have to do a little extra work. But, I believe in you. :)
The third step is to install and set up Eclipse PHP. You should do this before importing your site to a working directory.
Step four is to install and set up an SVN repository. This will allow you to make incremental changes before uploading to your server. If anything goes terribly wrong then you can just revert to a previous revision. This is great for teams of developers as well, since you can commit multiple, unrelated changes to a test space before you put it onto your live site. And changes can be compared before merging.
Step five is to import your working site into your local server. This will require an ftp transfer and some privilege changes.
Six is where the art begins. This is where you will start to build and maintain your site. With the knowledge provided in previous chapters, building the site will be easy and comprehensive. I will explain the general layout of the administration page and how it affects the user site. I will then talk about modules, plugins and extensions...
Audience
This article is intended for people with programming experience and a working knowledge of the Linux operating system. Aside from that I will make no assumptions about what is known of architecture, language or revision control. I want to give a high level overview of web development and best practices. I will give the bare minimum of what needs to be done to get set up. I will reference other tutorials for those of you whom would like to find out more. This is neither a top down nor a bottom up tutorial. It is a general step by step tutorial that includes tips for problems that I have experienced during install and configuration.
The stack used in this article is Linux Apache MySQL PHP (LAMP). There are other popular stacks such as IIS. The best practices described here can be translated to any stack. I hope that the information here will provide an outline for website management.
1 Basic System Setup
What does a teepee and Linux have in common? No Windows, no Gates and only Apache inside.
1.1 A Few Words On RPM And YUM
Before we delve into Apache, I want to discuss a little bit about Redhat Package Manager (RPM) and Yellowdog Update Manager (YUM). These programs make it easy to install new software on your Redhat/Fedora machine. But, there are some problems that I encounter from time to time that keep me hacking away. I will describe possible solutions to such problems.
RPM is a package manager that reads rpm files. An rpm file is an archive that contains executables and configurations. On a fresh Linux install, RPM will be installed and configured to unpack the archive to the correct locations with the correct permissions; i.e. read/write/execute. It also stores information about the newly installed software so that it can be removed later. To see all options offered by RPM, at the command prompt, type:
>rpm --help
For full comprehensive information type:
>man rpm
YUM retrieves rpm packages from a repository and installs them onto your machine. It also checks for dependencies which need installation. It keeps track of what is already installed and any problems that have occurred during installation. YUM will be used to install our basic system components. For a full list of YUM options type:
>yum help
For full comprehensive information type:
>man yum
I recommend reading both man pages before using either of these programs.
Since we will be using two different machines for our live site and our research and development site, it is very important to make sure that we are using the same versions of software at both locations. If there is a bug in a piece of software on our live site that is not on our localhost then we will get errors at the time of release that we have not seen during testing. To see the version information for an installed package use:
>yum info package_name
Make sure that both machines are getting their packages from the same repository.
>yum repolist
will show all repositories to which you have access. The most common YUM transaction you will be using are:
>yum install package_name
>yum upgrade package_name
>yum remove package_name
YUM is fairly verbose with its output and offers helpful solutions to problems that my arise. If you have an issue but YUM isn't being helpful then you can add the the switch “-v” or “-d 9” for more comprehensive output. Sometimes YUM will give error messages about unfinished transactions. In this case run:
>yum-complete-transaction
Further cleanup may be required, so:
>yum clean all
>yum -y update
In certain circumstances, YUM will give error messages about required packages missing but will give a “found instead” message that lists an installed package. This is usually due to an incorrect version of a package. If the package was installed using RPM instead of YUM then the best way to remove the package is from RPM:
>rpm -e package_name
If you are unsure of the package name then:
>rpm -q name
name-VERSION.fcVERSION.ARCHITECTURE
EX.
>rpm -q httpd
httpd-2.4.10-1.fc21.x86_64
To list all packages use:
>rpm -qa
>rpm -q --filesbypkg package_name
More information about these programs can be found at http://www.rpm.org/wiki/Docs and https://www.phy.duke.edu/~rgb/General/yum_HOWTO/yum_HOWTO/yum_HOWTO-1.html.
1.2 HTTP Daemon
Now that we know a little bit about package management, we can install Hyper Text Transfer Protocol Daemon (HTTPD also known as Apache). A daemon is a program that runs, continually, in the background and is not easily killed. HTTPD waits and listens on a port for requests from clients; i.e. a browser. It then serves the requested information to the client. This is the most important component to any website.
To install and then run:
>sudo yum install httpd
>sudo service httpd start
If all goes well then this should work without a hitch. If it doesn't then don't get discouraged. If there is a bug in YUM or a misconfiguration then it could take hours or even days of hacking and searching stackoverflow.com to find an answer. But, when you are finished you will have a greater understanding of how this works. This is also why people get paid the big bucks to do such work. To start Apache on each boot run:
>sudo chkconfig httpd on
To show the status of Apache run:
>service httpd status
If there are no errors then HTTPD is running and we can check to make sure by going to our browser and typing “localhost/” into the URL bar. This will give us a blank screen with no error messages. We should test to see if it will serve us a file over HTTP.
When HTTPD receives a request it will search for a file in a web directory; on Fedora it is /var/www/html/. For test this we will create a document with some text inside of it and then open it from our browser. Since the path is protected with root privileges we will use sudo to create this file.
> sudo echo hello world >/var/www/html/test.html
We can now access this file from our browser by entering “localhost/test.html” into the URL bar. If you get a 404 error then you may have the wrong permissions set for this file. We can change it so that Apache can read it by:
>sudo chmod 644 /var/www/html/test.html
SELinux: sometimes SELinux will not have the correct permissions granted to httpd. Check to see if there is an "rw" in the listed files, and if not then make it so:
>ls -aLZ
>chcon -R -t httpd_sys_content_rw_t /var/www/html/mysite/
Try accessing this file again from your browser. If you see “hello world” printed in 12 font then congratulations, you have successfully created a working web page. It's time to reward yourself with a smoke or some chocolate. =D
More documentation on Apache can be found at http://httpd.apache.org/docs/.
1.3 MySQL
MySQL is an open source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL). Every time a Joomla created web page is accessed, HTTPD will execute several Joomla scripts. Each script relies on database queries to build the web page. The MySQL package we will be installing is MariaDB.
>sudo yum install mariadb-server mariadb-client
We need to now start mysql server. But, we want this server to start each time we boot, so we will add one more command to do this:
>sudo service mysqld start
>sudo systemctl enable mysqld.service
We now have installed MySQL but it is not secure. In fact anyone who has access to your machine can log into your database and change things without even using a password... But, don't worry. An interactive script that will make your database secure was installed with MariaDB.
>sudo mysql_secure_installation
Set root password? [Y/n] y
New password:
Re-enter new password:
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
Congratulations, you now have a working RDBMS working on your machine. ;) Before we create a database for Joomla, we want to first install PHP and PHPMyAdmin.
For more information on MySQL options check out the manual page:
>man mysql
For just about everything you would ever need to know about MariaDB go to https://mariadb.com/kb/en/. For an interactive tutorial on Structured Query Language check out http://www.w3schools.com/sql/.
1.4 PHP and PHPMyAdmin
PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. Ultimately, Joomla IS a set of PHP scripts. In order to interpret these scripts we will install PHP. PHPMyAdmin is a cool web tool for accessing MariaDB. You are free to use any other workbench for your database management but I think it is important to stay consistent between your local site and your live site. If there is ever a problem with your live site's database then PHPMyAdmin offers an intuitive interface so that you can make the appropriate changes. By using PHPMyAdmin locally, you will be able to access your database from your web browser.
>sudo yum install php phpmyadmin
After the full complete installation we now have PHP. Now, both to verify a correct working install and to do further setup, we will run PHPMyAdmin from our browser. In the URL bar, enter “localhost/phpmyadmin/index.php”. Hopefully you see a login screen. If you don't then there a couple reasons you will get either a 403 or a 404 error. The first is that HTTPD is not configured to run php files. The second is that in order to access the folder in which PHPMyAdmin resides, HTTPD must know where to look. We can correct both problems from /etc/httpd/. All configuration files in this directory are loaded by Apache when it starts and inform it of the rules it will follow when it runs. As sudo user, first open /etc/httpd/conf/httpd.conf. Towards the top of this file, make sure that there is a line that adds the PHP module; it looks like:
LoadModule php5_module modules/libphp5.so
If there is a hashtag (#) at the begining of this line, remove it. Also, make sure that under <IfModule dir_module> there is a line that makes Apache look for index.php:
DirectoryIndex index.html index.php
Under <IfModule mime_module>, make sure that there is a type .php:
AddType application/x-httpd-php .php
Also check /etc/httpd/conf.d/phpMyAdmin.conf and make sure there are lines that looks something like this:
<Directory /usr/share/phpMyAdmin/>
...
Allow from 127.0.0.1 #This is for the local user.
Require all granted #This is for remote users.
...
</Directory>
After making changes to the configuration files you must always restart HTTPD:
>service httpd restart
Congratulations, you now have a working LAMP. =D You now, technically, have everything you need to install and run Joomla. But, there are two more things that I would like to install to make the development process a whole lot easier and safer.
Check out the php manual page for a list of options. If you would like an interactive php tutorial, check out http://www.w3schools.com/php/. For full documentation of PHPMyAdmin, check out http://docs.phpmyadmin.net/en/latest/.
2 SVN
Subversion (SVN) is a powerful revision control tool. We will be using SVN on our local machine to keep track of changes and create patch files that can be patched into our live site. SVN makes it easy to maintain multiple copies of source code, at various stages of development, and merge changes together before releasing them to the public. Patches are just text files that contain lines of code to be added or removed from previously released code. We can easily create patches from the difference of source code files and then apply them to our working source code, using SVN.
>yum install subversion
We want to create a repository where we can keep a copy of our source code. We will not have source code until we import Joomla into our web directory. But, we do want this tool available before we start making changes to Joomla. If anything goes terribly wrong then we can just, with SVN, revert the source code to an earlier working version. Learning to use SVN may seem like a pain in the a** but when you get around the learning curve, it makes development like a million times easier. Creating a repo can be done with the command:
>mkdir /home/user/myrepo
>svnadmin create /home/user/myrepo
Later we will be importing our Joomla directory into our repository. Since I am the only person working on this project and since my svn repo is stored locally on my machine, I will use the file: protocol. SVN also supports https, DAV and custom protocols so that your repo can be accessed from network connections.
>svn import file://localhost/home/user/myrepo/mysite /var/www/html/mysite -m “This is where you put your commit comments.”
Other useful svn commands include: co, push and add. We will cover these later. But, if you want to know everything that is to be known about svn check out the svn book at: http://svnbook.red-bean.com/
http://queens.db.toronto.edu/~nilesh/linux/subversion-howto/
We need to turn our web directory into a working directory
>svn co file:///home/user_name/myrepo/mysite/
If you find ? From svn status then you may have to delete the .svn folder in that directory. Update and revert are also important for maintaining your repo. --accept is required for revert, use theirs-all or working with this option.
3 Eclipse
Eclipse is an Integrated Development Environment (IDE). We will be using Eclipse to edit back-end PHP files, as well as HTML, CSS and XML. There are some great plugins for debugging PHP during script execution, Xdebug for example. There are also plugins for SVN. From Eclipse we can fix bugs in existing code and create our own components, modules and plugins. Can you feel the fun? It's on its way.
Revision Graph
Team Provider
Team Provider Sources
http://www.eclipse.org/subversive/installation-instructions.php
http://forum.joomla.org/viewtopic.php?t=187461
Configure Eclipse for development https://docs.joomla.org/Configuring_Eclipse_for_joomla_development
Install Xdebug http://xdebug.org/docs/install
>yum install php-pecl-xdebug
>php -m
...
xdebug
...
[Zend Modules]
Xdebug
This shows that Xdebug has successfully been installed!
Configure Eclipse for XDebug: https://docs.joomla.org/Configuring_Eclipse_and_Xdebug
PDT for Eclipse http://www.eclipse.org/pdt/articles/debugger/os-php-eclipse-pdt-debug-pdf.pdf
Remember to debug as CLI. If you debug Joomla as Web App then it will hang and no breakpoints will ever be hit.
When debugging a string, remember that only the first 1024 chars will be displayed from variables. However, you can view the entire string by right clicking and then selecting “change value.”
Xdebug information: https://eclipse.org/pdt/documents/XDebugGuideForPDT2.0.pdf
Make sure that this appears in /etc/php.ini:
[xdebug]
xdebug.remote_enable=1
xdebug.remote_host="localhost"
xdebug.port=9000
xdebug.remote_handler="dbgp"
zend_extension="/usr/lib64/php/modules/xdebug.so"
4 Joomla
Joomla can be downloaded from https://www.joomla.org/download.html
unzip the files into /var/www/html/site_name/. Make sure that all file permissions are 644 and directory permissions 755. You can now run Joomla admin from your web browser http://localhost/site_name/administration/. You now need to set up your database and password stuff. Follow the wizard.
Import the directory /var/www/html/site_name/ into your repo and then check it out to make it a working copy. You may need to revert after checkout. You can zip this directory and upload it to your server. Use phpMyAdmin to download your database and then use phpMyAdmin to upload it to your server.
When you create or edit a web page on your localhost, you will use phpMyAdmin to import the changes and then export them to your server.
When you make changes to Joomla code, you can use an svn diff to create a patch that can be applied to your server.
>svn diff > patch_file_name
When there is a Joomla update, apply the update to your localhost and test that it works. Make a patch and upload it to your server.
Basic security: https://opensourcehacker.com/2010/05/31/how-to-install-joomla-on-your-ubuntulinux-server-with-basic-security/
Remove index.php from URLs: http://forum.joomla.org/viewtopic.php?t=614279
Joomla developers documentation: https://api.joomla.org/cms-3/index.html
General Tips On Using Joomla With Lamp by Jesse Riggs is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Based on a work at www.jesse-riggs.com.
关于使用Joomla!和LAMP的一般提示 by Jesse Riggs is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Based on a work at jesse-riggs.com.