Setting Up LAMP Getting Linux, Apache, MySQL, and PHP Working Together phần 7 pps

42 281 0
Setting Up LAMP Getting Linux, Apache, MySQL, and PHP Working Together phần 7 pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

230 Chapter 8 • Apache Web Server: Installation and Configuration ● LimitRequestBody ● <Location> ● <LocationMatch> ● LogLevel ● Options ● ResourceConfig ● RLimitCPU ● RLimitMEM ● RLimitNPROC ● ServerAdmin ● ServerAlias ● ServerName ● ServerPath ● ServerSignature ● UseCanonicalName ● User (Requires suEXEC installed) ● Let’s put some of these directives to use in configuring an Apache virtual host. Configuring Apache Virtual Hosts It is now time to start planning and configuring your virtual hosts. By the end of this section, you will be very familiar with how well designed and flexible the Apache Web Server really is. Got DNS? For this example, we are going to assume you have two domain names with DNS set up to point to the IP address of your server. If you do not have DNS set up or you do not own domain names yet, you can simply edit your system’s hosts file and trick your system into resolving a fake name for your server’s IP address. On Linux, the HOSTS file is located in /etc/hosts, and you will need to add the following lines to it, assuming your server’s IP address is 123.456.789.1. NOTE The IP addresses used here are fictional will not work on a real server. You must substitute 123.456.789.1 with your real IP addresses! 4337Book.fm Page 230 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 231 Using Apache Virtual Hosts 123.456.789.1 yourdomain.com www.yourdomain.com 123.456.789.1 yourotherdomain.com www.yourotherdomain.com On Windows systems, you should search your C:\Windows directory for a file named hosts. It is usually located in C:\Windows\System32\Drivers\etc\hosts. In this file, you will add the following lines: 123.456.789.1 yourdomain.com www.yourdomain.com 123.456.789.1 yourotherdomain.com www.yourotherdomain.com Now your system is tricked into resolving the domain names yourdomain.com, www.yourdomain .com , yourotherdomain.com, and www.yourotherdomain.com to the IP address of the server. TIP Keep this previous trick in mind the next time you register a domain name. You can set up your HOSTS files so you can start building your website while you wait for DNS to register and resolve. Preparation Before you start digging into the configuration files, you need to prepare the system by creat- ing the DocumentRoot and Logging directories. A rule of thumb that usually works well is to use the domain name for the parent directory. You can store your DocumentRoot directories any- where you would like. We prefer to use /home. Follow these steps: 1. Create the directories: mkdir -p /home/www.yourdomain.com/public_html/cgi-bin mkdir -p /home/www.yourdomain.com/logs mkdir -p /home/www.yourotherdomain.com/public_html/cgi-bin mkdir -p /home/www.yourotherdomain.com/logs NOTE The traditional method of using cgi-bin directories is to place them outside of your doc- ument root and use the cgi-bin ScriptAlias and Alias directives. However, this method works and can be used if you understand that there might be minimal risks involved. 2. Now you have your directories created, you should change the permissions of these direc- tories because you are probably running as root. It is good practice to assign the files to a user other than root. This prevents your users from logging in to the server as root to edit files and manage websites. Change the permissions as follows: chown -R someuser.somegroup /home/www.yourdomain.com/ chown -R someuser.somegroup /home/www.yourotherdomain.com/ 3. You need to create a directory that will store your virtual host files. You have the ability to simply append the virtual host configurations directly to the httpd.conf file; however, you can also have Apache include a directory of configuration files when it starts up. Keeping 4337Book.fm Page 231 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 232 Chapter 8 • Apache Web Server: Installation and Configuration a separate directory of individual virtual host files seems more practical than editing a con- figuration file of 500+ lines each time you need to manage one of those hosts. Make the directory now: mkdir /www/conf/vhosts 4. Modify the httpd.conf file to include this new directory of configuration files you will be creating; open the /www/conf/httpd.conf file and add the following line to the end of it: include conf/vhosts 5. Because you are editing the httpd.conf file, you need to enable one more setting for your virtual hosts. Locate the NameVirtualHost directive line and remove the comment symbol (#); then change it to the following setting: NameVirtualHost * The previous setting enables virtual hosts to be configured for any IP address. You should be all set to create your virtual host configurations now. Virtual Host Configuration Files In the previous section, we mentioned that creating separate files for each host makes virtual hosts much easier to manage. Let’s set up the first file for www.yourdomain.com and name this file www.yourdomain.com.conf in the /www/conf/vhosts directory. The file will contain the fol- lowing contents: <VirtualHost *> ServerName www.yourdomain.com ServerAlias yourdomain.com DocumentRoot /home/www.yourdomain.com/public_html CustomLog /home/www.yourdomain.com/logs/access_log combined ErrorLog /home/www.yourdomain.com/logs/error_log </VirtualHost> NOTE When using the backslash to continue directives, you should avoid allowing any additional spaces or characters after the backslash (\), which could cause parse errors in the con- figuration files. Let’s break down this configuration file for better understanding. The following line is the opening tag for the virtual host. Everything between this line and the last line, </VirtualHost>, will contain the settings for this virtual host that you are configuring. Take special notice of the setting * in the open tag. The * indicates that this virtual host will respond to the request for this domain name on any IP address it is used for: <VirtualHost *> 4337Book.fm Page 232 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 233 Using Apache Virtual Hosts The next directive is the name of your server, hence the name ServerName: ServerName www.yourdomain.com Next, you define the ServerAlias directive. This enables you to point yourdomain.com to the same virtual host. This is important because web users have a tendency to access your site with- out the leading www, and you do not want to lose any users by an improperly configured server: ServerAlias yourdomain.com The following DocumentRoot directive is the path to the directory where the website’s files are located: DocumentRoot /home/www.yourdomain.com/public_html The following CustomLog directive is the path to the log file that will contain the access infor- mation about the virtual host. In the “Understanding the httpd.conf File” section of this chap- ter, we discussed the options and settings for this directive. CustomLog /home/www.yourdomain.com/logs/access_log \ combined The ErrorLog directive was also discussed in the “Understanding the httpd.conf File” sec- tion in this chapter and it contains error information regarding the domain: ErrorLog /home/www.yourdomain.com/logs/error_log Finally, you close the Virtual Host configuration for this domain name by using the closing tag: </VirtualHost> Now that you have a good understanding of the virtual host configuration, let’s make another virtual host for your other domain name. Create a file located at /www/conf/www.yourotherdomain .com.conf and enter the following information: <VirtualHost *> ServerName www.yourotherdomain.com ServerAlias yourotherdomain.com DocumentRoot /home/www.yourotherdomain.com/public_html CustomLog /home/www.yourotherdomain.com/logs/access_log\ combined ErrorLog /home/www.yourotherdomain.com/logs/error_log </VirtualHost> The next task you will perform is to create an index.html file that will display a message indi- cating which virtual host is being displayed when you access the domain name. In your /home/www.yourdomain.com/public_html directory, create a file named index.html with the following contents: <html> <head><title>YourDomain.com</title></head> <body>Welcome to YourDomain.com</body> </html> 4337Book.fm Page 233 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 234 Chapter 8 • Apache Web Server: Installation and Configuration In your /home/www.yourotherdomain.com/public_html directory, create a file named index.html with the following contents: <html> <head><title>YourOtherDomain.com</title></head> <body>Welcome to YourOtherDomain.com</body> </html> Before you go any further, read the next section, “A Lesson in Testing Configuration File Changes.” After you have completed those steps, access your domain names configured in this section via your web browser and you should see the appropriate files for each virtual host. A Lesson in Testing Configuration File Changes Because adding your virtual hosts has been the first real editing you have done with your con- figuration files, you need to go through a small routine that prevents you from taking your web server offline. Because Apache will not read configuration files on the fly, any changes you make to the con- figuration files will require the server to be restarted to take effect. The kind developers of Apache have taken measures to prevent you from taking your server offline in the event you “fat-fingered” your way through the configuration file. You might have noticed in the “Becoming Familiar with Apache Programs” section of this chapter that we gave you a list of commands to run. The command for apachectl contains a special setting that enables you to test your configuration files while the server is still running and prevent you from taking your server offline. Let’s issue the apachectl configtest command now and check the output: Good syntax output: Processing config directory: /www/conf/vhosts/ Processing config file: /www/conf/vhosts/www.yourdomain.com.conf Syntax OK In this example, everything parsed as expected by the server and you are clear to start, stop, or restart your server as needed to make the changes take effect. Bad syntax output: Processing config directory: /www/conf/vhosts/ Processing config file: /www/conf/vhosts/www.yourdomain.com.conf Syntax error on line 2 of /www/conf/vhosts/www.yourdomain.com.conf: Invalid command 'Oops', perhaps mis-spelled or defined by a module not included in the server configuration In this example, you have a bad token on line 2 of your www.yourdomain.com.conf file. (We purposely entered the text Oops because we knew that it would cause a syntax error in Apache.) 4337Book.fm Page 234 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 235 Performing Other Apache Configurations After you get the Syntax OK output from your configuration files, you are clear to restart the server. We prefer to completely stop the server and then start it instead of using the restart command because we have had experiences in the past where the changes would not take effect by using the restart command. apachectl stop apachectl start Alternatively, if you are using SSL: apachectl stop apachectl startssl Now is a good time for you to check your website and ensure the changes have taken effect. Performing Other Apache Configurations Apache has many configurations you can use to enable directory listings, password-protect directories, enable cgi-bin directories, and more. We’ll cover the most requested and popular configurations now. Enabling Directory Listings Apache directory listings enable you to view the contents of a directory in your DocumentRoot as icons. There are a few things you must keep in mind before you attempt to perform this configuration: ● You must ensure that there are no files in the directory that are listed in your DirectoryIndex directive of the httpd.conf file. ● You must ensure that there are no files in the directory that you want the public to find. After you have met these criteria, you can begin. You have an option to directly enter these configuration directives into the httpd.conf, or alternatively you can enter these directives into a virtual host configuration file. It is up to you how you wish to organize. Here are the steps: 1. Create a directory in the www.yourdomain.com DocumentRoot: mkdir -p /home/www.yourdomain.com/public_html/listing 2. Add a few files to this directory for your listing purposes: cd /home/www.yourdomain.com/public_html/listing touch file1.txt touch file2.jpg touch file3.exe touch file4.tar.gz touch file5.zip 4337Book.fm Page 235 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 236 Chapter 8 • Apache Web Server: Installation and Configuration cd / chown -R someuser.somegroup listing 3. Add the configuration directives to the /www/conf/vhosts/www.yourdomain.com.conf file: <Directory /home/www.yourdomain.com/public_html/listing> Options +Indexes IndexOptions FancyIndexing IconsAreLinks </Directory> 4. Following the steps in the “A Lesson in Testing Configuration File Changes” section, run the configuration test and restart the server: apachectl configtest apachectl stop apachectl start Alternatively, if you are using SSL: apachectl configtest apachectl stop apachectl startssl 5. Access the www.yourdomain.com/listing directory in your web browser and you should see a list of icons like those in Figure 8.3. Let’s move on to password-protecting a directory on your virtual host. Password-Protecting Web Directories Password-protecting directories is another popular request item. Because of the complicated documentation in Apache, many people end up finding alternate means to perform this simple operation. We will make this easier for you! FIGURE 8.3 Apache directory listing 4337Book.fm Page 236 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 237 Performing Other Apache Configurations The password protection directives will almost always fall into a <Directory> section of your configuration files. As we mentioned in the previous section, “Enabling Directory Listings,” you are going to set up a directory on your virtual host, www.yourdomain.com. Follow these steps: 1. Prepare this directory: cd /home/www.yourdomain.com/public_html mkdir -p protected touch protected/index.html cd / chown -R someuser.somegroup protected 2. Edit the configuration file /www/conf/vhosts/www.yourdomain.com.conf and add the fol- lowing lines to it: <Directory /home/www.yourdomain.com/public_html/protected> AuthType Basic AuthName "Members Only" AuthUserFile /home/www.yourdomain.com/.htpasswd require valid-user </Directory> 3. Create the password file used by Apache to authenticate the username and password. We use the htpasswd program provided by Apache to generate the file. Simply run the following: cd /home/www.yourdomain.com /www/bin/htpasswd -c .htpasswd username You are prompted to enter and confirm the password for the user username. Additionally, a file is created at /home/www.yourdomain.com/.htpasswd. NOTE For more information about using .ht prefixed files, see the AccessFileName .htaccess entry in the “Understanding the httpd.conf File” section earlier in this chapter. 4. Run the commands to test your configuration and restart the server to make the changes take effect: apachectl configtest apachectl stop apachectl start Alternatively, if you are using SSL: apachectl configtest apachectl stop apachectl startssl 5. Open your web browser and try to access the password-protected directory: http:// www.yourdomain.com/protected . You should be prompted for a username and password. If you enter the correct username and password, you will be shown the index.html page, 4337Book.fm Page 237 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 238 Chapter 8 • Apache Web Server: Installation and Configuration which in this case is a blank page. If you do not enter the correct username or password, you might be prompted continuously until Apache displays an error page. TIP If you want to simplify the process of creating and configuring the password-protected direc- tories, you might be interested in the Apache Password Wizzard we have created (yes, that is spelled with two zs intentionally). It is located at www.apachefreaks.com/apache- password-wizzard.php. This Web-based script will take you through the steps in config- uring your password-protected directories and provide you with the password files and directives to configure the directories with ease. Configuring cgi-bin Directories Even though this book is written for PHP, we acknowledge that you might need to use CGI scripts occasionally. Therefore, we will show you how to create a cgi-bin directory to house your executable CGI scripts. 1. Let’s start by ensuring that your cgi-bin directory is created and has the proper permissions. mkdir -p /home/www.yourdomain.com/public_html/cgi-bin chmod 755 /home/www.yourdomain.com/public_html/cgi-bin chown -R someuser.somegroup /home/www.yourdomain.com/public_html/cgi-bin NOTE The traditional method of using cgi-bin directories is to place them outside of your doc- ument root and use the cgi-bin ScriptAlias and Alias directives. However, this method works and can be used if you understand that there might be minimal risks involved. 2. Edit the /www/conf/vhosts/www.yourdomain.com.conf file and add the following lines to the end of it: <Directory /home/www.yourdomain.com/public_html/cgi-bin> Options +ExecCGI AddHandler cgi-script cgi pl </Directory> 3. Because you are editing the www.yourdomain.com.conf file, you need to add one more line into the <VirtualHost> section. Add the following lines directly above the </VirtualHost> closing tag: ScriptAlias /cgi-bin/ /home/www.yourdomain.com/public_html/cgi-bin/ 4. Test your configuration file changes and restart the server. apachectl configtest apachectl stop apachectl start 4337Book.fm Page 238 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com 239 Performing Other Apache Configurations Alternatively, if you are using SSL: apachectl configtest apachectl stop apachectl startssl 5. Create a test CGI script to ensure this directory works properly. Create a file located at /home/www.yourdomain.com/public_html/cgi-bin/index.cgi and add the following contents: #!/usr/bin/perl print "Content-type: text/html print "This CGI Script Works Properly."; 6. Change the permissions and ownership of the script: cd /home/www.yourdomain.com/public_html chmod -R 755 cgi-bin chown -R someuser.somegroup cgi-bin 7. Execute this script in your web browser: www.yourdomain.com/cgi-bin/index.cgi and you should see a message that says This CGI Script Works Properly. If you see the message, then all is well. Your cgi-bin is working properly! Using .htaccess Files for Local Directory Configurations Editing the httpd.conf file for each directory configuration can become painful over time. You can, however, include per-directory settings in a file named .htaccess located in that directory. A rule of thumb is that any directive that will work in <Directory xxxx></Directory> settings for the httpd.conf file will work in the .htaccess. You might need to add something like this to the httpd.conf file: <Directory /path/to/directory> AllowOverride All </Directory> The previous settings will allow the .htaccess file in the directory to change any settings. You should determine which settings to use in the AllowOverride directive so that you do not open your system to security holes, especially if you are virtual hosting for clients. Configuration File Summary If you have performed all of these exercises, your configuration file should appear as in Listing 8.1. 4337Book.fm Page 239 Saturday, June 19, 2004 5:24 PM Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... Know the Apache programs and commands ● Understand the Apache configuration files ● Understand Apache Virtual Hosts ● Understand Apache directory listings ● Understand Apache password protection ● Understand using the Apache cgi-bin directories ● Get support for Apache Let’s move on to the next chapter, where you’ll learn about installing and administrating MySQL Simpo PDF Merge and Split Unregistered... sequel, is a language with which you “talk” to the database by using commands such as SELECT, and UPDATE While these two commands are ANSI SQL 99 commands, MySQL adds additional commands to help maximize the efficiency of your queries REPLACE and LIMIT are two excellent examples of such commands MySQL also adds alternate syntaxes for commands to make porting pre-built applications easier This allows fewer... at the different solutions available and learn why we are using MySQL as our database server of choice Advantages and Limitations of MySQL Let’s take a look at some of the key advantages and features offered by MySQL First up, we have the American National Standards Institute (ANSI) SQL syntax support MySQL supports a broad spectrum of the ANSI SQL 99 syntax commands SQL, pronounced sequel, is a language... http://www.simpopdf.com Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Chapter 9 MySQL: Installation and Administration • Understanding MySQL and Database Structure • Downloading MySQL • Installing MySQL • Configuring MySQL after Installation • Performing MySQL Administration • Performance and Replication Chapter 9 • MySQL: Installation and Administration 244 Simpo PDF Merge and Split Unregistered... to eliminate duplication of data and thereby save on file size, which becomes important when you are Chapter 9 • MySQL: Installation and Administration 246 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com storing millions of records However, when working with databases of this size, some duplication might be necessary to make queries significantly faster These duplications are... just means that while you are updating a specific table, no other process can interfere with it This ensures that the data will be updated correctly, and the next user or process attempting to read the data of the updated row will receive the most up- to-date results Next up we have the security features MySQL offers a flexible security package that even allows for SSL support A built-in user system... the GPL Whatever your decision, you can download MySQL Classic, MySQL Standard, MySQL Pro, or MySQL Max: MySQL Classic is the simplest of servers and contains only the standard MySQL storage engine MySQL Standard includes the standard MySQL storage engines and the InnoDB storage engine MySQL Pro is the licensed version of MySQL Standard MySQL Max contains many features not yet available in other releases... constant-width and is preceded with shell>, you will know that this is a command meant to be run from a command prompt Continued on next page Chapter 9 • MySQL: Installation and Administration 250 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com mysql> When you see a line of text that is constant-width and is preceded with mysql>, you will know that this is a command meant to... Server and Client RPMs Your Fedora system might already have MySQL installed, so you are going to remove the RPM packages from your system In the future, you will want to be able to control what patches are downloaded and applied, and what upgrades are made to the system and when As with Apache, the reasoning behind building your own installation is that most distributions such as Fedora, Red Hat, and. .. to specify directives and variables for MySQL, let's take a look at the final method of specifying a directive: as an option from the command line This is done by using the standard method of flags or options as in most command-line programs Simply use double dashes in front of your directive and specify the option For the complete list, see Appendix B or run the mysqld help command Configuring MySQL . 1.3 and 2.0. ● Know how to install Apache 1.3 by using multiple methods. ● Set up an SSL-enabled server. ● Understand Apache directories. ● Know the Apache programs and commands. ● Understand. “talk” to the database by using commands such as SELECT , and UPDATE . While these two commands are ANSI SQL 99 commands, MySQL adds addi- tional commands to help maximize the efficiency. offered by MySQL. First up, we have the American National Standards Institute (ANSI) SQL syntax support. MySQL supports a broad spectrum of the ANSI SQL 99 syntax commands. SQL , pronounced

Ngày đăng: 13/08/2014, 15:21

Từ khóa liên quan

Mục lục

  • Setting Up LAMP : Getting Linux, Apache, MySQL, and PHP Working Together

    • Inner Cover

    • Title Page

    • Copyright

    • Dedications

    • Acknowledgments

    • Contents at a Glance

    • Contents

      • Chapter 1 Introducing LAMP

      • Chapter 2 Installing Linux

      • Chapter 3 Using Linux

      • Chapter 4 Linux Administration

      • Chapter 5 Network Connectivity

      • Chapter 6 Linux Security

      • Chapter 7 Electronic Mail

      • Chapter 8 Apache Web Server: Installation and Configuration

      • Chapter 9 MySQL: Installation and Administration

      • Chapter 10 PHP: Installation and Configuration

      • Chapter 11 Testing Your LAMP Installation

      • Appendix A LAMP Quick Installation

      • Appendix B MySQL Configuration Directives

      • Appendix C Getting Support

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan