Web + Mobile Blog / Berlin http://sebastianviereck.de/en/ PHP Freelancer Thu, 19 Dec 2019 16:12:20 +0000 en hourly 1 http://wordpress.org/?v=3.5.1 Create your own metrics in AWS Cloudwatch http://sebastianviereck.de/en/php-create-your-own-metrics-aws-cloudwatch/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/php-create-your-own-metrics-aws-cloudwatch/#comments Thu, 19 Dec 2019 16:12:20 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4132-en Continue reading ]]> To use CloudWatch Create your own metrics, by e.g.. alarm and to operate monitoring with the data of the application, can you use the aws-php-sdk use the following code:

 $client = new CloudWatchClient([
        'region' => 'eu-central-1',
        'version' => '2010-08-01',
        'credentials' => [
            'key'    => 'xxx',
            'secret' => 'xxx
        ],
    ]);
}
$result = $this->client->putMetricData(array(

    'Namespace' => 'MyProject',
    'MetricData' => array(
        array(
            'MetricName' => 'MyMetric,
            'Timestamp' => time(),
            'Value' => 1,
            'Unit' => 'Count',
            'Dimensions' => array(
                array(
                    'Name' => 'Environment',
                    'Value' => 'test',
                ),
            ),
        )
    )
));
]]>
http://sebastianviereck.de/en/php-create-your-own-metrics-aws-cloudwatch/feed/ 0
Docker Image build suddenly no longer an Internet connection http://sebastianviereck.de/en/docker-image-build-suddenly-no-longer-an-internet-connection/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/docker-image-build-suddenly-no-longer-an-internet-connection/#comments Thu, 12 Dec 2019 16:18:32 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4128-en If you can't install packs on a build, you can try, restart the docker-machine:

docker-machine restart
]]>
http://sebastianviereck.de/en/docker-image-build-suddenly-no-longer-an-internet-connection/feed/ 0
Use Docker Shell Alias on Linux http://sebastianviereck.de/en/use-docker-shell-alias-under-linux/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/use-docker-shell-alias-under-linux/#comments Thu, 12 Dec 2019 16:13:34 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4126-en Continue reading ]]> In order to use the Linux Shell to use the usual aliases such as Ll for ls -lh to use, you have to use the following trick:

Since the login via sh is not a Linux login shell, the alias definitions are /etc/profile.d nicht geladen beim Login.

Solution:

Enough, deal with Sh –login in the container, dann stehen die Alias Definitionen zur Verfügung.

]]>
http://sebastianviereck.de/en/use-docker-shell-alias-under-linux/feed/ 0
DockerFile PHP with XDebug and Amazon Linux 2 http://sebastianviereck.de/en/dockerfile-php-with-xdebug-and-amazon-linux-2/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/dockerfile-php-with-xdebug-and-amazon-linux-2/#comments Thu, 12 Dec 2019 13:33:48 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4122-en Continue reading ]]> A DockerFile for a PHP 7.2. Environment with XDebug for a Symfony 4 Environment on Amazon Linux 2:

FROM amazonlinux:2.0.20190823.1

RUN yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

# install php
RUN amazon-linux-extras install -y php7.2
RUN yum install -y php-devel.x86_64 php-xml.x86_64 php-pecl-zip.x86_64  php-intl.x86_64

#install xdebug
RUN yum install -y gcc make
RUN yum install -y php-pear.noarch
RUN pecl install xdebug
RUN echo 'zend_extension=/usr/lib64/php/modules/xdebug.so' >> /etc/php.ini
]]>
http://sebastianviereck.de/en/dockerfile-php-with-xdebug-and-amazon-linux-2/feed/ 0
Docker Xdebug remote_host setzen in Toolbox für PHPStorm http://sebastianviereck.de/en/docker-xdebug-remote_host-setzen-toolbox-fuer-phpstorm/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/docker-xdebug-remote_host-setzen-toolbox-fuer-phpstorm/#comments Thu, 12 Dec 2019 10:55:26 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4119-en Continue reading ]]> To debug with Xdebug and PHPStorm in the Docker Container, but gets the error message:

Connection was not established. Probably 'xdebug.remote_host=docker.for.win.localhost' is incorrect

this is due to the, that Docker in the Toolbox variant for Windows has the placeholder docker.for.win.localhost not filled. Es muss also die IP des PHPStorm Rechners manuell gesetzt werden.

