Ubuntu Server 16.04 安裝+安全Web服務器(linux+apache+mysql+php)搭建流程(更新至Ubuntu Server 16.04 LTS + PHP7)

之前整過CentOS,整了Ubuntu才發現,Ubuntu簡單多了~~不知道性能相比又如何。

以Ubtuntu 14.04爲例,記錄一下搭建流程。

安裝注意點

1. IP地址

安裝時先不要插網線,不然會DHCP,後續配置起來也麻煩

2. 硬盤分區

自動分區貌似有:/、swap、efiswap、/boot(200MB)
實際兩個區就足夠了:swap爲內存的1~2倍大小,剩餘皆爲/

IP配置

裝完後一般遠程連接,需要配置ip地址,以下是多ip配置。(安裝時會以嚮導形式配好一個IP,修改時只要複製粘貼就可以)

vi /etc/network/interfaces

配置信息:

 # The primary network interface
 auto em0
 iface em0 inet static
         address 192.168.1.1
         netmask 255.255.252.0
         network 192.168.1.0
         broadcast 192.168.1.255
         gateway 192.168.8.254
         # dns-* options are implemented by the resolvconf package, if installed
         dns-nameservers 8.8.8.8 8.8.8.9
         # dns-search 163.com

 # The second network interface
 auto em1
 iface em1 inet static
         address 192.168.1.2
         netmask 255.255.252.0
         network 192.168.1.0

重啓網卡(貌似不一定生效,reboot肯定可以)

/etc/init.d/networking restart

SSH服務器

apt-get install ssh

默認安裝後vi /etc/ssh/sshd_config,修改配置文件。
安全起見,可以建立允許列表:

1. 修改/etc/hosts.allow:

sshd: 192.168.1. , 192.168.0. : allow

2. 修改/etc/hosts.deny:

sshd : ALL

系統更新

先更新源,用以Trusty Tahr (14.04)爲例,其他版本看配置生成器

deb http://mirrors.ustc.edu.cn/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.ustc.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse

再更新apt-get

apt-get update
sudo apt-get upgrade

然後更新系統並重啓

sudo apt-get install update

補充:ssh終端亂碼問題

ubuntu默認是中文編碼,然後又沒法正常中文,因此putty連接時會亂碼,只要設定即可 /etc/default/locale

LANG=”zh_CN.UTF-8
LANGUAGE=”zh_CN:zh:en_US:en”

如果不行:
1. 查看 /var/lib/locales/supported.d/local 中設定的字符集:

en_US.UTF-8 UTF-8
zh_CN.UTF-8 UTF-8
zh_CN.GBK GBK
zh_CN GB2312

保存後,執行命令:sudo locale-gen

  1. 打開 /etc/environment,添加:
LANG=”zh_CN.UTF-8
LANGUAGE=”zh_CN:zh:en_US:en”

reboot 即可

Samba安裝網上鄰居

  1. 默認安裝 apt-get install samba
  2. 爲samba創建ubuntu系統已經存在的用戶somebody:
smbpasswd -a somebody

然後會提示你輸入密碼,這是訪問網上鄰居文件夾的密碼。
3. 修改配置文件:

cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
vi /etc/samba/smb.conf

在其後增加共享目錄:

[share]
    path = /home/somebody/samba_share
      available = yes 
      browseable = yes 
      public = no 
      writable = yes
[home]
   comment = Users' Home
   path = /home
   admin users = somebody
   writable = yes
   hosts deny = ALL
   hosts allow = 192.168.1.7
[www]
   path = /var/www
   admin users = somebody
   writable = yes
   hosts deny = ALL
   hosts allow = 192.168.1.7
[logs]
   path = /var/log
   read only = yes
  1. 重啓samba就可以像windows一樣訪問
/etc/init.d/samba restart 

安裝apache2

apt-get install apache2  

裝好後訪問http://localhost就可以看到“It Works!”
apache2配置文件在/etc/apache2/apache2.conf,web目錄在/var/www/html

默認用戶是www-data,定義在./envvars文件中

其他配置文件在:./sites-enabled/*.conf

啓用rewrite

sudo a2enmod rewrite

並修改AllowOverride None 爲AllowOverride All

安裝mysql

apt-get install mysql-server mysql-client

安裝過程需要設置root的密碼。Ubuntu下,MySQL的配置信息在/etc/mysql目錄

MySQL默認的字符集是latin1:
這裏寫圖片描述
爲了避免中文可能帶來的亂碼問題,將默認字符集改成utf-8,具體可以參考這篇文章,修改 /etc/mysql/my.cnf 文件,在相應位置添加:

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

修改後是這樣的:
這裏寫圖片描述

安裝php5及Apache的php5模塊

apt-get install php5 libapache2-mod-php5 php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

安裝php7

apt-get install php libapache2-mod-php php7.0-mysql

OK! It Works!

安裝常用模塊(gd mysql-dev mbstring)

apt install libmysqlclient-dev libmysql++-dev php7.0-mysql php7.0-gd fp-compiler php7.0-mbstring

Apache配置

<VirtualHost ip or *: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.example.com

    ServerAdmin webmaster1@localhost
    DocumentRoot /var/www/html
    ServerName ip or domain

    # 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 ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/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.conif
    <Directory "/var/www/html">
            Options FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>

    Alias /something "/var/www/html/public"
    <Directory "/var/www/yzoi/public">
            Options FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>

FTP Server

vsftpd安裝與配置,戳這裏

可能會出現的問題:

ServerName問題

apr_sockaddr_info_get() failed for localhost
....
ServerName error

明明在apache2.conf裏設置了ServerName,apache2還是報錯?那麼就是系統host名的關係,在/etc/hosts中:

127.0.0.1       localhost
user-ip-address    domain.com   hostname

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

domain.com是你設定的域名,hostname是你主機名,最好和/etc/hostname主機名一致

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章