1. 程式人生 > >ECS配置lamp環境

ECS配置lamp環境

1、安裝apache

  1.1 安裝apache

[[email protected] ~]# yum install httpd httpd-devel

  1.2 啟動apache服務

[[email protected] ~]# systemctl start  httpd

  1.3 設定httpd服務開機啟動

[[email protected] ~]# systemctl enable  httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

  1.4 檢視服務狀態

[[email protected] ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 五 2017-07-21 17:21:37 CST; 6min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 2449 (httpd)
   Status: "Total requests: 11; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─2449 /usr/sbin/httpd -DFOREGROUND
           ├─2450 /usr/sbin/httpd -DFOREGROUND
           ├─2451 /usr/sbin/httpd -DFOREGROUND
           ├─2452 /usr/sbin/httpd -DFOREGROUND
           ├─2453 /usr/sbin/httpd -DFOREGROUND
           ├─2454 /usr/sbin/httpd -DFOREGROUND
           ├─2493 /usr/sbin/httpd -DFOREGROUND
           ├─2494 /usr/sbin/httpd -DFOREGROUND
           └─2495 /usr/sbin/httpd -DFOREGROUND

7月 21 17:21:35 nmserver-7.test.com systemd[1]: Starting The Apache HTTP Server...
7月 21 17:21:36 nmserver-7.test.com httpd[2449]: AH00558: httpd: Could not reliably determine the server's fully q...ssage
7月 21 17:21:37 nmserver-7.test.com systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

  1.5 防火牆設定開啟80埠

[[email protected] ~]# firewall-cmd --permanent --zone=public  --add-service=http
success
[[email protected] ~]# firewall-cmd --permanent --zone=public  --add-service=https
success
[[email protected] ~]# firewall-cmd --reload
success

  這裡可能會提示錯誤

FirewallD is not running



1.通過systemctl status firewalld檢視firewalld狀態,發現當前是dead狀態,即防火牆未開啟。

2.通過systemctl start firewalld開啟防火牆,沒有任何提示即開啟成功。

3.再次通過systemctl status firewalld檢視firewalld狀態,顯示running即已開啟了。

4.如果要關閉防火牆設定,可能通過systemctl stop firewalld這條指令來關閉該功能。

5.END

 

1.6確認80埠監聽中

 
[[email protected] ~]# netstat -tulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      1084/sshd           
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN      1486/master         
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      1084/sshd           
tcp6       0      0 localhost:smtp          [::]:*                  LISTEN      1486/master         
tcp6       0      0 [::]:http               [::]:*                  LISTEN      2449/httpd          
udp        0      0 localhost:323           0.0.0.0:*                           592/chronyd         
udp6       0      0 localhost:323           [::]:*                              592/chronyd   
 

這時候可能你的伺服器還是不能訪問

這是因為阿里雲的安全策略還沒有新增

 

重新配置安全組公網規則

原因分析:安全組預設沒有放行網站使用的埠(如 80 埠)。您需要自行放行該介面。

解決方法:

  1. 登入 ECS 控制檯,找到該例項。
  2. 單擊例項 ID,進入詳情頁,再單擊本例項安全組 > 配置規則 >新增安全組規則。
  3. 根據網站使用的埠配置新的安全組規則,放行網站使用的埠,最後單擊確定。

 

1.8 查伺服器IP

 
[[email protected] ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:56:bc:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.9/24 brd 192.168.8.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe56:bccf/64 scope link 
       valid_lft forever preferred_lft forever
3: bridge0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether ea:89:d5:c7:32:73 brd ff:ff:ff:ff:ff:ff
 

 

 1.9 瀏覽器登陸

當你看到這個的時候,Apache已經安裝完成了

 

2、安裝mysql

  2.1安裝mysql

[[email protected] ~]# yum install mariadb mariadb-server mariadb-libs mariadb-devel

 

[email protected] ~]# rpm -qa |grep maria
mariadb-libs-5.5.52-1.el7.i686
mariadb-5.5.52-1.el7.i686
mariadb-server-5.5.52-1.el7.i686
mariadb-devel-5.5.52-1.el7.i686

 

  2.2 開啟mysql服務,並設定開機啟動,檢查mysql狀態

 
[[email protected] ~]# systemctl start  mariadb 
[[email protected] ~]# systemctl enable  mariadb 
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[[email protected] ~]# systemctl status  mariadb 
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since 六 2017-07-22 21:19:20 CST; 21s ago
 Main PID: 9603 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─9603 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─9760 /usr/libexec/mysqld --basedir=/usr --datadir=/v...

7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...
7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...
7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...
7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...
7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...
7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...
7月 22 21:19:15 nmserver-7.test.com mariadb-prepare-db-dir[9524]: ...
7月 22 21:19:16 nmserver-7.test.com mysqld_safe[9603]: 170722 21...
7月 22 21:19:16 nmserver-7.test.com mysqld_safe[9603]: 170722 21...
7月 22 21:19:20 nmserver-7.test.com systemd[1]: Started MariaDB ...
 

 

 
[[email protected] ~]# netstat -tulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      1084/sshd           
tcp        0      0 0.0.0.0:mysql           0.0.0.0:*               LISTEN      9760/mysqld         
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      1084/sshd           
tcp6       0      0 [::]:http               [::]:*                  LISTEN      2449/httpd          
udp        0      0 localhost:323           0.0.0.0:*                           592/chronyd         
udp6       0      0 localhost:323           [::]:*                              592/chronyd 
 

  2.3 資料庫安全設定

 
[[email protected] ~]# 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):   //輸入root的密碼,首次安裝直接按回車即可
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] y
New password: 
Re-enter new 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] 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? [Y/n] n
 ... skipping.

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] y
 - 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] y
 ... 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!
 

 

  2.4 登陸資料庫測試

 