To do this, you manually set the value of xdebug.remote_host to 192.168.99.1 (the IP Docker sets for the host, can varnise) cli interpreter options:

 

]]>
http://sebastianviereck.de/en/docker-xdebug-remote_host-setzen-toolbox-fuer-phpstorm/feed/ 0
Docker Directory Mount on Windows 10 with Docker Toolbox http://sebastianviereck.de/en/docker-directory-mount-under-windows-10/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/docker-directory-mount-under-windows-10/#comments Thu, 28 Nov 2019 14:59:49 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4110-en Continue reading ]]> To place a local folder in a Docker Container on Windows 10 to mount, you can either use Docker for Windows or if you are using the Windows 10 Home Edition used, one is forced, This by hand with Virtual Box erledigen.

To do this, you have to use the Docker default Select Box in Virtual Box:

virtual_box_shared_folder

and add a new Shared Folder:virtual_box_shared_folder_add

Then you have to restart the docker-machine in the Docker Toolbox, this will restart the docker default Virtual Box:

docker-machine restart

Now you can mount your directory in his docker-compose.yml:

volumes:
  - /htdocs/my_project:/var/www/html/

Jetzt wird der Ordner in dem Docker Container erfolgreich gemountet.

The following command can be used to test, whether the mount was successful:

docker exec -it CONTAINER_ID ls -ll /var/www/html
]]>
http://sebastianviereck.de/en/docker-directory-mount-under-windows-10/feed/ 0
Search Yum Package http://sebastianviereck.de/en/search-yum-package/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/search-yum-package/#comments Thu, 07 Nov 2019 10:07:30 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4106-en With this command you can search for yum packets:

yum search mysql | grep client
]]>
http://sebastianviereck.de/en/search-yum-package/feed/ 0
CREATE PHP csv File with Umlauts for Excel http://sebastianviereck.de/en/php-csv-file-relauts-create-excel/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/php-csv-file-relauts-create-excel/#comments Thu, 10 Oct 2019 08:54:59 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4096-en Continue reading ]]> To create a csv file, where the umlauts are correctly coded, you have to be in your project, which is normally encoded UTF-8, encode the umlauts according to UTF-16LE. Diese Kodierung kann von Windows und Mac angezeigt werden.

$file = fopen('file.csv', 'w');
foreach ($rows as $row) {
 $row = array_map(function($cell){
 return mb_convert_encoding($cell, 'UTF-16LE', 'UTF-8');
 }, $row);
 fputcsv($file, $row, ';');
}
fclose($file);
]]>
http://sebastianviereck.de/en/php-csv-file-relauts-create-excel/feed/ 0
Configuring Monolog Logging for AWS Cloudwatch Logs http://sebastianviereck.de/en/configure-monolog-logging-for-aws-cloudwatch-logs/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/configure-monolog-logging-for-aws-cloudwatch-logs/#comments Mon, 30 Sep 2019 15:49:07 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4087-en Continue reading ]]> So that the logs are beautifully formatted from a Symfony 4 Arrive application in AWS Cloudwatch, must be Monologue configure as follows:

# config/services.yaml
Monolog\Formatter\JsonFormatter:
    calls:
        - [includeStacktraces]

and

# config/packages/prod/monolog.yaml
monolog:
  handlers:
    main:
      type:  stream
      level: error
      path:  '%kernel.logs_dir%/error.log'
      formatter: Monolog\Formatter\JsonFormatter

After that, you have to Cloudwatch Daemon to configure:

# /etc/awslogs/awslogs.conf
[general]
# Path to the CloudWatch Logs agent's state file. The agent uses this file to maintain
# client side state across its executions.
state_file = /var/lib/awslogs/agent-state

[/var/www/html/var/log/error.prod.log]
datetime_format = [%Y-%m-%d H:%M:%S]
file = /var/www/html/var/log/error.log
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = test_group_name/error_log

and you get wonderful (Aggregated) ErrorLog Information in Cloudwatch Logs Insight:

 

 

]]>
http://sebastianviereck.de/en/configure-monolog-logging-for-aws-cloudwatch-logs/feed/ 0
11 AWS Cloudformation Expert Tricks http://sebastianviereck.de/en/11-cloudformation-experts-tricks/?utm_source=rss&utm_medium=rss& http://sebastianviereck.de/en/11-cloudformation-experts-tricks/#comments Thu, 26 Sep 2019 15:06:31 +0000 Sebastian Viereck http://sebastianviereck.de/?p=4071-en Continue reading ]]> I've collected a lot of useful tips in my work with Cloudfromation, die ich gern teilen will.

