## 配置云服務器
### 安裝NGINX、PHP和MySQL
參考我的另一篇文章,安裝NGINX、PHP和MySQL。(科學上網這一步可以根據實際情況選擇是否安裝,因為現在webtatic.com現在好像又能正常訪問了)。
[CentOS使用Yum源安裝 PHP、Nginx、MySQL](http://www.hmoore.net/colder/install_php_nginx_mysql_by_yum/727066)
以下是安裝過程的命令:
```
[root@VM_16_46_centos ~]# history
[root@VM_16_46_centos ~]# yum install epel-release
[root@VM_16_46_centos ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[root@VM_16_46_centos ~]# yum makecache fast
[root@VM_16_46_centos ~]# yum search php72
[root@VM_16_46_centos ~]# yum install php72w mod_php72w php72w-cli php72w-common php72w-common php72w-dba php72w-devel php72w-embedded php72w-enchant php72w-fpm php72w-gd php72w-imap php72w-interbase php72w-intl php72w-ldap php72w-mbstring php72w-mysql php72w-odbc php72w-opcache php72w-pdo php72w-pdo_dblib php72w-pear php72w-pecl-apcu php72w-pecl-apcu-devel php72w-pecl-geoip php72w-pecl-igbinary php72w-pecl-igbinary-devel php72w-pecl-imagick php72w-pecl-imagick-devel php72w-pecl-libsodium php72w-pecl-memcached php72w-pecl-mongodb php72w-pecl-redis php72w-pecl-xdebug php72w-pgsql php72w-phpdbg php72w-process php72w-pspell php72w-recode php72w-snmp php72w-soap php72w-sodium php72w-tidy php72w-xml php72w-xmlrpc
[root@VM_16_46_centos ~]# systemctl enable php-fpm.service
[root@VM_16_46_centos ~]# systemctl start php-fpm.service
[root@VM_16_46_centos ~]# systemctl status php-fpm.service
[root@VM_16_46_centos ~]# vim /etc/yum.repos.d/nginx.repo
[root@VM_16_46_centos ~]# yum install nginx
[root@VM_16_46_centos ~]# systemctl enable nginx.service
[root@VM_16_46_centos ~]# systemctl start nginx.service
[root@VM_16_46_centos ~]# systemctl status nginx.service
[root@VM_16_46_centos ~]# rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
[root@VM_16_46_centos ~]# yum makecache fast
[root@VM_16_46_centos ~]# yum install mysql-community-server
[root@VM_16_46_centos ~]# systemctl enable mysqld.service
[root@VM_16_46_centos ~]# systemctl start mysqld.service
[root@VM_16_46_centos ~]# systemctl status mysqld.service
[root@VM_16_46_centos ~]# grep 'temporary password' /var/log/mysqld.log
[root@VM_16_46_centos ~]# mysql -uroot -p
```
### 掛載NFS文件存儲
```
[root@VM_16_46_centos ~]# yum install -y nfs-utils
[root@VM_16_46_centos ~]# mkdir /exam
[root@VM_16_46_centos ~]# sudo mount -t nfs -o vers=4 172.16.16.48:/ /exam
[root@VM_16_46_centos ~]# cd /exam
[root@VM_16_46_centos ~]# mkdir code
[root@VM_16_46_centos ~]# cd code
[root@VM_16_46_centos ~]# touch index.html
[root@VM_16_46_centos ~]# vim index.html
```
我們先安裝nfs-utils掛載工具,然后根據文件存儲實例“掛載點信息”中的說明,掛載nfs文件存儲。我們把文件存儲掛載到/exam目錄下,并在文件存儲中創建code目錄和index.html文件。
然后我們使用命令df查看掛載是否成功:
```
[root@VM_16_46_centos ~]# df
```

掛載成功了。這里我們是執行了命令掛載的,主機重啟后就會斷開,需要重新掛載。因此,我們還要設置一下,讓系統每次啟動時,自動掛載。編輯/etc/rc.local文件,追加如下內容并保存:
```
[root@VM_16_46_centos ~]# vim /etc/rc.local
```
```
# Auto mout nfs
mkdir -p /exam
sudo mount -t nfs -o vers=4 172.16.16.48:/ /exam
```
以后,每次主機重啟,都會嘗試自動創建/exam目錄,并在此目錄上掛載文件存儲,不需要再手工掛載。
### 配置系統
1 、檢查系統最大可打開文件數
```
[root@VM_16_46_centos ~]# ulimit ?-n
```
如果太小,修改/etc/security/limits.conf加入以下內容:?
```
* soft nofile?100001
* hard nofile 100001
```
星號代表全局, soft為軟件,hard為硬件,nofile為這里指可打開文件數。
此配置需重啟才能生效。
修改vim /etc/sysctl.conf,增加如下配置:
```
fs.file-max=100001
```
修改/proc/sys/fs/file-max,內容為:
```
100001
```
修改NGINX配置文件/etc/nginx/nginx.conf,增加如下配置:
```
worker_rlimit_nofile 100001;
events {
worker_connections 10240;
}
```
因為socket在linux中也被認為是文件,如果連接數過多,會超過系統配置的數量,就會出現“Too many open files”的報錯,從而限制并發能力。
2、修改PHP配置文件/etc/php-fpm.d/www.conf:
```
pm = dynamic
pm.max_children = 5000
pm.start_servers = 30
pm.min_spare_servers = 10
pm.max_spare_servers = 50
pm.max_requests = 50
```
其它設置,可參考下面的文檔進行設置:[NGINX高并發性能優化](https://www.cnblogs.com/kevingrace/p/6094007.html)
3、創建站點
```
[root@VM_16_46_centos ~]# vim /etc/nginx/conf.d/exam.test.com.conf
```
寫入如下內容:
```
server
{
listen 80;
server_name exam.test.com;
index index.html index.htm index.php;
root /exam/code/public/;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
```
之后,我們把網站程序放在/exam/code中。創建一個文件:/exam/code/public/index.php,寫入如下內容
```
<?php
phpinfo();
```
重新載入nginx配置:
```
[root@VM_16_46_centos ~]# nginx -s reload
```
服務器配置完畢,我們就用目前這個配置,創建一個系統鏡像,在新增云服務器時,使用這個鏡像安裝系統,就得到了跟目前這個云服務器完全相同的服務器環境。
### 制作系統鏡像
在云服務器列表中,選擇“制作鏡像”菜單:
:-: 
:-: 
鏡像制作完成后,在“云服務器”-->“鏡像”中看到它,可以用這個鏡像創建實例,或者在新建云主機時,選擇使用“自定義鏡像”,并選擇使用這個鏡像。
:-: 
使用自定義鏡像,在對服務器進行擴容的時候非常快速,不需要在進行任何配置,只要創建服務器,即可迅速加入集群中,實現即買即用。特別提出的是,騰訊云支持“彈性伸縮”,“彈性伸縮”是配置用于在達到你規定的閾值時,有騰訊云自動為你增加或者減少云服務器,以動態的適應訪問壓力的方案。在“彈性伸縮”自動創建新的云主機時,不需要人工干預,只需要配置由彈性伸縮”服務使用你制作好的系統鏡像創建新的云主機,并由“彈性伸縮”服務幫你加入指定的負載均衡服務器集群中。