[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.02 sec)

MariaDB [(none)]> 
 

 

3、安裝PHP

/**這裡安裝的是PHP5.4.16 如果要安裝php7.1請繞道文末**/

  3.1 安裝php

[[email protected] ~]# yum -y install php
[[email protected] ~]# rpm -ql php
/etc/httpd/conf.d/php.conf
/etc/httpd/conf.modules.d/10-php.conf
/usr/lib/httpd/modules/libphp5.so
/usr/share/httpd/icons/php.gif
/var/lib/php/session

 

  3.2 將php與mysql關聯起來

 
[[email protected] ~]# yum install php-mysql
[[email protected] ~]# rpm -ql php-mysql
/etc/php.d/mysql.ini
/etc/php.d/mysqli.ini
/etc/php.d/pdo_mysql.ini
/usr/lib/php/modules/mysql.so
/usr/lib/php/modules/mysqli.so
/usr/lib/php/modules/pdo_mysql.so
 

 

  3.3 安裝常用PHP模組

[[email protected] ~]# yum install -y php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel php-bcmath

 

 

4 測試PHP

 
[[email protected] ~]# cd  /var/www/html/
[[email protected] html]# ls
[[email protected] html]# pwd
/var/www/html
[[email protected] html]# vi info.php

<?php
        phpinfo();
?>
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~        :wq
 

 

  3.5重啟apache伺服器

[[email protected] html]# systemctl restart httpd

  3.6測試PHP

  在自己電腦瀏覽器輸入ip/info.php,你可以看到已經安裝的模組;

 

4 安裝php7.1

 

如果你已經安裝過老版本

 

//解除安裝老版本

 -y remove php*

//設定安裝源

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

//檢視

yum search php71w

所有的外掛如下