1. Using an IDE with Autocomplete

For the Jetbrains IDEs like PHPStorm, Webstorm or IntelliJ there is a very good AWS Cloudfromation plugin called AWS Cloudfromation by Leonid Shalupov:

AWS Cloudfromation Plugin by Leonid Shalupov

This offers Autocomplete for the IDE and an automatic format check, whether attributes are set, which are not allowed there. Auf diesem weg kann man viele Fehler schon vor dem validieren finden und spart viel Zeit.

AWS Cloudfromation Autocomple

2. Using AWS CLI Commands

In order to test and deploy the templates in Cloudfromation, it is best to AWS CLI verwenden um viel Zeit zu sparen.

The most important commands for cloud formation are:

### stack anlegen:
#https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html
aws cloudformation create-stack  --template-body file://infrastructure/cloudformation.yml --stack-name myteststack

#### template validieren:
aws cloudformation validate-template --template-body file://infrastructure/cloudformation.yml

### status des stacks abrufen:
aws cloudformation describe-stacks --stack-name myteststack

### update stack
aws cloudformation update-stack --template-body file://infrastructure/cloudformation.yml --stack-name myteststack

### delete stack
aws cloudformation delete-stack --stack-name myteststack

## output ändern:
# json, table, text möglich
aws cloudformation describe-stacks --stack-name myteststack --output=text

3. Write multi-line text readable

files:
  /var/www/html/public/index.php:
    content: >-
      <html>
        <body>
          <h1>Welcome to the AWS CloudFormation PHP Sample</h1>
          <p/>
          <?php
            // Print out the current data and time
            print "The Current Date and Time is: <br/>";
            print date("g:i A l, F j Y.");
          ?>
          <?php  phpinfo(); ?>
        </body>
      </html>
    mode: '000600'
    owner: apache
    group: Apache

4. Using variables in strings

/etc/httpd/conf.d/http-vhost.conf:
  content: !Sub
    - >-
      <Directory "/var/www/html/public">
         Options Indexes FollowSymLinks
         AllowOverride All
         Require all granted
      </Directory>

      <VirtualHost *:80>
         DocumentRoot "/var/www/html/public"
         ServerName "${Domain}"
      </VirtualHost>
    - { Domain: !Ref Domain }

5. Use two stacks for testing

To save time, I always use 2 Stacks to develop, these are created and deleted alternately, weil sonst auf das Löschen des Stacks gewartet werden muss beim Entwickeln.

CLI commands:

aws cloudformation create-stack  --template-body file://infrastructure/cloudformation.yml --stack-name myteststack

aws cloudformation delete-stack --stack-name myteststack
aws cloudformation create-stack  --template-body file://infrastructure/cloudformation.yml --stack-name myteststack2

aws cloudformation delete-stack --stack-name myteststack2
aws cloudformation create-stack  --template-body file://infrastructure/cloudformation.yml --stack-name myteststack

....

6. Building a Cloudfromation Template

The most important page of the cloud formation documentation is the Building a Cloud Formation Template.

"Resources": {
  "MyInstance": {
    "Type": "AWS::EC2::Instance",
    "Metadata" : {
      "AWS::CloudFormation::Init" : {
        "config" : {
          "packages" : {
            :
          },
          "groups" : {
            :
          },
          "users" : {
            :
          },
          "sources" : {
            :
          },
          "files" : {
            :
          },
          "commands" : {
            :
          },
          "services" : {
            :
          }
        }
      }
    },
    "Properties": {
      :
    }
  }
}

Here, among other things,. described, in which order the configuration sections are processed:

  1. packages
  2. groupsgroups
  3. userssourceSources
  4. files
  5. commands
  6. servicServices

In dieser Reihenfolge sollten auch im Template die Konfigurationsabschnitte definiert werden.

7. Cronjob anlegen in Amazon Linux 2

Here are 2 Examples, how to work for different users (root and ec2-user) Crontabs creates:

InstallCrontab:
 files:
 /var/spool/cron/root:
 content: !Sub |
 # m h dom mon dow command
 39 1,13 * * * certbot renew --no-self-upgrade > /dev/null 2>&1

 mode: '000600'
 owner: root
 group: root

 /var/spool/cron/ec2-user:
 content: !Sub |
 # m h dom mon dow command
 */5 * * * * /usr/local/bin/aws-scripts-mon/mon-put-instance-data.pl --mem-avail --swap-used --disk-space-avail --disk-path=/ --from-cron > /dev/null 2>&1

 mode: '000600'
 owner: ec2-user
 group: ec2-user

