LAMP is short forLinux,Apache,Mysql,PHP. This tutorial shows how you can install an Apache web server on an Ubuntu 16.04 LTS (Xenial Xerus) server with PHP 7 (mod_PHP) and MysqL / MariaDB support and how to setup an SSL certificate with Let's encrypt. Additionally,I will install PHPMyAdmin to make MysqL administration easier. A LAMP setup is a perfect basis for CMS systems like Joomla,wordpress or Drupal.

Preliminary Note

In this tutorial,I will use the hostnameserver1.example.comwith the IP address192.168.1.100. These settings might differ for you,so you have to replace them where appropriate.

I recommend to use a minimal Ubuntu server setup as basis for the tutorial,that can be a virtual- or root server image with an Ubuntu 16.04 minimal install from a web hosting company or you use ourminimal server tutorialto install a server from scratch.

I'm running all the steps in this tutorial with root privileges,so make sure you're logged in as root:

sudo su

1. Installing MysqLor MariaDB

There are currently two widely used MysqL database systems available,the classic "MysqL" server which is developed by Oracle and is available in version 5.7 Now and the MysqL fork named MariaDB which is developed by the original MysqL developer Monty Widenius.


I will show you how to install both alternatives below. Just follow either chapter 1.1 or 1.2 but not both. I will use MysqL 5.7 for the virtual machine image that can be downloaded from Howtoforge.


1.1 InstallMysqL 5.7

To install MysqL 5.7,execute this command:

apt-get -y install MysqL-server MysqL-client

The packages MysqL-server and MysqL-client are so called 'Meta-packages',they install always the latest MysqL version that is available from Ubuntu. Th latest version is currently MysqL 5.7.

We have set the root password for MysqL already during installation,but I would like to remove the anonymous user and test database for security reasons. Run the MysqL_secure_installation command below to achieve that.

MysqL_secure_installation

You will be asked these questions:

Securing the MysqL server deployment.

Enter password for user root:<-- Enter the MysqL root password

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes,any other key for No:
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes,any other key for No) :<-- Choose 'y' here if you like to enable the password validation,I don't need that function,so I choose 'n' here.

