rhel5上的LAMP配置
LAMP代表Linux Apache Mysql和PHP
对于希望在LINUX平台上设计,开发,托管或者基于Web的应用程序的所有人员,LAMP都是有用的。
在这里我们学习如何配置Apache Mysql以使用PHP构建或者Web应用程序。
步骤1-登录到Linux系统,此处系统为RHEL5.4
步骤2-配置yum服务器以进行软件包安装
让我们在这里简要了解yum服务器配置
从RHEL5 DVD/IMAGE复制Server文件夹/目录,我们在Linux机器中的任何位置。
我将其复制到root桌面上。
现在在/etc/yum.repos.d/目录中创建一个theitroad.repo文件。
#vim /etc/yum.repos.d/theitroad.repo [theitroad] baseurl=file:///root/Desktop/Server gpgcheck=0
现在保存文件并退出
:wq!
现在,yum服务器就可以使用了。
我们可以在这里阅读YUM服务器教程。
第3步-安装和配置Apache服务器
由于PHP是一种服务器端脚本语言,因此我们需要一个Web服务器来运行php程序或者脚本,因此为了在Linux机器上实现这一点,我们将配置Web Server(Apache).apache服务器的基本配置变得非常容易一旦正确配置了yum服务器,在以下步骤中,我们将学习如何通过几个简单的步骤配置apache服务器。
要安装的软件包,要启动的服务,配置文件是什么以及其中进行更改。
#yum install httpd* ====>> this will install required packages for apache. #service httpd restart ====>> this will restart the apache service temporarily. #chkconfig httpd on =====>>this will on httpd service permanently.
第4步-安装和配置Mysql Server
#yum install mysql* ===========>> This will install mysql server required packages with dependencies. #service mysqld restart ===========>> This will restart mysql service. #chkconfig mysqld on ===========>> This will enable mysql services for permanent use.
(A)现在,我们需要更改root用户的mysql密码。
#mysql >USE mysql; >UPDATE user SET password=PASSWORD('redhat123456') WHERE user='root'; >FLUSH PRIVILEGES; >quit
注意:在上面的命令中,root用户是user,root用户的mysql密码是redhat123456.
(B)现在通过登录到mysql检查上述配置
#mysql -u root -p password:redhat123456
如果我们获得登录名,那么它可以正常工作,一切都很好。
第5步-现在使用apache服务器安装和配置PHP。
#yum install php* ========>>this will install all php packages with dependency.
配置Apache以运行PHP脚本。
这是http的配置文件,我们需要其中进行更改,以便默认情况下,我们的网络服务器会读取告诉我们要读取的页面,也告诉他们仅从给定目录中读取文件。
我们可以在此处分配站点名称并决定存储日志文件的位置。
转到apache配置文件/etc/httpd/conf/httpd.conf的最后一行,并添加以下行。
<VirtualHost 192.168.1.1:80> # ServerAdmin [email protected] DocumentRoot /var/www/html DirectoryIndex lamp.php ServerName theitroad.com # ErrorLog logs/dummy-host.example.com-error_log # CustomLog logs/dummy-host.example.com-access_log common </VirtualHost>
然后转到文档根目录,并其中创建php脚本。
#cd /var/www/html ========>>go to apache web directory where we create php pages. #vim lamp.php =========>> php scripts with name lamp.php <?php phpinfo(); ?>
保存文件并退出
phpinfo是一个PHP实用程序功能,可显示php配置。
步骤6-现在在系统中配置了LAMP。
要检查它,请打开浏览器
并在URL http://localhost中输入以下内容
或者输入Linux系统的http://ipaddress 像http://192.168.1.1
注意:我们上面运行的php脚本或者任何PHP脚本都不会受到浏览器的影响。
众所周知,PHP是一种服务器端脚本语言,因此它总是受服务器而不是浏览器类型的影响。
我们始终需要一台服务器运行一个PHP脚本。
PHP安装和配置:
如何在Linux中安装PHP?
[root@theitroad ~]# yum install php*
如何配置PHP?
Linux中PHP的配置文件是/etc/php.ini。
现在我们可以根据需要在php配置文件中进行许多更改.php的每个更改都在此php配置文件中进行。
1.To increase the memory limit. memory_limit = 256M 2.to increase maximum execution time for a php program. max_execution_time = 210 3.To increase maximum upload size of a php script. max_upload_size = 100M
这样,我们可以根据需要在php配置文件中进行很多更改。
所有可能的方式来编写PHP脚本。
其中我已经提到了编写php程序的所有可能方法。
现在取决于我们选择要使用的方法。
方法1:
<? your PHP Code here. ?>
方法2:
<?php OUR PHP CODE ?>
方法3:
<?php PHP CODE http://webscience.ru/special-loan php?>
方法4:
<script language="php"> then write code here. </script>
如何在文件中一起编写PHP HTML和这两个代码?
现在,如果有人想在一个文件中同时编写html和php,则可以执行以下操作。
我们可以在下面的脚本中了解如何将php和html代码一起编写在一个文件中。
#vim theitroad.php <html> <head> <title>HTML PHP COMBINE CODE</title> </head> <body> <h1>this code will show you how to write html and php code in a single file</h> </body> </html> <?php print "hello"; <?
如何在PHP脚本中编写HTML代码?
现在这里是将这两个(html和php)一起编写的另一种方法。
这里我们学习如何在php脚本中编写html代码。
下面是一个简单的示例,使我们学习在php程序中编写html代码。
看到它,然后编写自己的代码并进行测试。
#vim theitroad.php <?php echo "<html>"; echo "<body>"; echo "<title> Learn how to write html inside php. </title>"; echo "<h1> HTML inside PHP </h1>"; echo"</body>"; echo "</html>"; ?>
PHP HTML组合编码:
这是编写html和php组合编码的另一种方法,非常易于学习和实现。
#vim tiwary.php <html> <head> <title>php html combined codes</title> </head> <body> <?php here we write PHP Codes ?> </body> </html>
MySQL的PHP
让我们看看在Linux上安装,配置和运行mysql的最简单方法。
[root@theitroad ~]# yum install mysql* [root@theitroad ~]# service mysqld restart [root@theitroad ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MySQL to secure it, we'll need the current password for the root user. If you've just installed MySQL, and you 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): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MySQL root user without the proper authorisation. Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y ... Success! By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL!
现在,通过用户root和上面我们分配给root用户的密码登录mysql服务器。
[root@theitroad ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.0.77 Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | tiwary | +--------------------+ 3 rows in set (0.01 sec) mysql> quit Bye [root@theitroad ~]#
现在学习如何在MYSQL中创建和使用数据库。
mysql> create database linux; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | linux | | mysql | | tiwary | +--------------------+ 4 rows in set (0.00 sec) mysql> use linux; Database changed mysql> show tables; Empty set (0.00 sec) mysql> exit Bye [root@theitroad ~]#
我们在上面的代码中创建了一个名为linux的数据库。
PHP与mysql的连接
现在在LAMP或者Web开发领域中,与数据库的php连接是最重要的任务之一。
其中我们将学习如何将php脚本与我们的数据库连接。
在LAMP中,我们正在使用的数据库是Mysql。
[root@theitroad html]# vim index.php <html> <head> <title>Checking database Connection with PHP</title> </head> <body> <?php $dbhost='localhost:3306'; $dbuser='root'; $dbpass='redhat'; $conn=mysql_connect($dbhost,$dbuser,$dbpass); if(! $conn) { die('not possible to connect:' . mysql_error()); } echo 'Successfully connected with database<br; ?> </body> </html>
现在检查一下,我们可以转到浏览器并在URL中键入http://localhost。
上面的代码说明:
$dbhost='localhost:3306'; ===>>here localhost is hostname and 3306 is mysql port. $dbuser='root'; ===>>here root is the username of mysql database. $dbpass='redhat'; ===>>here redhat is the password of user root.