阿里云下Ubuntu16下LAMP+wordpress建站记录

因为需要,买了一个云服务器建了一个网站,当然这个不是给我用的,我还会继续用CSDN(虽然好久没更新了)。

建站记录:买云服务器,我买的是阿里云的云服务器ECS,貌似什么云虚拟主机好像也可以,不怎么了解那个,然后就是买域名,审核,然后备案,不得不说备案居然还要居住证或者暂住证,这么严格也是醉了。

然后说下心路历程吧。(想看教程的就跳过吧)

开始时,用什么系统好呢,肯定是linux啦,好像CentOS比较稳定,很适合当服务器,那就它吧!然后百度“CentOS7 wordpress”就开始了,安装好apache2后,打开浏览器访问localhost?喵喵喵?浏览器在哪?安装了一个firefox,怎么打开呢??啊哈,要安装一个图形界面啊,然后百度“CentO S7 图形界面”,%&*#@$,什么鬼啊,好麻烦,看不懂,,最后放弃。

那还是用ubuntu吧,毕竟还熟悉点,装了ubuntu sever,继续百度“ubuntu wordpress”,啊哈,浏览器访问localhost,好嘛,这个装图形界面简单哈,apt-get install ubuntu-desktop,就可以,#$%^&*,图形界面装好了。继续百度,啊,github上有个lamp的包,可以直接安装啊,就它了,,试了好多遍,下载速度好慢,,总是下载失败,,算了,,换一个。又搜啊搜,哎,这里有篇文章nginx+HHVM+fastCGI+mysql+wordpress,哇,看起来很叼,很高端,就它了,nginx..搞定,HHVM,FanQiang去官网,啊,,这个东西被Qiang了,,安装不了,,算了,还是用LAMP+ubuntu吧。然后自己配置lamp,啊,,要装php5.0啊,,ubuntu16上怎么没有,,原来ubuntu16已经默认用的是php7.0 了,,要添加一个ppa源啊,,搞好了,,啊,,这些什么libapache2-mod-php之类的怎么搞,,好麻烦啊,,这个服务器怎么还这么卡啊,经常卡死,,然后又去网上搜,,某乎上的一个问题“请问为什么阿里云ubuntu系统没有图形界面?”吸引了我,,进去看了看,为什么dalao们都说不需要图形界面啊,有命令行就可以,,Emm...,干tmd棒棒锤,,是不需要图形界面啊,我有公网ip,,通过公网ip访问不就可以了吗,,想起自己电脑上的wordpress,配置好了之后只通过网页就可以管理了。。(终于在这个时候走出了GUI的误区)然后又是php5.0的问题,百度上的都是基于ubuntu14或者12的,都很老,,万般无奈之下,打开google,“ubuntu16 wordpress”,窝日,,这么多教程。。原来用php7.0就可以,去搜了下wordpress用php7.0可不可以,原来完全可以,而且性能还很好,不亚于什么HHVM+fastCGI。然后后来看了n多篇教程,,大概学会了怎么安装以及一些细节问题,个人又试了两次wordpress中文版和英文版,都可以用,没什么问题。


正题,安装记录(都是在root用户下操作的):

先更新下软件

apt-get update
apt-get upgrade

安装apache2
apt-get install apache2
apt-get install libapache2-mod-php

一种重启apache2和查看状态的命令
service apache2 restart
service apache2 status

另一种启动apache2、重启和查看状态的命令(enable好像会报错,不过没影响)
有一种说法是ubuntu14.04一下版本的使用上面的命令,更高的使用下面的命令,总之16.04两种都可以用,个人感觉systemctl比较好
systemctl enable apache2
systemctl start apache2
systemctl restart apache2
systemctl status apache2
重启后,从自己电脑访问服务器公网ip,如果有ubuntu apache2,就说明安装成功了。


安装mysql
apt-get install mysql-server
apt-get install php7.0-mysql

安装验证密码插件(非必需,建议选)

mysql_secure_installation
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就安装,别的就是不安装,按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: 1


设置密码安全级别,看个人兴趣吧,感觉0或1就可以。

Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

表示当前密码强度是100,是否修改,y修改,n不修改。

然后后面的都按y就可以。

查看mysql状态和启动mysql

systemctl status mysql
systemctl start mysql


安装php7(我这里装了好多,我也不知道哪些是非必需的)