... skipping.
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? (Press y|Y for Yes,serif;text-decoration:underline;"><-- 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? (Press y|Y for Yes,mono;font-size:14px;font-style:italic;white-space:normal;">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? (Press y|Y for Yes,serif;text-decoration:underline;"><-- 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? (Press y|Y for Yes,mono;font-size:14px;font-style:italic;white-space:normal;">All done!

The MysqL setup has been secured Now.

1.2 Install MariaDB 10

Run the following command to install MariaDB-server and client:

apt-get -y install mariadb-server mariadb-client

Now we set a root password for MariaDB.

Enter current password for root (enter for none):<-- press enter
Set root password? [Y/n]<-- y
New password:<-- Enter the new MariaDB root password here
Re-enter new password:<-- Repeat the password
Remove anonymous users? [Y/n]<-- y
disallow root login remotely? [Y/n]<-- y
Reload privilege tables Now? [Y/n]<-- y

Test the login to MariaDB with the "MysqL command"

MysqL -u root -p

and enter the MariaDB root password that you've set above. The result should be similar to the screenshot below:

MariaDB Login on Ubuntu 16.04.

To leave the MariaDB shell,enter the command "quit" and press enter.

2. Install Apache 2.4

Apache 2 is available as an Ubuntu package,therefore we can install it like this:

apt-get -y install apache2

Now direct your browser tohttp://192.168.1.100,and you should see the Apache2 default page (It works!):

apache_ubuntu_default_page.png

The document root of the apache default vhost is/var/www/htmlon Ubuntu and the main configuration file is/etc/apache2/apache2.conf.The configuration system isfully documented in/usr/share/doc/apache2/README.Debian.gz.

3. Install PHP 7

We can install PHP 7 and the Apache PHP module as follows:

apt-get -y install PHP7.0 libapache2-mod-PHP7.0

Then restart Apache:

systemctl restart apache2


4. Test PHP and get details about your PHP installation

The document root of the default web site is/var/www/html. We will Now create a small PHP file (info.PHP) in that directory and call it in a browser. The file will display lots of useful details about our PHP installation,such as the installed PHP version.

nano /var/www/html/info.PHP

<?PHP
PHPinfo();
?>

Then change the owner of the info.PHP file to the www-data user and group.

chown www-data:www-data/var/www/html/info.PHP

Now we call that file in a browser (e.g.http://192.168.1.100/info.PHP):

PHPINFO output from PHP 7.

As you see,PHP 7.0 is working,and it's working through theApache 2.0 Handler,as shown in theServer APIline. If you scroll further down,you will see all modules that are already enabled in PHP5. MysqL is not listed there which means we don't have MysqL / MariaDB support in PHP yet.

5. Get MysqL / MariaDB support in PHP

To get MysqL support in PHP,we can install thePHP7.0-MysqLpackage. It's a good idea to install some other PHP modules as well as you might need them for your applications. You can search for available PHP modules like this:

apt-cache search PHP7.0

Pick the ones you need and install them like this:

apt-get -y install PHP7.0-MysqL PHP7.0-curl PHP7.0-gd PHP7.0-intl PHP-pear PHP-imagick PHP7.0-imap PHP7.0-mcrypt PHP-memcache PHP7.0-pspell PHP7.0-recode PHP7.0-sqlite3 PHP7.0-tidy PHP7.0-xmlrpc PHP7.0-xslPHP7.0-mbstringPHP-gettext

Now restart Apache2:

PHP with MySQL / MariaDB support.

PHP 7 has Now MysqL / MariaDB support as shown in PHPinfo() above.

6. Install the Opcache + APCu PHP cache to speed up PHP

PHP 7 ships with a built-in opcode cacher for caching and optimizing PHP intermediate code,it has the name 'opcache' and is available in the package PHP7.0-opcache. It is strongly recommended to have an Opcache installed to speed up your PHP page. Besides opcache,I will install APCu which is a compatibility wrapper for opcache to provide the functions of the APC cache,an often used caching system in PHP 5.x versions and many CMS systems still use it.

Opcache and APCucan be installed as follows:

apt-get -y installPHP7.0-opcache PHP-apcu

Don't worry if it shows that Opcache is already installed.

Now restart Apache:

Now reloadhttp://192.168.1.100/info.PHPin your browser and scroll down to the modules section again. You should Now find lots of new modules there:

APCu installed in PHP.

Please don't forget to delete the info.PHP file when you don't need it anymore as it provides sensitive details of your server. Run the following command to delete the file.

rm -f/var/www/html/info.PHP

7. Enable the SSL website in apache

SSL/ TLS is a security layer to encrypt the connection between the web browser and your server. Most web browsers start to show sites as insecure today when the connection between the server and the web browser is not encrypted with SSL. In this chapter,I will show you how to secure your website with SSL.

Execute the following commands on your server to enable SSL (https://) support. Run:

a2enmod ssl
a2ensite default-ssl

which enables the SSL module and adds a symlink in the /etc/apache2/sites-enabled folder to the file/etc/apache2/sites-available/default-ssl.conf to include it into the active apache configuration. Then restart apache to enable the new configuration:

Now test the SSL connection by openinghttps://192.168.1.100in a web browser.

Accept SSL Warning in Firefox

You will receive an SSL warning as the SSL certificate of the server is a "self-signed" SSL certificate,this means that the browser does not trust this certificate by default and you have to accept the security warning first. After accepting the warning,you will see the apache default page.

Apache default page in https mode.

The closed "Green Lock" in front of the URL in the browser shows that the connection is encrypted.

There are two ways to get rid of the SSL warning,either replace the self-signed SSL certificate/etc/ssl/certs/ssl-cert-snakeoil.pem with an officially signed SSL certificate that you buy from an SSL Authority or you get a free SSL certificate from Let's encrypt,which I will describe in chapter 8.


8. Get a free SSL Certificate from Let's Encrypt

The first step to secure the website with a Let's Encrypt SSL Certificate is to install thepython-letsencrypt-apachepackage. Run the following command:

apt-get -y install python-letsencrypt-apache

In the next step,we will request an SSL cert from Let's Encrypt,during this process,the Let's Encrypt server tries to connect to your server trough the domain name that you provide to the letsencrypt command. It is important that this domain name points to your server in DNS already so that the website is reachable by its domain name on port 80 (http) already. If the website is not reachable from the internet,then the creation of the Let's Encrypt SSL certificate will fail.

Before we can start to create the SSL cert,set the domain name in the vhost configuration file. Open the default vhost file with an editor:

nano /etc/apache2/sites-available/000-default.conf

and add the line:

ServerName example.com

Right below the 'DocumentRoot' line. Replace example.com with the domain name of your own website.

Then create the SSL Certificate with this command:

letsencrypt --apache -d example.com

Replace example.com with your domain name here again. The command will start a wizard that asks you several questions.

Enter the email address where the administrator who is responsible for this website can be reached.

letsencrypt-email.png

Accept the terms and conditions of the Let's Encrypt SSL authority.

letsencrypt-terms.png

Choose if you want to redirect non-SSL requests to https:// automatically. I'll select yes here to avoid duplicate content problems when the website is available as http:// and https:// version.

letsencrypt-ssl-only.png

The SSL certificate has been issued successfully.

letsencrypt-success.png

When you access the website Now with a browser,you will get redirected automatically to SSL and the green lock in front of the URL bar in the browser shows that we are using a trusted SSL certificate Now.

https-secure-letsencrypt.png

8.1 Let's encrypt Auto renewal

Let's Encrypt SSL certificates are valid for a short period of 80 days only. Therefore we will setup a cronjob Now to auto-renew the SSL certificate when necessary. The command is 'letsencrypt renew'.

Setup a cronjob for LE auto renewal. Run:

crontab -e

to open the root crontab in an editor. Insert the following line at the end of the file:

01***/usr/bin/letsencryptrenew&>/dev/null

save the file,this will activate the cronjob. This cronjob will call the Let's Encrypt renew command every night at 1 am. The command will renew the SSL cert only when necessary(30 days before it expires),there is no problem to run it every night.

9. Install PHPMyAdmin

phpMyAdminis a web interface through which you can manage your MysqL databases. It's a good idea to install it:

apt-get -y install PHPmyadmin

You will see the following questions:

Web server to configure automatically:<-- Select the option: apache2
Configure database for PHPmyadmin with dbconfig-common?<-- Yes
MysqL application password for PHPmyadmin:<-- Press enter,apt will create a random password automatically.

9.1Root access to PHPMyAdmin with MariaDB

The following step is required for MariaDB installations only,if you use MysqL 5.7,then skip this step.

MariaDB enables a plugin called "unix_socket" for the root user by default,this plugin prevents that the root user can log into PHPMyAdmin and that TCP connections to MysqL are working for the root user. To get a user with privileges to create other users and databases in PHPMyAdmin,I will create a new MysqL user with the name "admin" with the same privileges than the root user.

Login to the MysqL database as root user on the shell:

MysqL -u root

Create a new user with the name "admin" and password "howtoforge". Replace the password "howtoforge" with a secure password in the commands below!

CREATE USER 'admin'@'localhost' IDENTIFIED BY 'howtoforge';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit

Afterward,you can access PHPMyAdmin underhttp://192.168.1.100/PHPmyadmin/:

Login to phpmyadmin

PHPMyAdmin

10 Virtual machine image download of this tutorial

This tutorial is available as ready to use virtual machine image in ovf/ova format that is compatible with VMWare and VirtualBox. The virtual machine image uses the following login details:

SSH / Shell Login

Username: administrator
Password: howtoforge

This user has sudo rights.

MysqL Login

Username: root
Password: howtoforge

The IP of the VM is 192.168.1.100,it can be changed in the file /etc/network/interfaces. Please change all the above passwords to secure the virtual machine.

11 Links

  • Apache:http://httpd.apache.org/

  • PHP:http://www.php.net/

  • MysqL:http://www.mysql.com/

  • MariaDB:https://mariadb.com/

  • Ubuntu:http://www.ubuntu.com/

  • PHPMyAdmin:http://www.phpmyadmin.net/

Ubuntu 16.04 LAMP server tutorial with Apache 2.4, PHP 7 and MySQL的更多相关文章

  1. 从iOS应用程序发送帖子到PHP脚本不工作…简单的解决方案就像

    我之前已经做了好几次了但是由于某些原因我无法通过这个帖子…我尝试了设置为_POST且没有的变量的PHP脚本……当它们未设置为发布时它工作精细.这是我的iOS代码:这里是PHP的一大块,POST变量不在正确的位置?我想这对于更有经验的开发人员来说是一个相当简单的答案,感谢您的帮助!解决方法$_POST是一个数组,而不是一个函数.您需要使用方括号来访问数组索引:

  2. swift学习2 元组 tuples

    swift中出现了一种新的数据结构,非常牛掰的元组tuples如果懂PHP的猿,会发现这个元组和PHP的数组非常类似,同样是可以默认不指定key,也可以指定key目前的学习疑问是,如何进行元组的遍历?

  3. 尝试使用swift mailer,gmail smtp,php发送邮件

    这里是我的代码:在运行时出现此错误…

  4. Android – 将SQLite与MySQL同步的最佳方式

    参见英文答案>Synchronizingclient-serverdatabases5个我正在开发一个包含网络应用和移动应用程序的项目,该应用程序记录每日用户的数据.用户可以删除,更新他们的数据,他们可以使用许多设备插入数据.我打算这样开发:用户输入他们的数据然后插入sqlite.服务将定期启动(每5小时或每小时)以使用时间戳与MysqL同步.我确实在互联网上使用服务和时间戳搜索了一个样本,但我一

  5. android – 如何将唯一的GCM注册标识存储到MySQL中

    我正在设置GoogleCloudMessaging机制的服务器端,使用MySQL存储移动应用提供的注册ID.Google可以发出最多4k个注册码,我被迫将其存储在TEXT字段中.所有的好,到目前为止,问题是我必须处理这样的情况:>用户登录到应用程序>该应用从google请求注册ID>应用程序将新的注册ID发送到应用服务器>服务器存储该注册ID并将其链接到当前登录的用户>该用户注销并且新用户登录>应

  6. PHP使用JpGraph绘制折线图操作示例【附源码下载】

    这篇文章主要介绍了PHP使用JpGraph绘制折线图操作,结合实例形式分析了php使用JpGraph的相关操作技巧与注意事项,并附带源码供读者下载参考,需要的朋友可以参考下

  7. jQuery的Cookie封装,与PHP交互的简单实现

    下面小编就为大家带来一篇jQuery的Cookie封装,与PHP交互的简单实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. PHP+jquery+CSS制作头像登录窗(仿QQ登陆)

    本篇文章介绍了PHP结合jQ和CSS制作头像登录窗(仿QQ登陆),实现了类似QQ的登陆界面,很有参考价值,有需要的朋友可以了解一下。

  9. 如何在PHP环境中使用ProtoBuf数据格式

    这篇文章主要介绍了如何在PHP环境中使用ProtoBuf数据格式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

  10. PHP rsa加密解密算法原理解析

    这篇文章主要介绍了PHP rsa加密解密算法原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

随机推荐

  1. crontab发送一个月份的电子邮件

    ubuntu14.04邮件服务器:Postfixroot收到来自crontab的十几封电子邮件.这些邮件包含PHP警告.>我已经解决了这些警告的原因.>我已修复每个cronjobs不发送电子邮件(输出发送到>/dev/null2>&1)>我删除了之前的所有电子邮件/var/mail/root/var/spool/mail/root但我仍然每小时收到十几封电子邮件.这些电子邮件来自cronjobs,

  2. 模拟两个ubuntu服务器计算机之间的慢速连接

    我想模拟以下场景:假设我有4台ubuntu服务器机器A,B,C和D.我想在机器A和机器C之间减少20%的网络带宽,在A和B之间减少10%.使用网络模拟/限制工具来做到这一点?

  3. ubuntu-12.04 – 如何在ubuntu 12.04中卸载从源安装的redis?

    我从源代码在Ubuntu12.04上安装了redis-server.但在某些时候它无法完全安装,最后一次makeinstallcmd失败.然后我刚刚通过apt包安装.现在我很困惑哪个安装正在运行哪个conf文件?实际上我想卸载/删除通过源安装的所有内容,只是想安装一个包.转到源代码树并尝试以下命令:如果这不起作用,您可以列出软件自行安装所需的步骤:

  4. ubuntu – “apt-get source”无法找到包但“apt-get install”和“apt-get cache”可以找到它

    我正在尝试下载软件包的源代码,但是当我运行时它无法找到.但是当我运行apt-cache搜索squid3时,它会找到它.它也适用于apt-getinstallsquid3.我使用的是Ubuntu11.04服务器,这是我的/etc/apt/sources.list我已经多次更新了.我尝试了很多不同的debs,并没有发现任何其他地方的错误.这里的问题是你的二进制包(deb)与你的源包(deb-src)不

  5. ubuntu – 有没有办法检测nginx何时完成正常关闭?

    &&touchrestarted),因为即使Nginx没有完成其关闭,touch命令也会立即执行.有没有好办法呢?这样的事情怎么样?因此,pgrep将查找任何Nginx进程,而while循环将让它坐在那里直到它们全部消失.你可以改变一些有用的东西,比如睡1;/etc/init.d/Nginx停止,以便它会休眠一秒钟,然后尝试使用init.d脚本停止Nginx.你也可以在某处放置一个计数器,这样你就可以在需要太长时间时发出轰击信号.

  6. ubuntu – 如何将所有外发电子邮件从postfix重定向到单个地址进行测试

    我正在为基于Web的应用程序设置测试服务器,该应用程序发送一些电子邮件通知.有时候测试是使用真实的客户数据进行的,因此我需要保证服务器在我们测试时无法向真实客户发送电子邮件.我想要的是配置postfix,以便它接收任何外发电子邮件并将其重定向到一个电子邮件地址,而不是传递到真正的目的地.我正在运行ubuntu服务器9.10.先感谢您设置本地用户以接收所有被困邮件:你需要在main.cf中添加:然后

  7. ubuntu – vagrant无法连接到虚拟框

    当我使用基本的Vagrantfile,只配置了两条线:我看到我的虚拟框打开,但是我的流氓日志多次显示此行直到超时:然后,超时后的一段时间,虚拟框框终于要求我登录,但是太久了!所以我用流氓/流氓记录.然后在我的物理机器上,如果我“流氓ssh”.没有事情发生,直到:怎么了?

  8. ubuntu – Nginx – 转发HTTP AUTH – 用户?

    我和Nginx和Jenkins有些麻烦.我尝试使用Nginx作为Jenkins实例的反向代理,使用HTTP基本身份验证.它到目前为止工作,但我不知道如何传递带有AUTH用户名的标头?}尝试将此指令添加到您的位置块

  9. Debian / Ubuntu – 删除后如何恢复/ var / cache / apt结构?

    我在ubuntu服务器上的空间不足,所以我做了这个命令以节省空间但是现在在尝试使用apt时,我会收到以下错误:等等显然我删除了一些目录结构.有没有办法做apt-getrebuild-var-tree或类似的?

  10. 检查ubuntu上安装的rubygems版本?

    如何查看我的ubuntu盒子上安装的rubygems版本?只是一个想法,列出已安装的软件包和grep为ruby或宝石或其他:)dpkg–get-selections

返回
顶部