mod_php71w.x86_64 : PHP module for the Apache HTTP Server
php71w-bcmath.x86_64 : A module for PHP applications for using the bcmath library
php71w-cli.x86_64 : Command-line interface for PHP
php71w-common.x86_64 : Common files for PHP
php71w-dba.x86_64 : A database abstraction layer module for PHP applications
php71w-devel.x86_64 : Files needed for building PHP extensions
php71w-embedded.x86_64 : PHP library for embedding in applications
php71w-enchant.x86_64 : Enchant spelling extension for PHP applications
php71w-fpm.x86_64 : PHP FastCGI Process Manager
php71w-gd.x86_64 : A module for PHP applications for using the gd graphics library
php71w-imap.x86_64 : A module for PHP applications that use IMAP
php71w-interbase.x86_64 : A module for PHP applications that use Interbase/Firebird databases php71w-intl.x86_64 : Internationalization extension for PHP applications php71w-ldap.x86_64 : A module for PHP applications that use LDAP php71w-mbstring.x86_64 : A module for PHP applications which need multi-byte string handling php71w-mcrypt.x86_64 : Standard PHP module provides mcrypt library support php71w-mysql.x86_64 : A module for PHP applications that use MySQL databases php71w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases php71w-odbc.x86_64 : A module for PHP applications that use ODBC databases php71w-opcache.x86_64 : An opcode cache Zend extension php71w-pdo.x86_64 : A database access abstraction module for PHP applications php71w-pdo_dblib.x86_64 : MSSQL database module for PHP php71w-pear.noarch : PHP Extension and Application Repository framework php71w-pecl-apcu.x86_64 : APCu - APC User Cache php71w-pecl-apcu-devel.x86_64 : APCu developer files (header) php71w-pecl-geoip.x86_64 : Extension to map IP addresses to geographic places php71w-pecl-igbinary.x86_64 : Replacement for the standard PHP serializer php71w-pecl-igbinary-devel.x86_64 : Igbinary developer files (header) php71w-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library php71w-pecl-imagick-devel.x86_64 : Imagick developer files (header) php71w-pecl-libsodium.x86_64 : Wrapper for the Sodium cryptographic library php71w-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon php71w-pecl-mongodb.x86_64 : PECL package MongoDB driver php71w-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store php71w-pecl-xdebug.x86_64 : PECL package for debugging PHP scripts php71w-pgsql.x86_64 : A PostgreSQL database module for PHP php71w-phpdbg.x86_64 : Interactive PHP debugger php71w-process.x86_64 : Modules for PHP script using system process interfaces php71w-pspell.x86_64 : A module for PHP applications for using pspell interfaces php71w-recode.x86_64 : A module for PHP applications for using the recode library php71w-snmp.x86_64 : A module for PHP applications that query SNMP-managed devices php71w-soap.x86_64 : A module for PHP applications that use the SOAP protocol php71w-tidy.x86_64 : Standard PHP module provides tidy library support php71w-xml.x86_64 : A module for PHP applications which use XML php71w-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol 

 

//安裝php以及擴充套件

//可以根據自己的需求選擇

yum install php71w php71w-fpm php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath

//開啟服務

service php-fpm start

//設定開機啟動

systemctl enable php-fpm.service

//檢查開機自啟動是否設定成功

systemctl list-dependencies | grep php-fpm

ps -ef | grep php-fpm

//上面安裝最基本的擴充套件包如果還想使用其他擴充套件包自己手動安裝擴充套件命令

yum search php71w    #檢視所有包命令

yum install php71w php71w-pdo     #完整命令,php71w-pdo 為自己想要安裝的包

 

測試PHP

 
[[email protected] ~]# cd  /var/www/html/
[[email protected] html]# ls
[[email protected] html]# pwd
/var/www/html
[[email protected] html]# vi info.php

<?php
        phpinfo();
?>
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~                                                                                        
~        :wq

 

//重啟Apache

 

 systemctl restart httpd

 

 

4、設定虛擬主機

  4.1 修改apache預設的網站根目錄

  apache配置檔案路徑:/etc/httpd/conf/httpd.conf

  DocumentRoot "/var/www/html"  修改為 DocumentRoot "/var/www"

  

  

  4.2 新增站點

<VirtualHost *:80>
    DocumentRoot "/var/www/abc"
    ServerName www.abc.com
    ServerAlias abc.com
    <Directory "/var/www/abc">
        Options FollowSymLinks ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/lang"
    ServerName lang.abc.com
    ServerAlias lang.abc.com
    <Directory "/var/www/lang">
        Options FollowSymLinks ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

  

 

  //重啟Apache

 systemctl restart httpd