apt-get install php7.0 php7.0-curl php7.0-gd php7.0-json php7.0-opcache php7.0-xml mcrypt php7.0-curl php7.0-cgi php7.0-xmlrpc php7.0-gd php-pear


重启apache2和mysql

systemctl restart apache2
systemctl restart mysql


测试php和apache2

vim /var/www/html/info.php

输入

<?php
phpinfo();
?>

保存后访问你的域名或者公网ip:your_server_ip/info.php,看看有没有对应的信息,有就说明成功了。


至此,LAMP的必须操作就完成了,下面是一些可选操作。


将“全局服务器名称”设置为“禁止语法警告”(我也不知道有什么用)

如果你在命令行输入

apache2ctl configtest

将会看到

Output
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Syntax OK

下一步,我们将一个行添加到/etc/apache2/apache2.conf的文件来抑制一个警告消息。 而无害的,如果你不设置ServerName全球范围内,你会检查语法错误Apache配置时收到以下警告

vim /etc/apache2/apache2.conf

在最后一行加上

ServerName server_domain_or_IP

然后我们再输入

apache2ctl configtest

就会看到

Output
Syntax OK

这样就说明可以了。


更改apache2对html文件和php文件的偏好,就是/var/www/html文件夹下同时有index.html和index.php的话,它会指定哪个为主页,默认的是html文件,现在要改为是php文件。

(我是直接把初始的index.html文件扔到了其他没用的地方,没进行这一步操作)

在大多数情况下,我们希望修改Apache在请求目录时提供文件的方式。 目前,如果用户从服务器请求一个目录,Apache将首先寻找一个名为index.html 。 我们要告诉我们的Web服务器更喜欢PHP文件,所以我们让Apache寻找一个index.php文件中的第一。
要做到这一点,键入此命令打开dir.conf与root权限的文本编辑器文件中:

vim /etc/apache2/mods-enabled/dir.conf

它将如下所示:

<IfModule mod_dir.c>
    DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

我们要移动的PHP索引文件上文所强调的之后的第一个位置DirectoryIndex规范,就像这样:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

然后保存,就完成了。

systemctl restart apache2

重启服务器就生效了。


设置mpm多处理模块

(并不知道有什么作用,反正我建的只是一个小站,就没有设置这个)
The default multi-processing module (MPM) for Apache is the event module but by default PHP uses the prefork module. Open the mpm_prefork.conf file located in /etc/apache2/mods-available and edit the configuration. Below are the suggested values for a 2GB Linode:

vim /etc/apache2/mods-available/mpm_prefork.conf
<IfModule mpm_prefork_module>
        StartServers            4
        MinSpareServers         20
        MaxSpareServers         40
        MaxRequestWorkers       200
        MaxConnectionsPerChild  4500
</IfModule>

Disable the event module and enable prefork:

a2dismod mpm_event
a2enmod mpm_prefork


Restart Apache:

systemctl restart apache2

设置虚拟主机
貌似这个的作用就是可以使一个服务器运行多个网站,想了想,我的服务器暂时不需要,就没搞
You can set up virtual hosts several ways; however, below is the recommended method. By default, Apache listens on all IP addresses available to it. For all steps below, replace example.com with your domain name.
Create a copy of the default Apache configuration file for your site:
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

Edit the new example.com.conf configuration file by uncommenting ServerName and replacing example.com with your site’s IP or Fully Qualified Domain Name (FQDN). Enter the document root path and log directories as shown below, and add a Directory block before </VirtualHost>:
vim /etc/apache2/sites-available/example.com.conf

<Directory /var/www/html/example.com/public_html>
    Require all granted
</Directory>
<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/example.com/public_html

        ErrorLog /var/www/html/example.com/logs/error.log
        CustomLog /var/www/html/example.com/logs/access.log combined

</VirtualHost>

Note:
The file example above has all comment sections removed for brevity; you may keep or remove the commented areas as you see fit.
The ServerAlias directive allows you to include multiple domain names or subdomains for a single host. The example above allows visitors to use example.com or www.example.com to navigate to this virtual host.

Create the directories referenced above:
mkdir -p /var/www/html/example.com/{public_html,logs}
Link your virtual host file from the sites-available directory to the sites-enabled directory:
a2ensite example.com.conf

Note:
If you need to disable your website, run:
a2dissite example.com.conf
Disable the default virtual host to minimize security risks:
a2dissite 000-default.conf

Reload Apache:

systemctl reload apache2

Virtual hosting should now be enabled. To allow the virtual host to use your domain name, be sure that you have configured DNS services for your domain to point to your Linode’s IP address.

If there are additional websites you wish to host on your Linode, repeat the above steps to add a folder and configuration file for each.

至此,LAMP的可选操作也介绍完了。


WordPress的安装与配置

下载wordpress:

wget https://wordpress.org/latest.zip

安装unzip:

apt-get install unzip

解压和后续操作

unzip -q latest.zip -d /var/www/html/
cd /var/www/html/wordpress
cp -a * ..
rm -r wordpress/
chown www-data:www-data -R /var/www/html/

创建上传目录:

mkdir -p /var/www/html/wp-content/uploads
chown www-data:www-data -R /var/www/html/wp-content/uploads

mysql相关操作
创建mysql账户:
mysql -u root -p
CREATE DATABASE wordpress character set utf8 collate utf8_bin;
GRANT ALL PRIVILEGES on wordpress.* to 'wpuser'@'localhost' identified by 'your_password';
FLUSH PRIVILEGES;
exit

编辑配置文件
cd /var/www/html
mv wp-config-sample.php wp-config.php
vim wp-config.php
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');
 
/** MySQL database username */
define('DB_USER', 'wpuser');
 
/** MySQL database password */
define('DB_PASSWORD', 'your_password');
 
/** MySQL hostname */
define('DB_HOST', 'localhost');


设置固定链接:

如果是设置了虚拟主机的话,这部分可能和下面的操作不一样,我也不太清楚,可以去google搜一下

vim /etc/apache2/sites-available/000-default.conf
[...]
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ServerName server1.example.com
<Directory /var/www/html/>
AllowOverride All
</Directory>
[...]

就是在里面添加上ServerName到</Directory>这一段

允许URL的重写以及Apache2的重启

a2enmod rewrite
service apache2 restart

创建.htaccess文件

touch /var/www/html/.htaccess
chown :www-data /var/www/html/.htaccess
chmod 664 /var/www/html/.htaccess


添加wordpress安全保护(其实我也不太懂具体作用)
访问 https://api.wordpress.org/secret-key/1.1/salt/

然后打开wp-config.php

vim /var/www/html/wp-config.php
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

将其对应填入。


至此,wordpress就配置好了,通过ip地址访问网站,完成wordpress的注册即可。

另外,在设置->固定链接中,设置固定链接,最好设置为文章名(Post name),这样貌似也方便别人SEO。


目前能做的就是这些了,感觉基本完善了吧,如果后面有改进的会继续写进来,也欢迎大家讨论和提供建议。


另外,本文中的内容并非原创,很多来自网上的别人博客。

参考链接:

Ubuntu|烂笔头

如何使用让我们在Ubuntu 16.04加密安全Apache-Howtoing运维教程

如何在Ubuntu 16.04安装Linux,Apache,MySQL和PHP(LAMP)-Howtoing运维教程

How To Install WordPress with LAMP on Ubuntu 16.04 | DigitalOcean

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04 | DigitalOcean

Install Wordpress 4.6 in Ubuntu 16.04 with LAMP stack [Updated] - OSTechNix

Install Apache, MariaDB, PHP (LAMP stack) in Ubuntu 16.04 - OSTechNix

Install WordPress on Ubuntu 16.04

Install LAMP on Ubuntu 16.04

Install WordPress on Ubuntu 16.04 – Dizwell Informatics

How to install Wordpress 4.5 on Ubuntu 16.04 LAMP

Ubuntu 16.04 LAMP server tutorial with Apache 2.4, PHP 7 and MariaDB (instead of MySQL)

Install Wordpress Ubuntu 16.04 | Atlantic.Net Community

Install Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04 | Atlantic.Net Community

Configure Wordpress with Ubuntu 16.04 | Unixmen

How To Install LAMP Stack On Ubuntu 16.04 | Unixmen

How To Install WordPress on Ubuntu 16.04 - idroot

How To Install LAMP Stack on Ubuntu 16.04 - idroot

在ubuntu16.04 安裝wordpress | 傑瑞窩在這


猜你喜欢

转载自blog.csdn.net/code12hour/article/details/63691403