在Arch Linux 2015中安装Nginx,Mariadb,PHP(LEMP)堆栈
本教程通过安装和配置Arch Linux 2015上的安装和配置Nginx,MySQL,PHP(LEMP)堆栈。
正如我们所知,LEMP是Linux,Nginx(Engine X),MySQL/Mariadb,PHP/Perl/Pyhton的首字母缩略词。
出于本文的目的,我将使用以下测试机器。
- 操作系统:Arch Linux 2015 64位服务器
- IP地址:192.168.1.102/24
- 主机名:服务器
1.更新ARCH系统
以root用户身份运行以下命令以更新Arch Linux:
pacman -Syu
2.安装nginx.
更新系统后,使用命令安装nginx web服务器:
pacman -S nginx
启动nginx服务并启用它以使用命令启动启动:
systemctl start nginx
systemctl enable nginx
要验证nginx是否已启动,运行:
systemctl status nginx
示例输出:
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2015-02-19 16:20:41 IST; 5s ago Process: 10032 ExecStart=/usr/bin/nginx -g pid /run/nginx.pid; error_log stderr; (code=exited, status=0/SUCCESS) Main PID: 10047 (nginx) Tasks: 2 (limit: 512) CGroup: /system.slice/nginx.service ├─10047 nginx: master process /usr/bin/nginx -g pid /run/nginx.pid... └─10048 nginx: worker process Nov 19 16:20:41 server systemd[1]: Starting A high performance web server a..... Nov 19 16:20:41 server systemd[1]: Started A high performance web server an...r. Hint: Some lines were ellipsized, use -l to show in full.
nginx主要配置文件是/etc/nginx/nginx.conf。
我们可以根据要求编辑和更改它。
测试nginx.
打开Web浏览器并导航到http://localhost或者http://ip-address。
我们将对Nginx Server测试页面感到满意。
3.安装MariaDB.
运行以下命令以安装MariaDB:
pacman -S mysql
正如我们所知道的,MariaDB自2013年以来,MariaDB正在正式官方地执行arch Linux中MySQL的默认实现。
因此,我们将被问到是否安装MariaDB或者Percona服务器,只需点击Enter,然后键入"Y",然后再次按Enter键。
默认选择:例如:MariaDB将安装在Arch Linux上。
resolving dependencies... looking for conflicting packages... Packages (7) boost-libs-1.60.0-2 icu-56.1-2 jemalloc-4.0.4-1 libmariadbclient-10.1.11-1 libxml2-2.9.3-1 mariadb-clients-10.1.11-1 mariadb-10.1.11-1 Total Download Size: 30.68 MiB Total Installed Size: 218.10 MiB :: Proceed with installation? [Y/n] n [Hyman@theitroad ~]# pacman -S mysql :: There are 2 providers available for mysql: :: Repository extra 1) mariadb :: Repository community 2) percona-server Enter a number (default=1): ## Press Enter resolving dependencies... looking for conflicting packages... Packages (7) boost-libs-1.60.0-2 icu-56.1-2 jemalloc-4.0.4-1 libmariadbclient-10.1.11-1 libxml2-2.9.3-1 mariadb-clients-10.1.11-1 mariadb-10.1.11-1 Total Download Size: 30.68 MiB Total Installed Size: 218.10 MiB :: Proceed with installation? [Y/n] y
我们需要在开始服务之前初始化MariaDB数据目录。
为此,运行:
mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
然后运行以下命令以启用和启动MariaDB服务。
systemctl enable mysqld
systemctl start mysqld
我们可以验证MariaDB是否正在运行或者不使用命令:
systemctl status mysqld
示例输出:
● mysqld.service - MariaDB database server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2015-02-19 16:31:21 IST; 5s ago Process: 10285 ExecStartPost=/usr/bin/mysqld-post (code=exited, status=0/SUCCESS) Main PID: 10284 (mysqld) Tasks: 26 (limit: 512) CGroup: /system.slice/mysqld.service └─10284 /usr/bin/mysqld --pid-file=/run/mysqld/mysqld.pid Nov 19 16:31:20 server mysqld[10284]: 2015-02-19 16:31:20 140288888797056 [...a. Nov 19 16:31:20 server mysqld[10284]: 2015-02-19 16:31:20 140288888797056 [...e. Nov 19 16:31:20 server mysqld[10284]: 2015-02-19 16:31:20 140288888797056 [...rt Nov 19 16:31:20 server mysqld[10284]: 2015-02-19 16:31:20 140288888797056 [...19 Nov 19 16:31:20 server mysqld[10284]: 2015-02-19 16:31:20 140288473401088 [...ed Nov 19 16:31:20 server mysqld[10284]: 2015-02-19 16:31:20 140288888797056 [...d. Nov 19 16:31:20 server mysqld[10284]: 2015-02-19 16:31:20 140288888797056 [...'. Nov 19 16:31:20 server mysqld[10284]: 2015-02-19 16:31:20 140288888797056 [...s. Nov 19 16:31:20 server mysqld[10284]: Version: '10.1.11-MariaDB-log' socke...er Nov 19 16:31:21 server systemd[1]: Started MariaDB database server. Hint: Some lines were ellipsized, use -l to show in full.
设置MySQL/MariaDB root用户密码
我们可能知道,建议为数据库root用户设置密码。
运行以下命令以设置MariaDB root用户密码:
mysql_secure_installation
示例输出:
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): ## Press Enter OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n]## Press Enter New password:## Enter password Re-enter new password: ## Re-enter password Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB 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]## Press Enter ... 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]## Press Enter ... Success! By default, MariaDB 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]## Press Enter - 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]## Press Enter ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
MariaDB已安装并准备使用。
4.安装PHP.
要在Arch Linux中安装PHP,请运行:
pacman -S php-fpm
安装PHP后,我们需要告诉Nginx使用PHP-FPM运行PHP。
为此,请编辑/etc/nginx/nginx.conf文件,
nano /etc/nginx/nginx.conf
找到以下部分并添加或者修改它,如下所示。
location ~ \.php${ fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; root /usr/share/nginx/html; include fastcgi.conf; }
保存并关闭文件。
然后,使用下面给出的命令启动和启用nginx服务。
systemctl start php-fpm
systemctl enable php-fpm
测试PHP.
现在在Web根目录中创建一个test.php文件。
nano /usr/share/nginx/html/test.php
添加以下行:
<?php phpinfo(); ?>
最后,重新启动Nginx服务。
systemctl restart nginx
打开Web浏览器并导航到http://ip-address/test.php。
我们应该如下屏幕。
5.安装phpmyadmin
phpmyadmin是一个图形MySQL/Mariadb管理工具,可用于创建,编辑和删除数据库。
要安装它,请运行:
pacman -S phpmyadmin php-mcrypt
安装后,编辑php.ini文件,
nano /etc/php/php.ini
确保以下行取消注释。
[...] extension=bz2.so extension=mcrypt.so extension=mysqli.so extension=pdo_mysql.so [...]
保存并关闭文件。
然后编辑nginx配置文件,
nano /etc/nginx/nginx.conf
找到以下块:
location/{ root /usr/share/nginx/html; index index.html index.htm;
将其更改为(仅添加一个单词index.php)
location/{ root /usr/share/nginx/html; index index.html index.htm index.php;
保存并关闭文件。
然后,使用命令创建用于Nginx默认根路径的PHPMyaAdmin系统路径的符号链接:
ln -s /usr/share/webapps/phpMyAdmin//usr/share/nginx/html/
最后重新启动nginx和php-fpm服务:
systemctl restart nginx
systemctl restart php-fpm
测试phpmyadmin
打开浏览器并导航到http://ip-address/phpmyadmin。
请注意,字母M和A应该是URL中的大写字母。
我们应该看到以下类似屏幕。
输入mysql/mariadb根用户名及其密码。
其中我的phpmyadmin web控制台看起来像。
我们可能会看到一个错误,说明"配置文件现在需要PHPMyAdmin仪表板底部的秘密密码(Blowfish_secret)"。
要摆脱此错误,请编辑/etc/webapps/phpmyadmin/config.inc.php文件,
nano /etc/webapps/phpmyadmin/config.inc.php
找到以下行并指定Bluefish密码密码短语:
$cfg['blowfish_secret'] = 'Welc0me1'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH!$
/**
这里welc0me1是我的秘密密码。
保存并关闭文件。
重新启动nginx服务。
systemctl restart nginx
错误现在将消失。