8. yaml or json

Cloud formation offers 2 Formats to, which are indeed very different, json and yaml. I chose yaml, because there is less to write and the syntax easier to read. However, I noticed during the univing, that almost all questions on Stackoverflow are in json, was einen Nachteil dargestellt hat.

Even better, however, is:

9. Use a framework

Leider endet man immer mit einer riesigen Datei in yaml oder json ohne eine Möglichkeit den Inhalt aufzuteilen oder andere Dateien zu inkludieren.

That's why I would have a framework such as lono to recommend, um den Code einfacher zu halten und übersichtlich.

Die Möglichkeit ein Porjekt auf mehrere Dateien aufzusplitten erleichtert auch die Arbeit mit einem VCS wie GIT.

10. Debugging EC2 Instance Errors

Unfortunately, you often get the little telling error message:

&nbsp;"Failed to receive 1 resource signal(s) within the specified duration"

Error message in the Cloudwatch interface

Here you should then call the EC2 System Log and search for the correct error message for the location “Cloud-init”:

11. Create Cloudwatch Alarm with CloudWatchMonitoringScripts

To secure its infrastructure, you can create Cloudwatch alarms, which, for example,. by email (Sns) benachrichten, wenn die CPU Auslastung zu stark ist oder zu wenig freier RAM-Speicher vorhanden ist oder die Festplatte voll läuft.

To get the data for the hard drive and the RAM, there are CloudWatchMonitoringScripts installieren und einen Cronjob einrichten.

This can be done with Cloudfromation as follows:

InstallAwsCloudwatchAgent:
  commands:
    downloadCloudWatchMonitoringScripts:
      command: "Commandsl -o /tmp/CloudWatchMonitoringScripts-1.2.2.zip  https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip"

    unzipCloudWatchMonitoringScripts:
      command: "sudo unzip /tmp/CloudWatchMonitoringScripts-1.2.2.zip -d /usr/local/bin"

  packages:
    yum:
      perl-Switch: []
      perl-DateTime: []
packagesrl-Sys-Syslog: []
      perl-LWP-Protocol-https: []
      perl-Digest-SHA.x86_64: []
  files:
    /var/spool/cron/root:
      content: !Sub |
        # m h dom mon dow      command
        */5Files* *   /usr/local/bin/aws-scripts-moncontentt-instance-data.pl --mem-avail --swap-used  --disk-space-avail --disk-path=/ --from-cron > /dev/null 2>&1

      mode: '000644'
      owner: root
      group: root

And you can then create an alarm:

CPUAmode:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmName: MyCPUAlarm
    AlarmDescription: CPU alarm for ec2 instance
    AlarmActions:
      - arn:aws:sns:xxx
    MetricName: CPUUtilization
    Namespace: AWS/EC2
    Statistic: Average
    Period: '300'
    EvaluationPeriods: '3'
    Threshold: '50'
    ComparisonOperator: GreaterThanThreshold
    TreatMissingData:  missing
    Dimensions:
      - Name: InstanceId
        Value:
          Ref: WebServerInstance

MemoryAlarm:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmName: MyMemoryAlarm
    AlarmDescription: Memory alarm for ec2 instance
    AlarmActions:
      - arn:aws:sns:xxx
    MetricName: MemoryAvailable
    Namespace: System/Linux
    Statistic: Average
    Period: '300'
    EvaluationPeriods: '3'
    Threshold: '200'
    ComparisonOperator: LessThanOrEqualToThreshold
    TreatMissingData:  missing
    Dimensions:
      - Name: InstanceId
        Value:
          Ref: WebServerInstance

DiskSpaceAlarm:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmName: MyDiskSpaceAlaType  AWSlarmDescription: Free Disk Space alarm for ec2 instance
    AlarmActions:
      - arn:aws:sns:xxxx
    MetricName: DiskSpaceAvailable
    Namespace: System/Linux
    Statistic: Average
    Period: '300'
    EvaluationPeriods: '3'
    Threshold: '5'
    ComparisonOperator: LessThanOrEqualToThreshold
    TreatMissingData:  missing
    Dimensions:
      - Name: InstanceId
        Value:
          Ref: WebServerInstance
      - Name: Filesystem
        Value: /dev/xvda1
      - Name: MountPath
        Value: /

 

 

]]>
http://sebastianviereck.de/en/11-cloudformation-experts-tricks/feed/ 0