文章目錄
安裝Httpd
1. 安裝httpd。先檢查一下httpd是否已經有安裝:
[root@centos-server ~]$ rpm -qa | grep httpd
如果httpd沒有安裝,可以使用下面l命令進行安裝:
[root@centos-server ~]$ yum install httpd
[root@centos-server ~]$ chkconfig httpd on
[root@centos-server ~]$ service httpd start
2. 測試httpd。httpd的主目錄位於/var/www/html。我們在該目錄下建立一個index.html檔案,測試是否可以正常訪問。
[root@centos-server /var/www/html]$ echo "<h1>It Works</h1>" > index.html[root@centos-server /var/www/html]$ curl http://localhost/index.html<h1>It Works</h1>
3. 配置防火牆。CentOS預設的防火牆策略如下:
[root@centos-server /var/www/html]$ iptables -L -n --line-numbersChain INPUT (policy ACCEPT)num target prot opt source destination 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT)num target prot opt source destination 1 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT)num target prot opt source destination
該策略阻止了其它機器除了22(SSH)連接埠之外的所有連接埠對原生訪問。httpd服務需要開啟80連接埠,按照下面的方式開啟80連接埠:
[root@centos-server ~]$ iptables -I INPUT 5 -p tcp -m tcp --dport 80 -m state --state NEW -j ACCEPT
[root@centos-server ~]$ service iptables saveiptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@centos-server ~]$ service iptables restartiptables: Flushing firewall rules: [ OK ]iptables: Setting chains to policy ACCEPT: filter [ OK ]iptables: Unloading modules: [ OK ]iptables: Applying firewall rules: [ OK ]
OK,現在用其它機器訪問一下http://192.168.12.215/index.html(伺服器IP地址為192.168.12.215)試試看。
安裝MySQL
1. 安裝MySQL。先檢查一下mysql-server是否已經有安裝:
[root@centos-server ~]$ rpm -qa | grep mysql-server
如果mysql-server沒有安裝,可以使用下面l命令進行安裝:
[root@centos-server ~]$ yum install mysql-server
[root@centos-server ~]$ chkconfig mysqld on
[root@centos-server ~]$ service mysqld start
2. 配置MySQL。MySQL提供了一個名為mysql_secure_installation的工具,協助我們在安裝完畢後做一些必要的安全設定。
[root@centos-server ~]$ mysql_secure_installation ... ...In order to log into MySQL to secure it, we'll need the currentpassword for the root user. If you've just installed MySQL, andyou haven't set the root password yet, the password will be blank,so you should just press enter here.Enter current password for root (enter for none): ###輸入當前的root密碼, 預設為空白OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MySQLroot user without the proper authorisation.Set root password? [Y/n] ###是否需要重設root密碼?New password: Re-enter new password: Password updated successfully!Reloading privilege tables.. ... Success!By default, a MySQL installation has an anonymous user, allowing anyoneto log into MySQL without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] ###是否移除匿名使用者? ... Success!Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] ###是否禁用root遠端連線? ... Success!By default, MySQL comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] n ###是否刪除test資料庫?n... skipping.Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Y/n] ###重新載入授權資訊? ... Success!Cleaning up...All done! If you've completed all of the above steps, your MySQLinstallation should now be secure.Thanks for using MySQL!
3. 檢查MySQL是否正常運行。
[root@centos-server ~]$ mysqladmin -h localhost -u root -p versionEnter password: mysqladmin Ver 8.42 Distrib 5.1.61, for redhat-linux-gnu on x86_64Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Server version 5.1.61Protocol version 10Connection Localhost via UNIX socketUNIX socket /var/lib/mysql/mysql.sockUptime: 16 min 14 secThreads: 1 Questions: 21 Slow queries: 0 Opens: 15 Flush tables: 1 Open tables: 8 Queries per second avg: 0.21
安裝PHP
1. 安裝php。先檢查一下php是否已經有安裝:
[root@centos-server ~]$ rpm -qa | grep php
如果httpd沒有安裝,可以使用下面l命令進行安裝:
[root@centos-server ~]$ yum install php
2. 測試php。建立檔案hello-world.php,檔案內容如下:
<?phpprint "Hello World!\n";?>
執行hello-world.php,檢查PHP是否有正常運行:
[root@centos-server ~]$ php -f hello-world.php Hello World!
3. 測試與httpd的整合。在/var/www/html下建立檔案php-info.php,檔案內容如下:
<?phpphpinfo();?>
使用瀏覽器請求PHP檔案之前必須重啟httpd:
[root@centos-server /var/www/html]$ service httpd restart
然後開啟網址http://192.168.12.215/php-info.php試試看:
4. 安裝php-mysql模組。預設php沒有安裝該模組。
[root@centos-server ~]$ yum install php-mysql
[root@centos-server ~]$ service httpd restart ###需要重啟httpd
再次開啟網址http://192.168.12.215/php-info.php,發現找到mysql模組:
其它的php模組也可以比照此種方式安裝。使用下面的命令可以查詢在CentOS軟體源中可用的php模組:
[root@centos-server ~]$ yum search phpcups-php.x86_64 : Common Unix Printing System - php modulegraphviz-php.x86_64 : PHP extension for graphvizphp.x86_64 : PHP scripting language for creating dynamic web sitesphp-bcmath.x86_64 : A module for PHP applications for using the bcmath libraryphp-cli.x86_64 : Command-line interface for PHPphp-common.x86_64 : Common files for PHPphp-dba.x86_64 : A database abstraction layer module for PHP applicationsphp-devel.x86_64 : Files needed for building PHP extensionsphp-embedded.x86_64 : PHP library for embedding in applicationsphp-gd.x86_64 : A module for PHP applications for using the gd graphics libraryphp-imap.x86_64 : A module for PHP applications that use IMAPphp-intl.x86_64 : Internationalization extension for PHP applicationsphp-ldap.x86_64 : A module for PHP applications that use LDAPphp-mbstring.x86_64 : A module for PHP applications which need multi-byte string handlingphp-mysql.x86_64 : A module for PHP applications that use MySQL databasesphp-odbc.x86_64 : A module for PHP applications that use ODBC databasesphp-pdo.x86_64 : A database access abstraction module for PHP applicationsphp-pear.noarch : PHP Extension and Application Repository frameworkphp-pecl-apc.x86_64 : APC caches and optimizes PHP intermediate codephp-pgsql.x86_64 : A PostgreSQL database module for PHPphp-process.x86_64 : Modules for PHP script using system process interfacesphp-pspell.x86_64 : A module for PHP applications for using pspell interfacesphp-recode.x86_64 : A module for PHP applications for using the recode libraryphp-snmp.x86_64 : A module for PHP applications that query SNMP-managed devicesphp-soap.x86_64 : A module for PHP applications that use the SOAP protocolphp-tidy.x86_64 : Standard PHP module provides tidy library supportphp-xml.x86_64 : A module for PHP applications which use XMLphp-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocolphp-zts.x86_64 : Thread-safe PHP interpreter for use with the Apache HTTP Serverrrdtool-php.x86_64 : PHP RRDtool bindingsuuid-php.x86_64 : PHP support for Universally Unique Identifier libraryphp-enchant.x86_64 : Human Language and Character Encoding Supportphp-pecl-apc-devel.x86_64 : APC developer files (header)php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon