AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1007425
Accepted
vidarlo
vidarlo
Asked: 2018-02-19 08:35:31 +0800 CST2018-02-19 08:35:31 +0800 CST 2018-02-19 08:35:31 +0800 CST

如何安装和设置 Apache 2

  • 772

如何安装 Apache2、php、mysql 并使用虚拟主机进行设置,最好使用 Let's Encrypt SSL 证书,然后继续在其上安装 Wordpress?

server apache2 webserver 16.04 letsencrypt
  • 2 2 个回答
  • 1609 Views

2 个回答

  • Voted
  1. Best Answer
    vidarlo
    2018-02-19T08:35:31+08:002018-02-19T08:35:31+08:00

    我假设你有一个正在运行的 Ubuntu 安装。这是在考虑 16.04 的情况下编写的,但也应该适用于其他版本的一些改编。

    在此答案中,#表示根 shell,同时$表示普通用户 shell。

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

    安装 Apache2、PHP、MariaDB 等

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

    这将安装 Apache2、PHP、MariaDB 和一些依赖项,以及用于访问 mysql 的 PHP 绑定。

    在这个阶段,您应该能够访问http://example.org,并看到一个默认页面: 默认页面

    设置虚拟主机

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

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

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

    输入以下配置:

    <VirtualHost *:80>
            ServerName example.org
            ServerAlias www.example.org
            ServerAdmin [email protected]
            DocumentRoot /var/www/html/example.org/
            ErrorLog ${APACHE_LOG_DIR}/example.org.error.log
            CustomLog ${APACHE_LOG_DIR}/example.org.access.log combined
    </VirtualHost>
    

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

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

    创建新的 DocumentRoot

    我已将新的 documentroot 放在/var/www/html/example.org. 这是 Ubuntu 中允许由 Apache 提供服务的位置。例如,如果我将它放在 中/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,您应该会看到输出 *This is a test"。恭喜!您的第一个虚拟主机正在运行!

    安装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 作为安装示例。

    首先通过输入成为root sudo -i。接下来,将目录更改为您的 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 套接字身份验证。因此,要以 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、数据库名wordpress_db和密码。passw0rd基本上就是这样;剩下的就是遵循 WordPress 的安装指南。

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

    进一步阅读

    • Apache 模块 - 禁用和启用
    • 从 cron 运行 certbot 以自动更新证书
    • mod_rewrite 指南,一个常用的 Apache 模块
    • 文件权限/var/www/html
    • 证书机器人用户指南
    • 3
  2. Ankit Kumar Rajpoot
    2019-11-22T00:17:20+08:002019-11-22T00:17:20+08:00

    在 Ubuntu 中安装 Apache

    sudo apt-get update
    sudo apt-get install apache2
    sudo ufw app list
    sudo ufw allow 'Apache Full'
    sudo ufw status // Status will be inactive.
    sudo systemctl status apache2 // Apache server will be Active.
    

    检查 Apache 服务器 您可以通过您的 IP 检查您的 Apache 服务器是否正常工作。在您的浏览器中输入

    http://server_domain_or_IP

    您将找到默认页面。

    管理 Apache 进程

    sudo systemctl stop apache2 // Stop Apache Server
    sudo systemctl start apache2 // Start Apache Server
    sudo systemctl restart apache2 // Restart Apache Server
    sudo systemctl reload apache2 // Reload Apache Server
    sudo systemctl disable apache2 // Disable Auto Start Server
    sudo systemctl enable apache2 // Enable Auto Start Server
    
    • -1

相关问题

  • 使用 dpkg 手动安装软件包是否会阻止未来的升级路径?

  • 如何从命令行刻录双层 dvd iso

  • 如果在服务器机器上运行 Ubuntu 桌面版,性能损失是多少?

  • 将桌面版剥离为服务器版的最简单方法是什么?

  • 如何与无头服务器进行图形交互?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve