电影《社交网络》中,facebook创始人马克.扎克失恋后入侵哈佛大学宿舍楼服务器,窃取数据库资料,并在两个小时内完成了一个给校内女生评分的交互网站,该网站一天内点击数过10W,直接导致学校服务器崩溃。。。。。。
其实,构建那样一个网站并不需要很多高深的技术,任何一个有一定网络编程基础的人都可以做到。马克构建网站所用的是Apache服务器和PHP服务器脚本语言,我曾经用三天时间学习PHP,然后用了两个星期为学校写了一个网站,可见PHP是很简单的,而facebook后台也是用php写的,当然,大家现在看到的我的wordpress博客后台也是php~~。
话说回来,很多人可能已经用上ubuntu了,大家可能花了大量时间在ubuntu的美化上,这无可厚非,但是,ubuntu应该给我们的工作和学习带来更多的便利和方便。ubuntu作为linux,为我们提供了强大的网络方面的功能,其中的服务器技术尤其强大,安全。如果不学习linux的网络技术,很难说自己掌握了linux。
今天,我在ubuntu下配置了一个Apache服务器。通过Apache我们可以学习php网络编程,可以用它来部署自己本地的wordpress博客,从而进一步通过网络和朋友交流。从此,你将深刻体会到网络带个我们的神奇力量,至少我是这样觉得的~~
步骤一,安装apache2
1
|
sudo apt-get install apache2
|
安装完成。
运行如下命令重启下:
1
|
sudo /etc/init.d/apache2 restart
|
在浏览器里输入http://localhost或者是http://127.0.0.1,如果看到了It works!,那就说明Apache就成功的安装了,Apache的默认安装,会在/var下建立一个名为www/html的目录,这个就是Web目录了,所有要能过浏览器访问的Web文件都要放到这个目录里。
1
|
Ubuntu下Apache重启错误:Could not reliably determine解决
|
安装完成。
运行如下命令重启下:
错误信息:
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
解决方法:
sudo gedit /etc/apache2/apache2.conf
在文件后面加上:
#Server Name
ServerName 127.0.0.1
步骤二,安装mysql数据库:
1
|
sudo apt-get install mysql-server mysql-client
|
apt-get程序会自动下载安装最新的mysql版本。在安装的最后,它会要求里输入root的密码,注意,这里的root密码可不是Ubuntu的root密码啊,是你要给MySQL设定的root密码。
步骤四,安装phpmyadmin-Mysql数据库管理
1
|
sudo apt-get install phpmyadmin
|
phpmyadmin设置:
在 安装过程中会要求选择Web server:apache2或lighttpd,使用空格键选定apache2,按tab键然后确定。然后会要求输入设置的Mysql数据库密码连接密 码Password of the database’s administrative user。
然后将phpmyadmin与apache2建立连接,以我的为例:www/html目录在/var/www/html,phpmyadmin在/usr/share /phpmyadmin目录,所以就用命令:
1
|
sudo ln -s /usr/share/phpmyadmin /var/www/html
|
建立链接。
phpmyadmin测试:在浏览器地址栏中打开http://localhost/phpmyadmin。
以上ALMP的基本组件就安装完毕了,下面我们再来看一些其他的设置:
步骤三,安装php:
1
|
sudo apt-get install libapache2-mod-php5 php5
|
此外,建议安装扩展php5-gd php5-mysql,安装方式同上.
安装完后,我们要重新启动Apache,让它加载PHP模块:
1
|
sudo /etc/init.d/apache2 restart
|
接下来,我们就在Web目录下面新建一个test.php文件来测试PHP是否能正常的运行,命令:
1
|
sudo gedit /var/www/html/test.php
|
然后输入:
1
2
|
<?php echo "hello,world!!"?>
|
接着保存文件,在浏览器里输入http://127.0.0.1/test.php,如果在网页中显示hello,world!!,那就说明PHP已经正常运行了。
步骤五,设置Ubuntu文件执行读写权限
LAMP组建安装好之后,PHP网络服务器根目录默认设置是在:/var/www/html。由于Linux系统的安全性原则,改目录下的文件读写权限是只允许root用户操作的,所以我们不能在www/html文件夹中新建php文件,也不能修改和删除,必须要先修改/var/www/html目录的读写权限。在界面管理器中通过右键属性不能修改文件权限,得执行root终端命令:
1
|
sudo chmod 777 /var/www/html
|
。然后就可以写入html或php文件了。777是linux中的最高权限,表示可读,可写,可执行。
If you are 外国人:https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu
For Ubuntu 12.04 - see this updated tutorial for Ubuntu 14.04.
LAMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Apache, MySQL, and PHP. Since the virtual private server is already running Ubuntu, the linux part is taken care of. Here is how to install the rest.
The steps in this tutorial require the user to have root privileges on your VPS. You can see how to set that up in the Initial Server Setup in steps 3 and 4.
Apache is a free open source software which runs over 50% of the world’s web servers.
To install apache, open terminal and type in these commands:
sudo apt-get update sudo apt-get install apache2
That’s it. To check if Apache is installed, direct your browser to your server’s IP address (eg. http://12.34.56.789). The page should display the words “It works!" like this.
You can run the following command to reveal your server’s IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'
MySQL is a powerful database management system used for organizing and retrieving data
To install MySQL, open terminal and type in these commands:
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
During the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell.
Once you have installed MySQL, we should activate it with this command:
sudo mysql_install_db
Finish up by running the MySQL set up script:
sudo /usr/bin/mysql_secure_installation
The prompt will ask you for your current root password.
Type it in.
Enter current password for root (enter for none):OK, successfully used password, moving on...
Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps.
It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes.
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...
Once you're done with that you can finish up by installing PHP.
PHP is an open source web scripting language that is widely use to build dynamic webpages.
To install PHP, open terminal and type in this command.
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt
After you answer yes to the prompt twice, PHP will install itself.
It may also be useful to add php to the directory index, to serve the relevant php index files:
sudo nano /etc/apache2/mods-enabled/dir.conf
Add index.php to the beginning of index files. The page should now look like this:
<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>
PHP also has a variety of useful libraries and modules that you can add onto your virtual server. You can see the libraries that are available.
apt-cache search php5-
Terminal will then display the list of possible modules. The beginning looks like this:
php5-cgi - server-side, HTML-embedded scripting language (CGI binary) php5-cli - command-line interpreter for the php5 scripting language php5-common - Common files for packages built from the php5 source php5-curl - CURL module for php5 php5-dbg - Debug symbols for PHP5 php5-dev - Files for PHP5 module development php5-gd - GD module for php5 php5-gmp - GMP module for php5 php5-ldap - LDAP module for php5 php5-mysql - MySQL module for php5 php5-odbc - ODBC module for php5 php5-pgsql - PostgreSQL module for php5 php5-pspell - pspell module for php5 php5-recode - recode module for php5 php5-snmp - SNMP module for php5 php5-sqlite - SQLite module for php5 php5-tidy - tidy module for php5 php5-xmlrpc - XML-RPC module for php5 php5-xsl - XSL module for php5 php5-adodb - Extension optimising the ADOdb database abstraction library php5-auth-pam - A PHP5 extension for PAM authentication [...]
Once you decide to install the module, type:
sudo apt-get install name of the module
You can install multiple libraries at once by separating the name of each module with a space.
Congratulations! You now have LAMP stack on your droplet!
Although LAMP is installed, we can still take a look and see the components online by creating a quick php info page
To set this up, first create a new file:
sudo nano /var/www/info.php
Add in the following line:
<?php phpinfo(); ?>
Then Save and Exit.
Restart apache so that all of the changes take effect:
sudo service apache2 restart
Finish up by visiting your php info page (make sure you replace the example ip address with your correct one): http://12.34.56.789/info.php
It should look similar to this.
After installing LAMP, you can Set Up phpMyAdmin, Install WordPress, go on to do more with MySQL (A Basic MySQL Tutorial), Create an SSL Certificate, or Install an FTP Server.