如何在Ubuntu上安装LAMP
在本教程中,我将说明如何在最新的Ubuntu 18.04服务器上使用WordPress安装LAMP堆栈。
LAMP代表Linux-Apache-MySQL-PHP。
它是一组免费软件,可以将其安装在一起以构建服务器来托管动态的数据库驱动的和Web应用程序。
它使用Linux作为操作系统,使用Apache作为Web服务器,使用MySQL作为关系数据库管理系统来存储站点数据,并使用PHP处理动态内容。
还有一种使用Ubuntu/Debian开发的“ tasksel”工具安装LAMP服务器的简便方法。
但是在本教程中,我将手动安装每个软件包。
安装Apache
服务器存储库本身中包含所有最新版本的Apache,Mysql和PHP。
因此,我们可以使用易于安装的软件包管理器来安装所有软件包,而无需任何第三方存储库。
我们可以通过在下面运行以下命令来安装Apache:
#apt-get update #apt install apache2
我们需要确保使用systemctl命令启用并启动此服务:
#systemctl enable apache2 #systemctl start apache2
我们已经在服务器中安装了最新的Apache 2.4.29版本。
接下来,我们可以通过访问浏览器中的服务器IP来验证已安装的Apache版本并测试其是否正常运行,就像“ >> http://Server-IP /”
优化Web服务器性能
请按照以下步骤来优化Web服务器性能。
1.优化Apache指令
用最少的可用资源确保服务器性能始终很重要。
KeepAlive是一个apache指令,它允许Apache使用服务器端内存,从而减少了托管站点上用户的延迟。
它提供了长期存在的HTTP会话,该会话允许通过同一TCP连接发送多个请求。
如果主机有足够的内存来支持,则KeepAlive将使速度更快。
补充此指令的是MaxKeepAliveRequests和KeepAliveTimeout。
MaxKeepAliveRequests设置控制持久连接期间的最大请求数。
50是保守的数量;我们可以根据我们的网络流量设置更高的数字。
KeepAliveTimeout控制服务器等待来自已连接客户端的新请求的时间,将此选项设置为5将避免浪费内存。
其值越高,在高负载服务器中导致性能问题的机会就越大。
超时时间越高,等待与空闲客户端进行连接的服务器进程将被占用的时间越多。
考虑到这些指令,我使用这些值优化了位于/etc/apache2/apache.conf的默认Apache配置文件,以提高Web服务器性能(我们需要根据需要进行调整)。
KeepAlive On MaxKeepAliveRequests 50 KeepAliveTimeout 5
2.选择MPM模块
Apache 2引入了多处理模块或者MPM。
建议使用Prefork,Worker和Event这三个MPM。
默认情况下,Apache选择预叉MPM。
由于LAMP堆栈需要PHP,因此最好坚持使用默认值。
我们可以通过在命令行上运行以下命令来确定当前的MPM状态。
注意:我们需要根据服务器性能和Web服务器请求调整这些值
# apache2 -V |grep -i 'version\|mpm' Server version: Apache/2.4.29 (Ubuntu) Server MPM: prefork
打开/etc/apache2/mods-available中的mpm_prefork.conf文件,然后编辑配置。
# cat /etc/apache2/mods-available/mpm_prefork.conf # prefork MPM # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxRequestWorkers: maximum number of server processes allowed to start # MaxConnectionsPerChild: maximum number of requests a server process serves <IfModule mpm_prefork_module> StartServers 4 MinSpareServers 3 MaxSpareServers 40 MaxRequestWorkers 200 MaxConnectionsPerChild 10000 </IfModule>
PS:我们甚至可以根据托管要求使用这些命令在MPM之间进行切换。
#a2dismod mpm_event #a2enmod mpm_prefork
3.重新启动Apache服务
始终建议在对其配置进行任何更改后重新启动Apache服务,以使更改生效。
因此,在进行了上述更改之后,请确保重新启动Apache服务。
#systemctl restart apache2
安装MySQL
其次,我们需要安装数据库服务器。
我已经在服务器上安装了MySQL 5.7.22.
我们可以通过运行以下命令来安装可用的最新版本:
#apt install mysql-server # mysql -V mysql Ver 14.14 Distrib 5.7.22, for Linux (x86_64) using EditLine wrapper
启动/启用MySQL服务并检查其状态。
# systemctl enable mysql Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install. Executing: /lib/systemd/systemd-sysv-install enable mysql # systemctl start mysql # systemctl status mysql
接下来,我们将需要运行安装随附的安全脚本来保护数据库服务器的安全。
我们可以运行以下命令来保护MySQL数据库并设置root密码:
# mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank 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: Y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 Please set the password for root here. New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y 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, any other key for No) : 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, any other key for No) : 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? (Press y|Y for Yes, any other key for No) :y 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, any other key for No) :y Success All done!
如我们所见,在此安全安装阶段,它将提示我们设置“ PASSWORD PLUGIN”。
出于安全原因,我绝对会建议我们选择强密码级别。
完成后,我们可以设置管理密码。
对于其余的问题,我们可以为“是”输入“ y”,这将删除匿名用户和测试数据库,禁用远程root登录,并使用这些新规则立即重新加载特权表。
安装PHP
PHP是我们的脚本语言,用于处理代码以显示动态内容。
基本上,它运行域脚本,连接到它们各自的数据库,然后将内容提供给Web服务器进行显示。
要安装最新的PHP及其一些重要模块,请在终端上使用以下命令:
#apt install php libapache2-mod-php php-mysql # php -v PHP 7.2.3-1ubuntu1 (cli) (built: Mar 14 2016 22:03:58) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.2.3-1ubuntu1, Copyright (c) 1999-2016, by Zend Technologies
# systemctl restart apache2
我已经在服务器上安装了PHP 7.2.3.
我们可以使用上述命令从命令行确认其版本。
还有许多其他PHP模块可用来增强PHP的功能。
要查看可用的模块PHP模块列表和库,请将'apt search into less'的结果传递到管道中,这是一个寻呼机,可让我们滚动查看命令的输出:
#apt search php- | less
我们可以使用箭头键上下滚动,然后按q退出。
要显示有关每个模块的更多信息,可以使用命令“ apt show package_name”。
例如,要了解有关“ PHP-bz2”模块的使用的更多信息,可以输入以下内容:
# apt show php7.2-bz2 Package: php7.2-bz2 Version: 7.2.3-1ubuntu1 Priority: optional Section: universe/php Source: php7.2 Origin: Ubuntu Maintainer: Ubuntu Developers <[email protected]> Original-Maintainer: Debian PHP Maintainers <[email protected]> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 52.2 kB Provides: php-bz2 Depends: php-common (>= 1:35), ucf, php7.2-common (= 7.2.3-1ubuntu1), libbz2-1.0, libc6 (>= 2.14) Homepage: http://www.php.net/ Download-Size: 10.2 kB APT-Sources: http://mirrors.linode.com/ubuntu bionic/universe amd64 Packages Description: bzip2 module for PHP This package provides the bzip2 module(s) for PHP. . PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
描述部分简要说明了此模块的用途。
根据要求,我们可以选择所需的模块软件包并使用命令“ apt install”进行安装。
我们需要重新启动Apache才能使这些更改生效。
现在,我们可以在域文档根目录下创建一个PHP信息页面,以确认其版本和模块列表。
# cd /var/www/html #echo "<?php phpinfo(); ?>" >> info.php
我们可以浏览URL'http://ServerIP/info.php'以确认其正常工作。
我们已经成功地在Ubuntu 18上完成了LAMP堆栈的安装。
现在,是时候建立我们的WordPress教程 了。
让我们按照以下步骤安装WordPress:
安装WordPress
我们可以从其官方下载最新的WordPress下载文件,并将其解压缩到文档根目录“/var/www/html”进行安装。
# apt install wget # wget http://wordpress.org/latest.tar.gz # tar -xvf latest.tar.gz -C /var/www/html/
为WordPress创建MySQL数据库
接下来,我们需要为WordPress安装创建数据库和用户,为此,请运行以下命令:
# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.22-0ubuntu18.04.1 (Ubuntu) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names Jan be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database wpdatabase; Query OK, 1 row affected (0.00 sec) mysql> CREATE USER 'wpuser'@'localhost' IDENTIFIED BY '9RYiZ1fca$#RBrvaFpxwL&TYY'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL ON wpdatabase.* TO 'wpuser'@'localhost'; Query OK, 0 rows affected (0.00 sec)
我们已经连接到MySQL Shell,并为用户WordPress安装创建了一个名为“ wpdatabase”的数据库,该数据库的用户为“ wpuser”。
接下来,我们需要从模板“ wp-config-sample.php”文件创建“ wp-config.php”,并使用我们创建的配置文件修改配置文件中的MySQL设置,以开始安装。
#cd /var/www/html/wordpress/ #cp wp-config-sample.php wp-config.php
我已经使用创建的数据库详细信息修改了文件设置。
//** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define('DB_NAME', 'wpdatabase'); /** MySQL database username */ define('DB_USER', 'wpuser'); /** MySQL database password */ define('DB_PASSWORD', '9RYiZ1fca$#RBrvaFpxwL&TYY'); /** MySQL hostname */ define('DB_HOST', 'localhost');
配置虚拟主机
我们可以如下所示为WordPress设置虚拟主机。
默认情况下,Apache监听所有可用的IP地址。
对于以下所有步骤,请用域名替换example.com。
我将其替换为我的域名mywordpressblog.com。
1.为站点创建默认的Apache配置文件的副本:
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mywordpressblog.com.conf
2.编辑新的mywordpressblog.com.conf配置文件,方法是:取消ServerName注释,然后将example.com替换为我们站点的IP或者完全合格域名(FQDN)。
就我而言,我已替换为www.mywordpressblog.com。
输入文档根目录路径和日志目录,如下所示,并在</VirtualHost>之前添加一个Directory块,如下所示:
# cat mywordpressblog.com.conf <Directory /var/www/html/wordpress> Require all granted </Directory> <VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. ServerName www.mywordpressblog.com ServerAdmin webmaster@localhost DocumentRoot /var/www/html/wordpress # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog /var/www/html/mywordpressblog.com/error.log CustomLog /var/www/html/mywordpressblog.com/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost>
3.创建配置文件中引用的目录:
# mkdir -p /var/www/html/mywordpressblog.com/ # touch /var/www/html/mywordpressblog.com/access.log # touch /var/www/html/mywordpressblog.com/error.log
4.将虚拟主机文件从sites-available目录链接到sites-enabled目录,并禁用默认虚拟主机,以最大程度地降低安全风险:
# a2ensite mywordpressblog.com.conf # a2dissite 000-default.conf
5.进行这些更改后,重新加载Apache。
# systemctl reload apache2
现在,我们可以浏览以下URL'>> http://mywordpressblog.com/'以完成我们的安装。
我们可以设置管理员凭据和标题以完成安装阶段。
最后,我们可以使用创建的凭据浏览URL'http://mywordpressblog.com/wp-login.php'来访问管理区域。