如何安装和设置Apache 2

如何安装Apache2,php,mysql并使用虚拟主机进行设置,最好是让我们加密SSL证书,然后继续在其上安装Wordpress?

我假设你有一个正在运行的Ubuntu安装。 这是用16.04编写的,但也适用于其他版本的一些调整。

在这个答案中, #表示根shell,而$表示普通用户shell。

在本回答中,example.org用作示例(D’oh),应根据您的安装进行更改。

安装Apache2,PHP,MariaDB等

 $ sudo apt install apache2 libapache2-mod-php mariadb-server php-mysql 

除了用于访问mysql的PHP绑定之外,这将安装Apache2,PHP,MariaDB和一些依赖项。

在此阶段,您应该能够访问http://example.org ,并查看默认页面: 默认页面

设置vhost

Vhost是虚拟主机,用于为不同的域名提供不同的内容。

在您喜欢的编辑器中开始编辑名为/etc/apache2/sites-available/01-example.org.conf的新文件:

 $ sudo editor /etc/apache2/sites-available/01-example.org.conf 

输入以下配置:

  ServerName example.org ServerAlias www.example.org ServerAdmin webmaster@example.org DocumentRoot /var/www/html/example.org/ ErrorLog ${APACHE_LOG_DIR}/example.org.error.log CustomLog ${APACHE_LOG_DIR}/example.org.access.log combined  

首先,我们定义主ServerName。 这是用于访问该站点的域。 每个vhost只能定义一个。 此外,我们定义了ServerAlias,以防有人在浏览器中输入www.example.org。 这可确保Apache回复这两个名称。 两个名称都必须指向您的服务器,在DNS或/etc/hosts进行本地测试。

可以指定任意数量的服务器别名,并且它们不必包含ServerName的一部分。 因此, ServerAlias example.com将是有效的。

创建新的DocumentRoot

我已将新文档放在/var/www/html/example.org 。 这是一个允许Apache在Ubuntu中提供服务的位置。 例如,如果我将它放在/srv/ ,我将不得不为它包含一个Directory节。 现在,创建webroot,填充一些内容,并激活新配置:

 $ sudo mkdir /var/www/html/example.org $ echo "This is a test" | sudo tee /var/www/html/example.org/index.html $ sudo a2ensite 01-example.org.conf $ sudo service apache2 reload 

如果你现在访问http://example.org ,你应该看到输出*这是一个测试“。恭喜!你的第一个vhost正在运行!

安装letsencrypt并获取证书

要从Let’s Encrypt接收证书,我们需要一个客户端。 16.04中包含的letsencrypt包很古老,所以我们需要一个ppa。

 $ echo "deb http://ppa.launchpad.net/certbot/certbot/ubuntu xenial main" | sudo tee /etc/apt/sources.list.d/01-certbot.list $ sudo add-apt-key -k keyserver.ubuntu.com 8C47BE8E75BCA694 $ sudo apt update && sudo apt install certbot python3-certbot-apache 

以root身份运行certbot:

 $ sudo certbot 

选择要获取证书的域,然后按照certbot的屏幕说明进行操作。 当系统询问您是否要重定向时,如果您只想要https,请选择重定向;如果您想要http和https,则不选择重定向。 今天,几乎没有理由不重定向。

 Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. ------------------------------------------------------------------------------- 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. ------------------------------------------------------------------------------- Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 

尝试再次访问http://example.com – 它应该更改以显示它是安全的。

恭喜,您尚未使用有效的TLS证书设置apache2,以确保流量已加密!

安装WordPress

下一步是安装一些应用程序。 我选择了WordPress作为安装示例。

首先通过输入sudo -i成为root。 接下来,将目录更改为您的webroot,然后下载,解压缩并将所有权更改为Apache的用户:

 $ sudo -i # cd /var/www/html/example.org/ # wget https://wordpress.org/latest.tar.gz # tar -zxf latest.tar.gz && rm latest.tar.gz # chown -R www-data.www-data wordpress/ 

您现在将在https://example.com/wordpress/上有一个WordPress实例 – 让我们去那里。

该向导告诉您需要MySQL表,用户和密码。 让我们来吧!

默认情况下,Ubuntu将对MariaDB使用unix套接字身份validation。 因此,要以root用户身份登录MariaDB,必须使用

 sudo mysql -u root 

或者在root shell中运行该命令(例如sudo -i )。 这将在不输入任何密码的情况下登录

 # mysql -u root Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 32 Server version: 10.0.33-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04 Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> CREATE DATABASE wordpress_db; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> GRANT ALL PRIVILEGES ON `wordpress_db`.* TO 'wordpress_user'@'localhost' IDENTIFIED BY 'passw0rd'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> exit 

在WordPress的配置中填写用户名wordpress_user ,databasename wordpress_db和密码passw0rd 。 基本上就是这样; 其余的是关于遵循WordPress的安装指南 。

要添加更多虚拟主机,只需从“设置虚拟主机”开始。

进一步阅读

  • Apache模块 – 禁用和启用
  • 从cron运行certbot到autorenew certs
  • mod_rewrite指南,一个常用的Apache模块
  • /var/www/html文件权限
  • Certbot用户指南